问题
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
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/26617271/executing-a-script-which-runs-even-if-i-log-off