Run Linux command in background and keep runing after closing SSH [duplicate]

此生再无相见时 提交于 2021-02-20 01:35:33

问题


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.

  1. ssh into server
  2. open screen session by screen -S sessionname
  3. Now run your command (background/foreground both works)
  4. now detach to your session by command ctrl+a then press d.
  5. Now shut your pc and enjoy
  6. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!