[systemd-devel] Problems trying to convert a System-V-Init script to systemd

Andrei Borzenkov arvidjaar at gmail.com
Thu Jul 14 13:49:26 UTC 2016


14.07.2016 15:34, Holger Kiehl пишет:
> Hello,
> 
> I am new to systemd and the maintainer of the file distribution software
> AFD (http://www.dwd.de/AFD) and I am trying to adapt this application
> to systemd. The problem I am unable to solve is that doing a reboot,
> poweroff or halt, all process get a SIGTERM before systemd calls the
> command supplied by ExecStop. If I do a 'systemctl stop afd.service'
> everything works as expected. I have searched the web for a solution
> and have tried all the different service Type=, unit After=/Before=
> combination, but failed so far. I must be doing something obviously
> wrong, but unable to see what and need help please.
> 
> The service/unit file looks as follows:
> 
>    [Unit]
>    Description=Automatic File Distributor
>    After=basic.target
> 

This is redundant - it is default for any standard service

>    [Service]
>    RemainAfterExit=yes
>    Type=oneshot

That's wrong (although it is unrelated to the problem you have). It
means systemd expects ExecStart to run and finish. In your case it
appears to work because you use RemainAfterExit, but if you look more
closely, your service is in state where PID is exited.

>    ExecStart=-/etc/init.d/afd start
>    ExecStop=-/etc/init.d/afd stop
>    KillMode=none

Well, in this case you rely on your service to behave well. You must be
very confident :)

>    StandardOutput=syslog+console
>    StandardError=syslog+console
> 
>    [Install]
>    WantedBy=multi-user.target
> 
> /etc/init.d/afd is a shell script that starts one or more instances of

If you start multiple instances, you should consider service template so
each instance can be represented as exactly one systemd service.

> the AFD under different users. The users are configured in /etc/sysconfig/afd
> and are started via the following command: su - $afduser -c "<cmd start/sop AFD>"

so what most likely happens, is - su establishes new login session; on
shutdown these sessions are cleaned up (concurrently with any running
service - unfortunately, there is no way to express dependency between
user and system systemd instances). Here is where you get your signals.

Check with loginctl after service is started - do you see and extra seesion?

> To speed things up a bit the script forks for each user to start/stop the
> AFD instances. The script /etc/init.d/afd is just initiating thinks, but
> always waits for the command to complete. AFD itself has an init process
> (init_afd) that then starts several other process and monitors them.
> 
> In /etc/init.d/afd I have added the following line in the beginning, before
> it wants to do the shutdown:
> 
>    echo "Before: ps -u afd: `ps -u $afduser`" >> /var/log/afd.log
> 
> And this then shows the following:
> 
>    Before: ps -u afd:   PID TTY          TIME CMD
>     1258 ?        00:00:00 init_afd
>     1260 ?        00:00:00 system_log <defunct>
>     1261 ?        00:00:00 event_log <defunct>
>     1262 ?        00:00:00 receive_log <defunct>
>     1263 ?        00:00:00 transfer_log <defunct>
>     1264 ?        00:00:00 trans_db_log <defunct>
>     1265 ?        00:00:00 archive_watch <defunct>
>     1266 ?        00:00:00 input_log <defunct>
>     1267 ?        00:00:00 output_log <defunct>
>     1268 ?        00:00:00 delete_log <defunct>
>     1269 ?        00:00:00 production_log <defunct>
>     1270 ?        00:00:00 distribution_lo <defunct>
>     1271 ?        00:00:00 amg <defunct>
>     1272 ?        00:00:00 aldad <defunct>
>     1287 ?        00:00:00 afd_stat <defunct>
>     1288 ?        00:00:00 fd <defunct>
>     1289 ?        00:00:00 dir_check <defunct>
> 
> All the process have received a SIGTERM before (or during the execution)
> of the script.
> 
> Any idea what I am doing wrong,

Much :)

> or what else I can try to do a proper
> shutdown of this application under systemd?
>

Use native systemd unit, do not use init scripts. Use User= to
impersonate user, use Type=forking and PIDFile= to proper synchronize
service startup, use templates to run multiple instances.


More information about the systemd-devel mailing list