Stop Backgrounding Scripts and Daemonize Them With the Daemon Utility

If you have a small utility or script that you need to run in the background – but haven’t gone through the work to make it into a real daemon – may I suggest the daemon utility from libslack?

If you’re running your scripts as & to send it to background, that’s not an ideal solution. It’s not truly turning your script into a daemon, though. It’s quite possible, but it’s kind of a pain in the rear. Writing systemd units is also a bit of a PITA. But the daemon utility is perfect for these small jobs.

Once you download and install the utility, the options are available from the shell by simply typing daemon --help, but the documentation is somewhat sparse, so this is a quick HOWTO for anyone else interested … and for me, when I forget how to use it.

Starting a Daemon

The most common way you’re probably going to want to start this is like so:

/usr/bin/daemon --name=[NAME] --inherit --respawn --command [COMMAND TO RUN]

The --name option lets you name (and control) the process after it starts. --inherit means that it will inherit the environment variables in place at the time of execution in that shell. --respawn means exactly what it says on the tin.

For example, I’m working on a notification tweaking script for weechat that requires a program running in the background. So my command line is:

/usr/bin/daemon --name=fifo --inherit --respawn --command /home/steven/.weechat/

If you wish to run it in the foreground (to test, for example), add --foreground to the command string.

Handling running daemons

After it runs, I can type daemon --list to see the full list of named daemons running in case I forget.

If I wish to stop or restart a named daemon, I’ll use daemon --stop --name [NAME] or daemon --restart --name [NAME] (Note the lack of equals sign in those commands!).

You can also check if a named daemon is running by daemon --running --name [NAME] and then evaluating the exit code. For example,

daemon --running --name fifo; echo $? returns 0, because it’s running. The program will return 1 if the named daemon does not exist.

There are more options – setting which user to run as, or which directory to run the script in, and more, but this covers your basic use case for when you’re simply running a program in the background. One important note – because daemon runs the program in a subshell, the $PID value may be different than expected if the script checks its own PID. (My mpdq script has this problem; I’m bundling that in with some other tweaks to be uploaded soon.)

Overall, daemon is a utility I wish I’d learned about years ago, and hope that this quick guide has helped someone else find this useful program and made their life easier! You can get daemon at

Featured Photo by Sai Kiran Anagani on Unsplash

Was this post helpful or insightful? Buy me a coffee here or here and share this post with others!

Popular posts:

  • The difference between boundaries and rules
  • Two Ways to get CMYK Separation Using GIMP Instead of Photoshop in 2022
  • Weekend Project: Whole House and Streaming Audio for Free with MPD
  • If there's one Nazi (or a racist) at the table...
  • Word Porn Quotes
  • Odds and Ends: Optimizing SSHFS, moving files into subdirectories, and getting placeholder images

Recent Posts