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

前端 未结 20 1313
悲&欢浪女
悲&欢浪女 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 07:01

    I used screen command. This link has detail as to how to do this

    https://www.rackaid.com/blog/linux-screen-tutorial-and-how-to/#starting

    0 讨论(0)
  • 2020-11-22 07:02

    Append this string to your command: >&- 2>&- <&- &. >&- means close stdout. 2>&- means close stderr. <&- means close stdin. & means run in the background. This works to programmatically start a job via ssh, too:

    $ ssh myhost 'sleep 30 >&- 2>&- <&- &'
    # ssh returns right away, and your sleep job is running remotely
    $
    
    0 讨论(0)
  • 2020-11-22 07:05

    i would also go for screen program (i know that some1 else answer was screen but this is a completion)

    not only the fact that &, ctrl+z bg disown, nohup, etc. may give you a nasty surprise that when you logoff job will still be killed (i dunno why, but it did happened to me, and it didn't bother with it be cause i switched to use screen, but i guess anthonyrisinger solution as double forking would solve that), also screen have a major advantage over just back-grounding:

    screen will background your process without losing interactive control to it
    

    and btw, this is a question i would never ask in the first place :) ... i use screen from my beginning of doing anything in any unix ... i (almost) NEVER work in a unix/linux shell without starting screen first ... and i should stop now, or i'll start an endless presentation of what good screen is and what can do for ya ... look it up by yourself, it is worth it ;)

    0 讨论(0)
  • 2020-11-22 07:07

    Use screen. It is very simple to use and works like vnc for terminals. http://www.bangmoney.org/presentations/screen.html

    0 讨论(0)
  • 2020-11-22 07:07

    There's also the daemon command of the open-source libslack package.

    daemon is quite configurable and does care about all the tedious daemon stuff such as automatic restart, logging or pidfile handling.

    0 讨论(0)
  • 2020-11-22 07:08

    When the session is closed the process receives the SIGHUP signal which it is apparently not catching. You can use the nohup command when launching the process or the bash built-in command disown -h after starting the process to prevent this from happening:

    > help disown
    disown: disown [-h] [-ar] [jobspec ...]
         By default, removes each JOBSPEC argument from the table of active jobs.
        If the -h option is given, the job is not removed from the table, but is
        marked so that SIGHUP is not sent to the job if the shell receives a
        SIGHUP.  The -a option, when JOBSPEC is not supplied, means to remove all
        jobs from the job table; the -r option means to remove only running jobs.
    
    0 讨论(0)
提交回复
热议问题