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

Peng Yu pengyu.ut at gmail.com
Sun Feb 18 17:33:15 UTC 2018


On Sun, Feb 11, 2018 at 7:51 PM, Robert Heller <heller at deepsoft.com> 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.

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.

-- 
Regards,
Peng




More information about the ubuntu-users mailing list