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

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Peng Yu
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.

--
Regards,
Peng

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

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

Robert Heller
At Sun, 11 Feb 2018 18:16:02 -0600 "Ubuntu user technical support,  not for general discussions" <[hidden email]> 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.


>

--
Robert Heller             -- 978-544-6933
Deepwoods Software        -- Custom Software Services
http://www.deepsoft.com/  -- Linux Administration Services
[hidden email]       -- Webhosting Services
                                                                                                                     

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

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

Colin Law-2
In reply to this post by Peng Yu
On 12 February 2018 at 00:16, Peng Yu <[hidden email]> 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.

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

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

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

Ralf Mardorf-5
On Mon, 12 Feb 2018 08:59:36 +0000, Colin Law wrote:

>On 12 February 2018 at 00:16, Peng Yu <[hidden email]> 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.  
>
>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.

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.
https://www.freedesktop.org/software/systemd/man/systemd.service.html
https://www.freedesktop.org/software/systemd/man/systemd.unit.html
consider to take a look at the systemd units provided by packages, doing
something similar as your scrip.


--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

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

Ralf Mardorf-5
In reply to this post by Colin Law-2
On Mon, 12 Feb 2018 08:59:36 +0000, Colin Law wrote:
>unless the requirements for you daemon are complex

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.


--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

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

Peng Yu
In reply to this post by Robert Heller
On Sun, Feb 11, 2018 at 7:51 PM, Robert Heller <[hidden email]> wrote:

> At Sun, 11 Feb 2018 18:16:02 -0600 "Ubuntu user technical support,  not for general discussions" <[hidden email]> 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

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

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

Colin Law-2
On 18 February 2018 at 17:33, Peng Yu <[hidden email]> 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.

Googling for
example systemd service
yields this as the first hit for me

There are other examples in the hits there.

Colin
 

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

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

Ralf Mardorf-5
In reply to this post by Peng Yu
On Sun, 18 Feb 2018 11:33:15 -0600, Peng Yu wrote:
>Can anybody provide a minimal working example so that I learn about
>it? [snip] a man page is not what I am looking for.

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?


--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

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

Ralf Mardorf-5
In reply to this post by Robert Heller
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" <[hidden email]> 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@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 ~]#


--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

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

Ralf Mardorf-5
On Sun, 18 Feb 2018 19:09:11 +0100, Ralf Mardorf wrote:
>On Sun, 11 Feb 2018 20:51:49 -0500 (EST), Robert Heller wrote:
>>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.

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.


--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users