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

前端 未结 20 1312
悲&欢浪女
悲&欢浪女 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:17

    On a Debian-based system (on the remote machine) Install:

    sudo apt-get install tmux

    Usage:

    tmux

    run commands you want

    To rename session:

    Ctrl+B then $

    set Name

    To exit session:

    Ctrl+B then D

    (this leaves the tmux session). Then, you can log out of SSH.

    When you need to come back/check on it again, start up SSH, and enter

    tmux attach session_name

    It will take you back to your tmux session.

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

    daemonize? nohup? SCREEN? (tmux ftw, screen is junk ;-)

    Just do what every other app has done since the beginning -- double fork.

    # ((exec sleep 30)&)
    # grep PPid /proc/`pgrep sleep`/status
    PPid:   1
    # jobs
    # disown
    bash: disown: current: no such job
    

    Bang! Done :-) I've used this countless times on all types of apps and many old machines. You can combine with redirects and whatnot to open a private channel between you and the process.

    Create as coproc.sh:

    #!/bin/bash
    
    IFS=
    
    run_in_coproc () {
        echo "coproc[$1] -> main"
        read -r; echo $REPLY
    }
    
    # dynamic-coprocess-generator. nice.
    _coproc () {
        local i o e n=${1//[^A-Za-z0-9_]}; shift
        exec {i}<> <(:) {o}<> >(:) {e}<> >(:)
    . /dev/stdin <<COPROC "${@}"
        (("\$@")&) <&$i >&$o 2>&$e
        $n=( $o $i $e )
    COPROC
    }
    
    # pi-rads-of-awesome?
    for x in {0..5}; do
        _coproc COPROC$x run_in_coproc $x
        declare -p COPROC$x
    done
    
    for x in COPROC{0..5}; do
    . /dev/stdin <<RUN
        read -r -u \${$x[0]}; echo \$REPLY
        echo "$x <- main" >&\${$x[1]}
        read -r -u \${$x[0]}; echo \$REPLY
    RUN
    done
    

    and then

    # ./coproc.sh 
    declare -a COPROC0='([0]="21" [1]="16" [2]="23")'
    declare -a COPROC1='([0]="24" [1]="19" [2]="26")'
    declare -a COPROC2='([0]="27" [1]="22" [2]="29")'
    declare -a COPROC3='([0]="30" [1]="25" [2]="32")'
    declare -a COPROC4='([0]="33" [1]="28" [2]="35")'
    declare -a COPROC5='([0]="36" [1]="31" [2]="38")'
    coproc[0] -> main
    COPROC0 <- main
    coproc[1] -> main
    COPROC1 <- main
    coproc[2] -> main
    COPROC2 <- main
    coproc[3] -> main
    COPROC3 <- main
    coproc[4] -> main
    COPROC4 <- main
    coproc[5] -> main
    COPROC5 <- main
    

    And there you go, spawn whatever. the <(:) opens an anonymous pipe via process substitution, which dies, but the pipe sticks around because you have a handle to it. I usually do a sleep 1 instead of : because its slightly racy, and I'd get a "file busy" error -- never happens if a real command is ran (eg, command true)

    "heredoc sourcing":

    . /dev/stdin <<EOF
    [...]
    EOF
    

    This works on every single shell I've ever tried, including busybox/etc (initramfs). I've never seen it done before, I independently discovered it while prodding, who knew source could accept args? But it often serves as a much more manageable form of eval, if there is such a thing.

    0 讨论(0)
提交回复
热议问题