DevHeads.net

How to implement a Linux daemon in bash that can be start/stop by `sudo service`?

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.

Comments

Re: How to implement a Linux daemon in bash that can be start/st

By Colin Law at 02/12/2018 - 04:59

On 12 February 2018 at 00:16, Peng Yu <pengyu. ... at gmail dot com> wrote:
On recent Ubuntu systems systemd is used to control such things. It is
easy to write systemd scripts unless the requirements for you daemon
are complex. Google for systemd script tutorials to get you going.

Colin

Re: How to implement a Linux daemon in bash that can be start/st

By silver.bullet at 02/12/2018 - 05:32

On Mon, 12 Feb 2018 08:59:36 +0000, Colin Law wrote:
IOW unless the script isn't a mission to Mars script, chances are good
that somebody subscribed to this list could help you, if you would be
more precise about what you want to achieve, assuming you already
migrated to systemd. If not, don't even consider to write an upstart
script, migrate to systemd before writing the script, since sysvinit,
upstart and Co. are tem­pi pas­sa­ti.

Re: How to implement a Linux daemon in bash that can be start/st

By silver.bullet at 02/12/2018 - 05:18

On Mon, 12 Feb 2018 08:59:36 +0000, Colin Law wrote:
Or else write a script and don't care about the init system at all and
when you finished the script, read how to edit a systemd unit. Instead
of learning the rocket sience explained by e.g.
<a href="https://www.freedesktop.org/software/systemd/man/systemd.service.html" title="https://www.freedesktop.org/software/systemd/man/systemd.service.html">https://www.freedesktop.org/software/systemd/man/systemd.service.html</a>
<a href="https://www.freedesktop.org/software/systemd/man/systemd.unit.html" title="https://www.freedesktop.org/software/systemd/man/systemd.unit.html">https://www.freedesktop.org/software/systemd/man/systemd.unit.html</a>
consider to take a look at the systemd units provided by packages, doing
something similar as your scrip.

Re: How to implement a Linux daemon in bash that can be start/st

By Robert Heller at 02/11/2018 - 21:51

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.

Re: How to implement a Linux daemon in bash that can be start/st

By silver.bullet at 02/18/2018 - 14:09

On Sun, 11 Feb 2018 20:51:49 -0500 (EST), Robert Heller wrote:
[root@moonstudio ~]# dpkg-query -L smartmontools | grep service
/lib/systemd/system/smartd.service
/lib/systemd/system/smartmontools.service
[root@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@moonstudio ~]#

Re: How to implement a Linux daemon in bash that can be start/st

By silver.bullet at 02/18/2018 - 14:22

On Sun, 18 Feb 2018 19:09:11 +0100, Ralf Mardorf wrote:
Given that the OP also requested "Any other alternative to at" we
seriously should ignore the OP unless the OP describes what the OP
want's to get. If we just give hints, without knowing what the OP
want's to reach, we probably need to help the OP to handle a glorious
mess, which could be avoided in the first place.

Re: How to implement a Linux daemon in bash that can be start/st

By Peng Yu at 02/18/2018 - 13:33

On Sun, Feb 11, 2018 at 7:51 PM, Robert Heller < ... at deepsoft dot com> wrote:
Can anybody provide a minimal working example so that I learn about
it? Not that the example must be working, thus a man page is not what
I am looking for. It must be minimal, thus any application-specific
(except some simple code for demonstrator purpose) should not be
excluded. Only code related to the above two points should be
maintained.

Re: How to implement a Linux daemon in bash that can be start/st

By silver.bullet at 02/18/2018 - 13:52

On Sun, 18 Feb 2018 11:33:15 -0600, Peng Yu wrote:
Much likely most subscribers to this list understand that manpages
don't help for several purposes. However, all your requests I read back
to 2016, I might not have read all of them, suffer from not explaining
what you want to accomplish.

To start or stop something when using upstart or systemd isn't an
issue, yes, I'm willing to give you an example, if you are willing to
post your "daemon in bash", and most important:

Are you using upstart or systemd?

Re: How to implement a Linux daemon in bash that can be start/st

By Colin Law at 02/18/2018 - 13:50

Googling for
example systemd service
yields this as the first hit for me
<a href="https://www.devdungeon.com/content/creating-systemd-service-files" title="https://www.devdungeon.com/content/creating-systemd-service-files">https://www.devdungeon.com/content/creating-systemd-service-files</a>

There are other examples in the hits there.

Colin