Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a command in a shell and keep running the command when you close the session

I am using Putty to connect to a remote server. What I want to know is if there is any way to write my commands and allow them to keep running after I close the session with Putty. The reason for this is that I do not want to keep the computer ON all the time. Is there any way to do this?.

Update with the solution

For my question as it is presented the best solution is use one of the commands provided such as nohup, because you do not have to install any additional software. But if you are in the same problem use screen, install it and use it. It is amazing.

I have selected the answer of Norman Ramsey as favourite because propose several solutions using commands and screen. But please check the other answers specially the one of PEZ, then you get an insight of what screen is able todo.

like image 951
Eduardo Avatar asked Jan 10 '09 18:01

Eduardo


People also ask

Which command keep processes running even after exiting the shell?

Another way to keep a process running after the shell exit is to use nohup . The nohup command executes another program specified as its argument and ignores all SIGHUP (hangup) signals. SIGHUP is a signal that is sent to a process when its controlling terminal is closed. The command output is redirected to the nohup.

How do I run a command in the background and close a terminal?

In the new screen session, execute the command or script you wish to put in the background. Press Ctrl + A on your keyboard, and then D . This will detach the screen, then you can close the terminal, logout of your SSH session, etc, and the screen will persist.

How can I keep a process alive after closing the putty session?

In the new screen type your run command ./startWebLogic.sh . Press Ctrl + a then press d (without holding Ctrl ); you will return back to the previous screen. When you want to return to your server log screen, type command screen -r .


1 Answers

screen! It's the best thing since sliced bread. (Yeah, I know others have already suggested it, but it's so good the whole world should join in and suggest it too.)

screen is like, like, ummmm ... like using VNC or the like to connect to a GUI destop, but for command shell windows. You can have several shell "windows" open at once in the same screen session. You can do stuff like:

  1. Start a screens session using "screen -dR" (get used to using -dR)
    • run some commands in one window
    • press CTRL-A,C to create a new window open a file there in vim
    • press CTRL-A,0 to go back to the first window and issue some command on the file you just edited
    • CTRL-A, 1 to go back to your vim session
    • CTRL-A, C for yet another window and maybe do "sudo - su" (because you just happen to need a full root shell)
    • CTRL-A, 0 and start a background process
    • CTRL-A, C to create yet a new window, "tail -f" the log for that background process
    • CTRL-A, d to disconnect your screen then CTRL-D to disconnect from the server
    • Go on vacation for three weeks
    • Log on to the server again and issue "screen -dR" to connect to your existing screen session
    • check the log in the the fourth window with CTRL-A, 3 (it's like you've been there watching it all the time)
    • CTRL-A, 1 to pick up that vim session again
    • I guess you're starting to get the picture now? =)

It's like magic. I've been using screen for longer than I can remember and I'm still totally amazed with how bloody great it is.

EDIT: Just want to mention there's now also tmux. Very much like screen, but has some unique features, splitting the windows being the most prominent one.

like image 59
PEZ Avatar answered Oct 19 '22 23:10

PEZ