How to prevent a background process from being stopped after closing SSH client in Linux

前端 未结 20 1713
悲&欢浪女
悲&欢浪女 2020-11-22 06:16

I\'m working on a Linux machine through SSH (Putty). I need to leave a process running during the night, so I thought I could do that by starting the process in background (

20条回答
  •  野的像风
    2020-11-22 06:51

    For most processes you can pseudo-daemonize using this old Linux command-line trick:

    # ((mycommand &)&)
    

    For example:

    # ((sleep 30 &)&)
    # exit
    

    Then start a new terminal window and:

    # ps aux | grep sleep
    

    Will show that sleep 30 is still running.

    What you have done is started the process as a child of a child, and when you exit, the nohup command that would normally trigger the process to exit doesn't cascade down to the grand-child, leaving it as an orphan process, still running.

    I prefer this "set it and forget it" approach, no need to deal with nohup, screen, tmux, I/o redirection, or any of that stuff.

提交回复
热议问题