[systemd-devel] a little help with $MAINPID please

Mantas Mikulėnas grawity at gmail.com
Sat Feb 23 10:13:51 PST 2013


On Sat, Feb 23, 2013 at 8:01 PM, lux-integ <lux-integ at btconnect.com> wrote:
> 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.

> 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 <grawity at gmail.com>


More information about the systemd-devel mailing list