[systemd-devel] systemd unit to launch perl script/service launches the script but doesn't exit the unit

Mantas Mikulėnas grawity at gmail.com
Fri Nov 29 18:02:53 PST 2013


On Fri, Nov 29, 2013 at 10:03 PM,  <jen142 at promessage.com> wrote:
> I'm trying to launch a perl service using systemd.
>
> The perl script is 'smtpprox'
> (https://github.com/jnorell/smtpprox/blob/master/smtpprox)
>
> The unit file I created is
>
>         cat /etc/systemd/system/smtpprox.service
>                 [Unit]
>                 Description=smtpprox
>                 Requires=var-run.mount
>                 After=var-run.mount

It should be never necessary to add dependencies on /var/run, as
systemd always mounts a tmpfs on /run before starting any service at
all, and /var/run should just be a symlink to that.

>
>                 [Service]
>                 Type=oneshot
>                 KeepAfterExit=true

Should be Type=simple (see below).

>                 PrivateTmp=false

PrivateTmp is already off by default.

>                 ExecStart=/usr/local/etc/postfix/filters/smtpprox
>                 127.0.0.1:10025 127.0.0.1:10026
>                 ExecStop=kill `pidof perl`

ExecStop is not needed (see below).

>
>                 [Install]
>                 WantedBy=multi-user.target
>
> When I exec
>
>         systemctl daemon-reload
>         systemctl start smtpprox.service
>
> It does start the service
>
>         ps ax | grep smtpprox
>                 10835 pts/2    S+     0:00 systemctl start
>                 smtpprox-custom.service
>                 10837 ?        Ss     0:00 /usr/bin/perl -w
>                 /usr/local/etc/postfix/filters/smtpprox --children=4
>                 127.0.0.1:10025 127.0.0.1:10026
>                 10838 ?        S      0:00 /usr/bin/perl -w
>                 /usr/local/etc/postfix/filters/smtpprox --children=4
>                 127.0.0.1:10025 127.0.0.1:10026
>                 10839 ?        S      0:00 /usr/bin/perl -w
>                 /usr/local/etc/postfix/filters/smtpprox --children=4
>                 127.0.0.1:10025 127.0.0.1:10026
>                 10840 ?        S      0:00 /usr/bin/perl -w
>                 /usr/local/etc/postfix/filters/smtpprox --children=4
>                 127.0.0.1:10025 127.0.0.1:10026
>                 10841 ?        S      0:00 /usr/bin/perl -w
>                 /usr/local/etc/postfix/filters/smtpprox --children=4
>                 127.0.0.1:10025 127.0.0.1:10026
>
> but never exits/releases at shell
>
>         systemctl start smtpprox-custom.service
>                 ( ... just sits here ... )
>
> How do I get the unit to exec that script, leave the script running, and
> exit the properly?

Don't use Type=oneshot. It is only for short-lived "tool" services
that do something and quickly exit, so right now the 'start' action
hangs because systemd is waiting for the process to exit.

And smtpprox looks like it's a real daemon – when you start it, it
continues to run waiting for connections. For this, Type=oneshot is
wrong, and the default Type=simple should be right. (This also means
KeepAfterExit= can be removed.)

(Type=simple is for daemons that keep running in "foreground" when
started. Some daemons, when started, fork a "background" process and
have the original one exit; those would need Type=forking instead.)

>
> Also,  the
>
>         ExecStop=kill `pidof perl`
>
> works, but kills ALL running perl scripts/jobs -- not just the smtpprox
> parent/children.
>
> What's the right way to get systemd to find/kill only the pids related
> to smtpprox?

The right way is to not use ExecStop= at all. If no command is
specified for stopping the service, systemd just kills all processes
belonging to that service, using cgroups to find the correct
processes. (You can check `systemctl status smtpprox-custom.service`
for just one service, or `systemd-cgls` for all of them.)

-- 
Mantas Mikulėnas <grawity at gmail.com>


More information about the systemd-devel mailing list