How to implement a Linux daemon in bash that can be start/stop by `sudo service`?
Ralf Mardorf
silver.bullet at zoho.com
Sun Feb 18 18:09:11 UTC 2018
On Sun, 11 Feb 2018 20:51:49 -0500 (EST), Robert Heller wrote:
>At Sun, 11 Feb 2018 18:16:02 -0600 "Ubuntu user technical support,
>not for general discussions" <ubuntu-users at lists.ubuntu.com> wrote:
>
>>
>> Hi,
>>
>> There are a lot of resource about creating Linux daemon. But I am not
>> sure which is one is the most correct and provide the essential
>> things for a Linux daemon in bash that can be start/stop by `sudo
>> service`? Would anybody point me the best resource to learn how to
>> create such a daemon? Thanks.
>
>There are actually two parts to this.
>
>One part is the daemon itself. Nomially, this is just a program that
>does something in an endless loop, eg it could be waiting for network
>and/or UNIX socket connections and processing them. Or it could be
>doing some periodic task. In any case, it needs to do this "quietly"
>-- eg without doing I/O to std in, out, or err (unless stderr is your
>log file). There are some "rules" and tips about closing off
>stdin/stdout/stderr and "fading" into the background (disassociating
>itself from a controlling terminal and becoming its own process group
>head, etc.).
>
>The second part has to do with what flavor of /bin/init is in use:
>SysV, Upstart, or SystemD. Each of these has a different mechanism for
>dealing with getting deamons started or stopped during boot up and
>shutdown. There are man pages for that.
>
>One of the best resources is to just pick a favored deamon see how
>someone else did it. Find the source package and, to misquote Obiwan
>Kenobe, "Use the Source, Luke"... :-) In many cases you can copy
>someone else's start/stop script (or whatever) and make a small number
>of changes and there you are.
[root at moonstudio ~]# dpkg-query -L smartmontools | grep service
/lib/systemd/system/smartd.service
/lib/systemd/system/smartmontools.service
[root at moonstudio ~]# cat /lib/systemd/system/smartd.service
[Unit]
Description=Self Monitoring and Reporting Technology (SMART) Daemon
Documentation=man:smartd(8) man:smartd.conf(5)
[Service]
EnvironmentFile=-/etc/default/smartmontools
ExecStart=/usr/sbin/smartd -n $smartd_opts
ExecReload=/bin/kill -HUP $MAINPID
StandardOutput=syslog
[Install]
WantedBy=multi-user.target
[root at moonstudio ~]#
More information about the ubuntu-users
mailing list