Discussion:
[systemd-devel] a little help with $MAINPID please
lux-integ
2013-02-23 18:01:18 UTC
Permalink
Greetings

I am an absolute beginner with systemd. I came across the gentoo systemd
script
( http://en.gentoo-wiki.com/wiki/Systemd )
for mysql-daemon. It has these lines:-

ExecStart=/usr/sbin/mysqld --pid-file=/var/run/mysqld/mysqld.pid
ExecStop=/bin/kill -15 $MAINPID
PIDFile=/var/run/mysqld/mysqld.pid

I am curious and puzzled about two things.
a) What is $MAINPID which I have seen in so many systemd scripts
i.e. where is is set? or how is it determined?
b) what are the advantages or disadvantaes of the following line
ExecStop=/bin/kill -15 $(/bin/pidof mysqld)
over
ExecStop=/bin/kill -15 $MAINPID


thanks in anticipation
regards
lluxInteg
Reindl Harald
2013-02-23 17:58:06 UTC
Permalink
Post by lux-integ
Greetings
I am an absolute beginner with systemd. I came across the gentoo systemd
script
( http://en.gentoo-wiki.com/wiki/Systemd )
for mysql-daemon. It has these lines:-
ExecStart=/usr/sbin/mysqld --pid-file=/var/run/mysqld/mysqld.pid
ExecStop=/bin/kill -15 $MAINPID
PIDFile=/var/run/mysqld/mysqld.pid
I am curious and puzzled about two things.
a) What is $MAINPID which I have seen in so many systemd scripts
i.e. where is is set? or how is it determined?
b) what are the advantages or disadvantaes of the following line
ExecStop=/bin/kill -15 $(/bin/pidof mysqld)
over
ExecStop=/bin/kill -15 $MAINPID
systemd simply knows the PID because it has started
the service and crontrols it - why would you want to
use external commands if the init-system has all needed
informations because it is it's job to have them
Reindl Harald
2013-02-23 18:00:42 UTC
Permalink
Post by lux-integ
b) what are the advantages or disadvantaes of the following line
ExecStop=/bin/kill -15 $(/bin/pidof mysqld)
over
ExecStop=/bin/kill -15 $MAINPID
and BTW you do not need any ExecStop in such cases

15 = SIGTERM = default and at "service stop" systemd
sends SIGTERM to all processes of the cgroup
Mantas Mikulėnas
2013-02-23 18:13:51 UTC
Permalink
Post by lux-integ
Greetings
I am an absolute beginner with systemd. I came across the gentoo systemd
script
( http://en.gentoo-wiki.com/wiki/Systemd )
for mysql-daemon. It has these lines:-
ExecStart=/usr/sbin/mysqld --pid-file=/var/run/mysqld/mysqld.pid
ExecStop=/bin/kill -15 $MAINPID
PIDFile=/var/run/mysqld/mysqld.pid
I am curious and puzzled about two things.
a) What is $MAINPID which I have seen in so many systemd scripts
i.e. where is is set? or how is it determined?
MAINPID is determined by systemd.

- For Type=simple/dbus/notify, it's usually the first process started.

- For Type=forking, systemd can guess based on the cgroup's contents –
each service runs in a separate cgroup (for example, run
"systemd-cgls") and systemd can use this information to find the right
process.

- But if PIDFile is specified, then systemd doesn't try to guess but
just uses the pidfile written by mysqld itself.
Post by lux-integ
b) what are the advantages or disadvantaes of the following line
ExecStop=/bin/kill -15 $(/bin/pidof mysqld)
over
ExecStop=/bin/kill -15 $MAINPID
The former will simply not work, as Exec lines are not run through a
shell and $() is not interpreted in them. Even if it worked, it would
be unreliable as there can be more than one mysqld process. (For
example, on a desktop system, one copy of mysqld can be running as a
system service, and a second copy – in the user's login session, as
KDE uses MySQL as a storage backend. In this case, you would simply
kill the *wrong process*.)

The latter works, but is redundant, since sending SIGTERM is already
what systemd does by default if ExecStop is not set. (You can change
the signal by setting KillSignal.)
--
Mantas Mikulėnas <***@gmail.com>
Mantas Mikulėnas
2013-02-23 18:15:28 UTC
Permalink
Post by Mantas Mikulėnas
Post by lux-integ
b) what are the advantages or disadvantaes of the following line
ExecStop=/bin/kill -15 $(/bin/pidof mysqld)
over
ExecStop=/bin/kill -15 $MAINPID
The former will simply not work, as Exec lines are not run through a
shell and $() is not interpreted in them. Even if it worked, it would
be unreliable as there can be more than one mysqld process. (For
example, on a desktop system, one copy of mysqld can be running as a
system service, and a second copy – in the user's login session, as
KDE uses MySQL as a storage backend. In this case, you would simply
kill the *wrong process*.)
The latter works, but is redundant, since sending SIGTERM is already
what systemd does by default if ExecStop is not set. (You can change
the signal by setting KillSignal.)
In addition, systemd kills *all* processes in the cgroup by default,
so you can be sure that you won't have leftover "helper" processes
left (which various services often use). Plain /bin/kill won't
guarantee that.
--
Mantas Mikulėnas <***@gmail.com>
Continue reading on narkive:
Loading...