How to Keep a Process Running in Background on Linux?

Process stops after SSH disconnects, want to keep it running in background

Solutions

nohup()Recommended

# nohup node server.js > output.log 2>&1 & # tail -f output.log # jobs -l

nohup SIGHUP (). &.

: commands

systemd (Recommended)Recommended

# sudo vim /etc/systemd/system/myapp.service # : # [Unit]
# Description=My Application
# After=network.target
#
# [Service]
# Type=simple
# User=deploy
# WorkingDirectory=/opt/myapp
# ExecStart=/usr/bin/node server.js
# Restart=always
#
# [Install]
# WantedBy=multi-user.target # sudo systemctl daemon-reload
sudo systemctl enable myapp
sudo systemctl start myapp
sudo systemctl status myapp

systemd Linux.,.

:, and

screen/tmux()

# tmux tmux new -s myapp # node server.js # # Ctrl+B D # tmux attach -t myapp # New # screen screen -S myapp
node server.js
# Ctrl+A D
screen -r myapp

tmux/screen, SSH, New.

: and

pm2(Node.js )

# npm install -g pm2 # pm2 start server.js --name myapp # pm2 list
pm2 logs myapp
pm2 restart myapp
pm2 stop myapp # pm2 startup
pm2 save

pm2 Node.js,,.

: Node.js

nohup
nohup, systemd pm2
tmux/screen

Related Commands