36

I'm new to systemd after upgrading to 16.04 and I'm encountering a problem with starting and restarting services. When I run (for example)...

systemctl start djalbat.com

...it seems to work, however I don't get the prompt back, it just appears to hang. If I ctrl-c to get the prompt back and then test whether the service has started, it appears to have done so. I wonder what there is in the configuration that would cause this to happen? Here it is:

[Unit]
Description=djalbat.com


[Service]
Type=forking
WorkingDirectory=/var/www/djalbat.com/
ExecStart=/usr/bin/node ./bin/main.js start 2>&1 >> /var/log/djalbat.com.log


[Install]
WantedBy=multi-user.target

Also, if someone could point out the need for the last WantedBy directive, that would be appreciated.

5
  • 3
    It might be because the service is configured to fork but actually it does not fork. What happens when you execute the command from ExecStart in a terminal? Does it put itself into the background? If not, use Type=simple.
    – Thomas
    Commented Feb 10, 2018 at 13:45
  • Let me try that. I put in that directive, much like the WantedBy directive, simply because I'd seen it so many times in sample configurations. Commented Feb 10, 2018 at 14:17
  • That worked, many thanks! If you have a moment, please make your comment into an answer. Then I can accept it, which might help others coming here. Thanks again! Commented Feb 10, 2018 at 14:20
  • Not your question and besides the point but you should not redirect to file and let the journal system handle it. What you have there could run you into storage issues
    – nhed
    Commented Mar 4, 2021 at 19:11
  • I have a similarly hanging unit, but it is Type=oneshot. Should I change this to simple too? Not sure why it would cause it to hang...
    – wpcarro
    Commented Jun 25, 2022 at 22:53

1 Answer 1

35

So it turned out that the command that is executed with the ExecStart configuration did not fork whereas the systemd service was configured for a forking executable. This lead systemctl to wait for the for of the executable leading to a not returning command line.

The correct configuration for an executable that does not fork is to use Type=simple.

[Unit]
Description=djalbat.com

[Service]
Type=simple
WorkingDirectory=/var/www/djalbat.com/
ExecStart=/usr/bin/node ./bin/main.js start 2>&1 >> /var/log/djalbat.com.log

[Install]
WantedBy=multi-user.target

The WantedBy is needed to connect this unit with a target, so this unit or service is started automatically when the appropriate target is reached and the service is enabled to start automatically with

systemctl enable djalbat

Don't forget to refresh systemd after you have made changes to your service files with

systemctl daemon-reload
2
  • Then what does type=notify mean?
    – djangofan
    Commented Mar 28, 2020 at 0:49
  • @djangofan type=notify is for services that make use of sd_notify API to notify systemd about various state changes (e.g. whether it is ready, or is waiting for something, etc.).
    – Hi-Angel
    Commented Nov 30, 2020 at 14:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .