Possible Duplicate:
Leave bash script running on remote terminal while not logged in?
How do I detach a process from Terminal, entirely?

I'm running a program on a linux server that will take days to complete.

I'm launching it from my workstation from an SSH terminal, as this program is command-line only.

I want to be able to do all of these :

  • launch that program,
  • redirect standard outputs to files,
  • exit my SSH session without making this terminate the process.

I thought about $ ./MyProg.csh -params -foo -bar </dev/null 1>~/out.log 2>~/err.log &

However, the process is terminated the moment I close my SSH session. My workstation is running Windows XP, and I cannot guarantee its uptime over several days, which is required for the processing of my data on the Linux server.

As you may have noted, my program requires to be launched from CSH.

Is it possible to do this ?



you can either user screen or nohup

if you choose screen :

launch screen before you run your service:

$ screen

and then, run your service inside of the screen:

./MyProg.sh > myprog.log ( or anything you want here ).

and then Ctrl+a d

when you came back, just:

$ screen -r

for more information: http://www.gnu.org/s/screen/

about the nohup : just:

nohup ./MyProg.sh > myprog.log &

| improve this answer | |
  • AWESOME! I knew I could manage using screen but I wasn't able to get through it. This is plain awesome, thank you so much! – Benoit Duffez Nov 17 '11 at 16:52
  • I think the "disown" command will do what he OP asked for.... – Erik Aronesty Nov 15 '16 at 14:06

Make a shell wrapper like this:

exec </dev/null
exec >> ~/out.log
exec 2>> ~/err.log
exec setsid ./MyProg.csh -params -foo -bar

If you need PID file too, then send this shell script to bg and read $! (PID of last bg process). You can do it using subshell with () in the same script like this:

( exec </dev/null
  exec >> ~/out.log
  exec 2>> ~/err.log
  exec setsid ./MyProg.csh -params -foo -bar
) &
kill -0 $! > /dev/null 2>&1 || exit 1
echo $! > program.pid
exit 0
| improve this answer | |

The connection drops because the shell sends SIGHUP to all children when it exits. If you use bash or zsh, you can use disown and the shell will no longer send SIGHUP and the processes will stay around.

screen and setsid above works, but this answer has the advantage of not having to remember to do screen before you start anything. screen has a lot of power other than this, so you may want to get into a habit of using it.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.