In 2021 we can also suggest for linux distributions with systemd the use of systemd-run
as a way to detach a process and keep it running after closing ssh login.
One can launch a command
, give it some name like 'background_cmd_service' and turn it into a systemd service. Later inspect status:
systemd-run --unit=background_cmd_service --remain-after-exit command
# later on
journalctl -b -u background_cmd_service.service
systemctl status background_cmd_service.service
When launching the service as regular user, one might need to enable lingering (cf. enable-linger
loginctl option). This is true even with nohup since systemd is cleaning up all services running within the session once the user logs out.
For scripts, or other executables, you can provide the path to the executable like this:
systemd-run --unit=background_cmd_service --remain-after-exit --working-directory=/full/path/ command
You can also stop your service or clear a failed service using the standard systemctl
options like:
systemctl stop background_cmd_service.service
systemctl reset-failed background_cmd_service.service
Many of the pre-2016, but highly voted answers on stack exchange suggesting nohup
, disown
, command &
, and (command &)
don't work since 2016, due to a systemd default change in how it treats user processes after user logout - Details
nohup
intercepts SIGHUP so that when the shell that ran it quits and sends SIGHUP to all its still-running children,long-running-process
doesn't die.disown
simply removes the specified job from Bash's child list, so it won't try to send SIGHUP at all.nohup
is a program separate from the shell, so it works with all shells, whereasdisown
is a Bash builtin.nohup
accepts the command to run, whereasdisown
only works after the job is started and you've backgrounded it so you can get back to the shell. – Warren Young Aug 15 '10 at 14:00http://www.serverwatch.com/tutorials/article.php/3935306/Detach-Processes-With-Disown-and-Nohup.htm
– Dan Aug 12 '14 at 21:58tail -f nohup.out
to check what's going on when you're back. – Ricardo Stuven Nov 03 '16 at 15:15nohup
? i tried disown but it didin't work – Abhay Mar 26 '18 at 04:17pgrep
,pkill
,ps -eaf | grep
, etc. If you need to reattach and shut it down gracefully, then you probably want to be usingscreen
/tmux
instead. – Warren Young Mar 26 '18 at 04:42long-running-process
in the background so you can log out without having to do that via a separate step. – Warren Young Apr 03 '18 at 21:46&
? See What are the shell's control and redirection operators? See also Difference between nohup, disown and&
. – G-Man Says 'Reinstate Monica' Mar 12 '24 at 22:12