7

I want systemd service to handle forking (my file doesn't handle forking by itself. So I'm relying on systemd for handling that)

My .service file:

[Unit]
Description=swamp services management service
After=syslog.target

[Service]
Type=forking
ExecStart=/usr/bin/swamp

[Install]
WantedBy=multi-user.target

Question

Is specifying Type=forking enough for what I'm trying to achieve? Or is it similar to expect forking in upstart which actually tells upstart (if I understand correctly, not sure I do, I'm new at writing initscripts) that my service would handle forking/daemonizing.

13

systemd has excellent documentation. See the page on service files:

If set to forking, it is expected that the process configured with ExecStart= will call fork() as part of its start-up. The parent process is expected to exit when start-up is complete and all communication channels are set up. The child continues to run as the main daemon process. This is the behavior of traditional UNIX daemons. If this setting is used, it is recommended to also use the PIDFile= option, so that systemd can identify the main process of the daemon. systemd will proceed with starting follow-up units as soon as the parent process exits.

So, using that type will just tell systemd to wait until swamp returns and then consider it to be still running: making that happen remains your responsibility...

  • 4
    If the script doesn't do any forking, it's either oneshot (a typical short running script) or simple (a long-running service that that doesn't fork+exit). – Pavel Šimerda Apr 26 '14 at 17:32
  • 2
    Would you consider a script (Bash?) to fork another process when it backgrounds using ampersand &? – Felipe Alvarez Jan 16 '17 at 0:57
  • 3
    @FelipeAlvarez yes, the ampersand operator in bash is equivalent to a fork and exec. – Thayne Dec 21 '17 at 21:51
0

If you want systemd to handle forking, then you should use e.g. Type=simple or Type=notify. Then systemd will do the forking for you.

If you can modify the swamp executable, then Type=notify is the best solution. The executable should then notify systemd when it is successfully initialized. This means that systemctl start swamp will wait for swamp to initialize when called from the command line, and print any error message while initializing to the command line, which is what you usually want.

For an example of an executable written to use Type=notify, see the following shellscript:

#!/bin/bash                                                                     
sleep 3
systemd-notify READY=1
sleep 1000000

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.