2

I have written a systemd service.

When I run systemctl start foo.service, the service starts and runs correctly, but systemctl does not return immediately to the shell.

After a while it exits with

Job for foo.service failed because a timeout was exceeded.

and then the service is no longer running.

Here is foo.service, I RTFM and used Type=forking with a command which forks:

[Unit]
Description=blah blah

[Service]
Type=forking
PIDFile=/var/run/myservice.pid
ExecStart=/my/path/fork my-script my-args

[Install]
WantedBy=multi-user.target

my-script does not itself fork, so /my/path/fork is as follows

#!/bin/bash
PATH="$PATH:/my/path" "$@" &

Why does this service not fork and run in the background as expected? I have done everything as root on CentOS.

  • 1
    Why are you trying to do this? If the service doesn't fork, you can just use Type=simple and get on with your life. No goofy shell scripts needed. – Michael Hampton Dec 29 '16 at 1:18
  • 1
    @MichaelHampton I tried Type=simple, the systemctl start foo command doesn't return me to the shell. – spraff Dec 29 '16 at 12:22
  • That sounds quite strange. What is foo, then? – Michael Hampton Dec 29 '16 at 19:45
  • It's the placeholder for actual-name-of-my-service – spraff Dec 30 '16 at 17:49
  • Yes, I figured that out already. Now, what is it really? – Michael Hampton Dec 30 '16 at 17:52
2

Service Type=forking is for truly forking programs (apache for example). Your bash script only run another script on background. This is not exactly what systemd wants as forking program. Why you using

Type=forking

, when your script my-script is normal simple service?

Try to change service type and run your script directly with this:

[Service]
Type=simple
PIDFile=/var/run/myservice.pid
ExecStart=/my/path/my-script my-args

And remove whole /my/path/fork file. I can't see any profit from it.

Also you can try to set TimeoutStartSec=1 option to service configuration file.

I'm using bash script containing infinite loop with this configuration:

[Service]
User=root
ExecStart=/root/ha.sh
WorkingDirectory=/root/
KillSignal=SIGKILL
Restart=on-abort
Type=simple
TimeoutStartSec=1
TimeoutStopSec=1
StandardError=syslog
NotifyAccess=all

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.