Search code examples
linuxsystemd

systemd After=nginx.service is not working


I am trying to setup a custom systemd service on my linux system and was experimenting with it
Following is my custom service, where it will trigger a bash file

[Unit]
Description=Example systemd service.
After=nginx.service

[Service]
Type=simple
ExecStart=/bin/bash /usr/bin/test_service.sh

[Install]
WantedBy=multi-user.target

Since I have mentioned After=nginx.service i was expecting nginx serivce to start automatically
So after starting the above service, i check the status of nginx, which has not started

However if i replace After with Wants it works
Can someone differenciate between After and Wants and when to use what?


Solution

  • Specifying After=foo tells systemd how to order the units if they are both started at the same time. It will not cause the foo unit to autostart.

    Use After=foo in combination with Wants=foo or Requires=foo to start foo if it's not already started and also to keep desired order of the units.

    So your [Unit] should include:

    [Unit]
    Description=Example systemd service.
    After=nginx.service
    Wants=nginx.service
    

    Difference between Wants and Requires:

    Wants= : This directive is similar to Requires= , but less strict. Systemd will attempt to start any units listed here when this unit is activated. If these units are not found or fail to start, the current unit will continue to function. This is the recommended way to configure most dependency relationships.