3

So, I have a long running script (of order few days) say execute.sh which I am planning to execute on a server on which I have a user account...

Now, I want to execute this script so that it runs forever even if I logoff or disconnect from the server?? How do i do that? THanks

frazman
  • 32,081
  • 75
  • 184
  • 269
  • http://en.wikipedia.org/wiki/Cron – Marc B Oct 28 '14 at 19:51
  • 1
    possible duplicate of [How to make a programme continue to run after log out from ssh?](http://stackoverflow.com/questions/954302/how-to-make-a-programme-continue-to-run-after-log-out-from-ssh) – ErlVolton Oct 28 '14 at 19:52
  • 1
    possible duplicate of [Linux: Prevent a background process from being stopped after closing SSH client](http://stackoverflow.com/questions/285015/linux-prevent-a-background-process-from-being-stopped-after-closing-ssh-client) – Etan Reisner Oct 28 '14 at 19:54

2 Answers2

9

You have a couple of choices. The most basic would be to use nohup:

nohup ./execute.sh

nohup executes the command as a child process and detaches from terminal and continues running if it receives SIGHUP. This signal means sig hangup and will getting triggered if you close a terminal and a process is still attached to it.

The output of the process will getting redirected to a file, per default nohup.out located in the current directory.


You may also use bash's disown functionality. You can start a script in bash:

./execute.sh

Then press Ctrl+z and then enter:

disown

The process will now run in background, detached from the terminal. If you care about the scripts output you may redirect output to a logfile:

./execute.sh > execute.log 2>&1

Another option would be to install screen on the remote machine, run the command in a screen session and detach from it. You'll find a lot of tutorials about this.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
3

nohup (no hangup) it and run it in the background:

nohup execute.sh &

Output that normally would have gone to the screen (STDOUT) will go to a file called nohup.out.

Gary_W
  • 9,933
  • 1
  • 22
  • 40