LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Running script in background? (https://www.linuxquestions.org/questions/linux-newbie-8/running-script-in-background-792378/)

bobbyright 03-01-2010 12:14 PM

Running script in background?
 
If I wanted to run a script in the background and keep it running even if i close putty i do like

./perl.pl &

right?

tom4everitt 03-01-2010 12:21 PM

That will run it in the background in the sense that you will get a new prompt.

If you want it to "survive" the terminal that you started it in (or putty in your case) you should start it with

nohup ./perl.pl &

or have a look at the screen command.

Prehistorik 03-01-2010 12:30 PM

Not quite. The script process will still be a child of your remotely open shell, so it will be terminated when you disconnect. A better solution is to use screen:

Code:

screen -S u ./perl.pl
  • -S tells screen to create a session from scratch
  • u is the name of your screen session in which the script is being run. You may choose a different one if you like
  • After all the options and the session name your command goes (along with arguments, if any).

Then you detach it (Ctrl-A, then Ctrl-D) and log out of the SSH session. To reattach it and see the results of your script you may log in to your remote server again and issue a command:

Code:

screen -d -r u
  • -r tells screen to reattach the detached session
  • -d is used to detach it first if it is already attached elsewhere
  • Again, u is your session name

Prehistorik 03-01-2010 12:33 PM

Quote:

Originally Posted by tom4everitt (Post 3881266)
nohup ./perl.pl &

Didn't know that before, thanks a lot :)

tom4everitt 03-02-2010 02:17 AM

No problem :) Prehistoriks way (with the screen command) is more powerful and perhaps also more common. Its very convenient, especially when you're connected from a remote location. It sort of allows you to have several terminals (screens) in the same terminal.

The nohup (which comes from "no hangup", so you remember it ;)) is sort of neat in its simpleness though.


All times are GMT -5. The time now is 12:21 AM.