问题
I need to run a Perl script for several days processing something. On a linux Centos server, from the SSH terminal I run this command:
nohup perl script.cgi 2>&1 &
This runs the script in the background and writes the output to nohup.out. The problem when I close the SSH terminal or even my internet connection disconnects the script terminates.
I need to keep this command running in the background on the server after I close the SSH terminal.
回答1:
you can either use screen or run the command using supervisor in linux systems.
- you can install screen using
sudo apt install screen
- then use following command to run it.
screen -S test_command
nohup perl script.cgi 2>&1 &
- Then press ctrl+a and ctrl+d to leave that session running for whatever amount of time required until your server reboots.
- If you want to stop the command use
screen -x test_command
, then ctrl+c and use ctrl+a and ctrl+d to close screen or ctrl+a and ctrl+d to leave the screen session as it is.
回答2:
You can use Terminal multiplexer tools like screen, byobu or tmux. I personally use screen. so install it on remote server via sudo apt-get install screen.
- ssh into server
- open screen session by screen -S sessionname
- Now run your command (background/foreground both works)
- now detach to your session by command ctrl+a then press d.
- Now shut your pc and enjoy
- now come back ssh into server then use command screen -x sessionname to reconnect the detached session.
Hurray! script is still running.
回答3:
The only way I was able to run the command and exit the shell and keep the command running is using the "at" tool to schedule the job like this using the full script path:
echo "perl /home/username/www/script.cgi" | at now + 1 minute
来源:https://stackoverflow.com/questions/57292884/run-linux-command-in-background-and-keep-runing-after-closing-ssh