If you’re like me, you find yourself running processes every now and then that don’t have init scripts and simply remain active in your session. One common example of that would be a Minecraft server manually launched via SSH/console. Something you’ve probably run into is that the process is killed once your shell session ends. A common answer to this is to use “screen” but that isn’t really the standard “Linux Admin” way to do it.
So for this short tutorial, I would like to show you how to background and disown a process so that it continues to run even when you log out.
In my example, I’m using the Caddy web server which doesn’t include it’s own init script. I’m executing it as root like a bad admin, I trust you to make good decisions :)
First, start your process with the & symbol after it to start a job like this:
root@gw:/home/jarland/bin# ./caddy –conf=”/home/jarland/conf/jarlandme” &
Next, if you press Enter you will see that you are back at your regular prompt and that the process is still running. Take a look at the jobs running in the foreground with this command:
jobs -l
It should look something like this:
root@gw:/home/jarland/bin# jobs -l
[1]+ 9983 Running ./caddy –conf=”/home/jarland/conf/jarlandme” &
Now let’s disown the job with this command:
disown
If you were to run “jobs -l” again you would see none. The process has been kicked to the background and disowned. You can log out, it will remain running. Easy, right?
There are of course many more details to this and variations on ways that you can use it, but this is the shortest path to accomplish the goal using the proper tools.
Related Posts:
- Q3 Top Provider Poll Results - November 13, 2016
- BudgetNode – DDoS Protected OpenVZ VPS Starting @ $12/year – Ashburn, Virginia - June 29, 2016
- BuyVM – $7/m KVM 2GB RAM / 40GB SSD / 2TB BW – in Las Vegas - June 17, 2016
I wouldn’t agree that this approach is any more “Linux Admin”-like than using screen or tmux, nor that it’s using any “proper tools”. Indeed you could at least see console output in a multiplexer.
Any Linux admin should be using an upstart (http://upstart.ubuntu.com/) or systemd (https://wiki.ubuntu.com/SystemdForUpstartUsers) script, which affords all the flexibility you should need, and allows you to log and automatically restart your process, among many other features.
Simple, nice and useful. Thanks :)
Careful jarland, with useful posts like this you’re bound to get disowned by the upstarts.
Congrats on your new position, hope you have great success :)
Or you could simply use ‘nohup’ to tell the process to ignore the hangup (e.g. logout):
nohup ./caddy –conf=”/home/jarland/conf/jarlandme” &
‘Screen’ is also useful for backgrounding processes where you still want to be able to interact with them at a later date.
nohup was the way I did it as well before tmux/screen.
I usually do :
nohup command &>/dev/null &
If at all possible, I’d recommend writing a systemd service file. Starting the process manually feels a bit hackish to me.