Using Debian 9 stable, I want to start a custom shell script before starting NGINX processes and shorewall firewall:

  • Do some init work
  • Mount a directory (overlayfs) to overlay /etc with NGINX configuration, shorewall configuration and /etc/hosts
  • The script also ends with sync, not sure if it's a good idea

systemctl list-dependencies
default.target
● ├─display-manager.service
● ├─systemd-update-utmp-runlevel.service
● └─multi-user.target
● ├─console-setup.service
● ├─cron.service
● ├─dbus.service
● ├─dropbear.service
● ├─myservice.service   <-- My service (link created with systemctl enable)
● ├─networking.service
● ├─nginx.service       <-- To be executed after myservice
[...]
● ├─basic.target
● │ ├─-.mount
● │ ├─myservice.service <-- My service (link created with systemctl enable)
● │ ├─shorewall.service  <-- To be executed after myservice

myservice.service ATTEMPT 1

[Unit]
Description=My startup service
Requires=shorewall.service nginx.service
Before=shorewall.service nginx.service

[Service]
RemainAfterExit=yes
ExecStart=/usr/local/bin/myservice start
ExecStop=/usr/local/bin/myservice stop

[Install]
WantedBy=multi-user.target
WantedBy=basic.target

The logs:

journalctl [...]
Oct 12 11:31:43 server-dev nginx[448]: nginx: [emerg] host not found in upstream "server-dev.com" in /etc/nginx/sites-enabled/default:33
Oct 12 11:31:43 server-dev nginx[448]: nginx: configuration file /etc/nginx/nginx.conf test failed <== NGINX: BAD
Oct 12 11:31:43 server-dev systemd[1]: nginx.service: Control process exited, code=exited status=1
Oct 12 11:31:43 server-dev systemd[1]: Failed to start A high performance web server and a reverse proxy server.
Oct 12 11:31:43 server-dev systemd[1]: nginx.service: Unit entered failed state.
Oct 12 11:31:43 server-dev systemd[1]: nginx.service: Failed with result 'exit-code'.
Oct 12 11:31:43 server-dev systemd[1]: Reached target Multi-User System.
Oct 12 11:31:43 server-dev systemd[1]: Reached target Graphical Interface.
Oct 12 11:31:43 server-dev systemd[1]: Starting Update UTMP about System Runlevel Changes...
Oct 12 11:31:43 server-dev systemd[1]: Started Update UTMP about System Runlevel Changes.
Oct 12 11:31:43 server-dev server[423]: DO: server start DONE <== END OF SCRIPT myservice
Oct 12 11:31:43 server-dev shorewall[449]: Compiling using Shorewall 5.0.15.6... <== SHOREWALL: GOOD
Oct 12 11:31:44 server-dev shorewall[449]: Processing /etc/shorewall/shorewall.conf...
Oct 12 11:31:44 server-dev shorewall[449]: Loading Modules...

Shorewall is systematically started correctly, after the execution of myservice. Nginx is most of the time started during the execution of myservice, before /etc is correctly overlayed (overlaid?), and therefore it fails to initialize properly.

myservice.service ATTEMPT 2

I also tried to change the

[Install]
WantedBy=default.target

And change

[Unit]
Before=multi-user.target

It also does not work.

How can I ensure that nginx and shorewall start after the execution of myservice?

up vote 1 down vote accepted

If you don't specify the type of your systemd service, it defaults to Type=simple. This means the service is considered started at the moment its ExecStart command is started. You probably want to use Type=oneshot instead, which waits for the ExecStart command to exit before considering the service started. See man systemd.service for further details.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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