Hosting Websites on Bare Minimum VPS/Dedicated Servers

How to properly background a process in Linux

Tags: , , , Date/Time: December 21, 2015 @ 12:00 am, by Jarland Donnell

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.

5 Comments

  1. Gary Moon:

    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.

    December 21, 2015 @ 1:51 pm | Reply
  2. aglodek:

    Simple, nice and useful. Thanks :)

    December 21, 2015 @ 3:12 pm | Reply
  3. sleddog:

    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 :)

    December 21, 2015 @ 9:35 pm | Reply
  4. 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.

    December 22, 2015 @ 9:03 pm | Reply
    • nohup was the way I did it as well before tmux/screen.

      December 28, 2015 @ 12:55 am | Reply
  5. hugo:

    I usually do :
    nohup command &>/dev/null &

    January 24, 2016 @ 12:58 pm | Reply
  6. If at all possible, I’d recommend writing a systemd service file. Starting the process manually feels a bit hackish to me.

    June 27, 2016 @ 1:38 pm | Reply

Leave a Reply

Some notes on commenting on LowEndBox:

  • Do not use LowEndBox for support issues. Go to your hosting provider and issue a ticket there. Coming here saying "my VPS is down, what do I do?!" will only have your comments removed.
  • Akismet is used for spam detection. Quoting webhostingtalk.com URL seems to get binned consistently here, but I do peek into the spam box frequently to publish those comments.
  • Use <pre>...</pre> to quote the output from your terminal/console, or consider using a pastebin service.

Your email address will not be published. Required fields are marked *