"Proper" way to run shell script as a daemon
Solution 1
Using systemd you should be able to run a script as a daemon by creating a simple unit. There are a lot of different options you can add but this is about as simple as you can get.
Say you have a script /usr/bin/mydaemon
.
#!/bin/sh
while true; do
date;
sleep 60;
done
Don't forget to sudo chmod +x /usr/bin/mydaemon
.
You create a unit /etc/systemd/system/mydaemon.service
.
[Unit]
Description=My daemon
[Service]
ExecStart=/usr/bin/mydaemon
Restart=on-failure
[Install]
WantedBy=multi-user.target
To start the demon you run
systemctl start mydaemon.service
To start at boot you enable it
systemctl enable mydaemon.service
If on a systemd based system, which a majority of Linux distributions are today, this isn't really an external tool. The negative would be that it won't work everywhere though.
Solution 2
I am probably missing something here; why exactly wouldn't nohup
be appropriate? Of course it's not enough alone, but supplementing it seems straightforward.
#!/bin/bash
if [ "$1" = "DAEMON" ]; then
# is this necessary? Add other signals at will (TTIN TTOU INT STOP TSTP)
trap '' INT
cd /tmp
shift
### daemonized section ######
for i in $( seq 1 10 ); do
date
sleep 5
done
#### end of daemonized section ####
exit 0
fi
export PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin
umask 022
# You can add nice and ionice before nohup but they might not be installed
nohup setsid $0 DAEMON $* 2>/var/log/mydaemon.err >/var/log/mydaemon.log &
As far as I can see:
- the output is appropriately redirected (use /dev/null if necessary)
- the umask is inherited
-
stdin
dies at the end of the parent script anyway - the daemon.sh script is reparented to
init
(orsystemd
)
I have a strong feeling I'm missing the obvious. Downvote, but please tell me what it is :-)
Solution 3
The Linux screen
command contained in most distros can daemonize a shell script. I use it often. Here's a quick example to start, list, and quit a detached screen session...
# screen -dmS Session_Name bash -c "while true; do date; sleep 60; done"
# screen -ls
There are screens on:
8534.Session_Name (04/04/2018 08:46:27 PM) (Detached)
# screen -S Session_Name -X quit
user339676
Updated on September 18, 2022Comments
-
user339676 over 1 year
I am writing a shell script that I would like to run as a daemon on startup without using external tools like daemontools or daemonize.
Linux Daemon Writing HOWTO
According to the Linux Daemon Writing HOWTO, a proper daemon has the following characteristics:
- forks from the parent process
-
closes all file descriptors (i.e.,
stdin
,stdout
,stderr
) - opens logs for writing (if configured)
- changes the working directory to one that is persistent (usually
/
) - resets the file mode mask (umask)
- creates an unique Session ID (SID)
daemonize Introduction
The daemonize Introduction goes further, stating that a typical daemon also:
- disassociates from its control terminal (if there is one) and ignores all terminal signals
- disassociates from its process group
- handles
SIGCLD
How would I do all this in a
sh
,dash
, orbash
script with common Linux tools only?The script should be able to run on as many distros as possible without additional software, although Debian is our primary focus.
NOTE: I know there are plenty of answers on the StackExchange network recommending the use of
nohup
orsetsid
, but neither of these methods tackles all of the requirements above.
EDIT: The daemon(7) manpage also gives some pointers, although there seem to be some differences between older-style
SysV
daemons and newersystemd
ones. Since compatibility with a variety of distros is important, please ensure the answer makes clear any differences.
-
Rich about 6 yearsThe "proper" way to craft your own shell script is to make it do its own logging, provide a method for launching it as a daemon, etc. Things like
daemon
and those other things are for running arbitrary shell scripts with no provision for running as a daemon. Since you're the author, fully in control of how that script is written, make it so it can just be launched from a systemd unitfile or rc.d script. You did specify "Proper"!
-
Cristian Ciupitu about 6 yearsWhile I like the systemd approach, the OP said "no external tools". There are Linux distributions which don't have systemd yet, or let you use choose between systemd and something else, e.g. OpenRC.
-
111--- about 6 yearsI was about to suggest something very similar. I use
nohup
with&
and I/O redirection to launch several non -C
daemon utilities with perhaps the added security of wrapping yournohup
command inside of asu -c "nohup ... &" -s /bin/bash systemUser
to run the daemon as a non-privileged user. -
Yurij Goncharuk about 6 years
screen
doesn't deamonize shell script. It just run they in distinguished terminal and can detach from this terminal (like unplug keyboard from PC) without closing session. So, the program running in detached terminal is running in background. Therefore - detach pass program in background. -
Alexander about 6 yearsFor distros that use systemd,
systemd
is no more of an "external tool" thanbash
. -
Roberto Manfreda about 4 yearsUsing that example unit didn't work for me (receiving a 203/EXEC failue). I added /bin/sh before the ExecStart path
-
roaima almost 4 years@RobertoManfreda the script must be executable (see the
chmod
part of this answer)