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 ?

Thanks.

link|improve this question
feedback

closed as exact duplicate by slhck, Simon Sheehan, Gareth, Sathya Nov 18 '11 at 8:51

This question covers exactly the same ground as earlier questions on this topic; its answers may be merged with another identical question. See the FAQ for guidance on how to improve it.

3 Answers

up vote 8 down vote accepted

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 &

link|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! – Bicou Nov 17 '11 at 16:52
you are welcome :) – bitsMix Nov 17 '11 at 16:54
feedback

Make a shell wrapper like this:

#!/bin/sh
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:

#!/bin/sh
( 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
link|improve this answer
feedback

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.

link|improve this answer
I have to use csh. – Bicou Nov 19 '11 at 13:47
feedback

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