How to run Node.js as a background process and never die?

前端 未结 14 941
刺人心
刺人心 2020-11-22 10:59

I connect to the linux server via putty SSH. I tried to run it as a background process like this:

$ node server.js &

However, after 2.5

相关标签:
14条回答
  • 2020-11-22 11:15

    Have you read about the nohup command?

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

    I have this function in my shell rc file, based on @Yoichi's answer:

    nohup-template () {
        [[ "$1" = "" ]] && echo "Example usage:\nnohup-template urxvtd" && return 0
        nohup "$1" > /dev/null 2>&1 &
    }
    

    You can use it this way:

    nohup-template "command you would execute here"
    
    0 讨论(0)
  • 2020-11-22 11:21

    Simple solution (if you are not interested in coming back to the process, just want it to keep running):

    nohup node server.js &
    

    There's also the jobs command to see an indexed list of those backgrounded processes. And you can kill a backgrounded process by running kill %1 or kill %2 with the number being the index of the process.

    Powerful solution (allows you to reconnect to the process if it is interactive):

    screen
    

    You can then detach by pressing Ctrl+a+d and then attach back by running screen -r

    Also consider the newer alternative to screen, tmux.

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

    nohup node server.js > /dev/null 2>&1 &

    1. nohup means: Do not terminate this process even when the stty is cut off.
    2. > /dev/null means: stdout goes to /dev/null (which is a dummy device that does not record any output).
    3. 2>&1 means: stderr also goes to the stdout (which is already redirected to /dev/null). You may replace &1 with a file path to keep a log of errors, e.g.: 2>/tmp/myLog
    4. & at the end means: run this command as a background task.
    0 讨论(0)
  • 2020-11-22 11:22

    To run command as a system service on debian with sysv init:

    Copy skeleton script and adapt it for your needs, probably all you have to do is to set some variables. Your script will inherit fine defaults from /lib/init/init-d-script, if something does not fits your needs - override it in your script. If something goes wrong you can see details in source /lib/init/init-d-script. Mandatory vars are DAEMON and NAME. Script will use start-stop-daemon to run your command, in START_ARGS you can define additional parameters of start-stop-daemon to use.

    cp /etc/init.d/skeleton /etc/init.d/myservice
    chmod +x /etc/init.d/myservice
    nano /etc/init.d/myservice
    
    /etc/init.d/myservice start
    /etc/init.d/myservice stop
    

    That is how I run some python stuff for my wikimedia wiki:

    ...
    DESC="mediawiki articles converter"
    DAEMON='/home/mss/pp/bin/nslave'
    DAEMON_ARGS='--cachedir /home/mss/cache/'
    NAME='nslave'
    PIDFILE='/var/run/nslave.pid'
    START_ARGS='--background --make-pidfile --remove-pidfile --chuid mss --chdir /home/mss/pp/bin'
    
    export PATH="/home/mss/pp/bin:$PATH"
    
    do_stop_cmd() {
        start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 \
            $STOP_ARGS \
            ${PIDFILE:+--pidfile ${PIDFILE}} --name $NAME
        RETVAL="$?"
        [ "$RETVAL" = 2 ] && return 2
        rm -f $PIDFILE
        return $RETVAL
    }
    

    Besides setting vars I had to override do_stop_cmd because of python substitutes the executable, so service did not stop properly.

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

    Try this for a simple solution

    cmd & exit

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