3

I have the following file in /etc/systemd/system/mytest.service

[Unit]
Description=My Test Service
After=network.target

[Service]
Type=forking
User=mytestuser
Group=mytestuser
WorkingDirectory=/opt/mytest

ExecStart=/bin/echo "My Test Start!"

ExecStop=/bin/echo "My Test Stop!"

ExecReload=/bin/echo "My Test Restart!"

[Install]
WantedBy=multi-user.target

After running systemctl daemon-reload and systemctl start mytest the following output can be seen from journalctl -xe

Jun 29 09:44:57 localhost.localdomain systemd[1]: Starting My Test Service...
-- Subject: Unit mytest.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit mytest.service has begun starting up.
Jun 29 09:44:57 localhost.localdomain echo[4370]: My Test Start!
Jun 29 09:44:57 localhost.localdomain echo[4372]: My Test Stop!
Jun 29 09:44:57 localhost.localdomain polkitd[619]: Unregistered Authentication Agent for unix-process:4364:694445 (system bus name :1.149, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale en_GB.UTF-8) (disconnected from
Jun 29 09:44:57 localhost.localdomain systemd[1]: Started My Test Service.
-- Subject: Unit mytest.service has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit mytest.service has finished starting up.
--
-- The start-up result is done.

This shows the ExecStart command running, immediately followed by the ExecStop, why is this happening? Is there something missing from my .service file?

Thanks

1 Answer 1

3

The problem is that echo isn't forking - no child processes are left behind. What you need is a oneshot service, with RemainAfterExit=true. If you just change it to oneshot, you'll get the exact same behavior. The RemainAfterExit part tells it that even after the ExecStart exits, the service should still be considered running, so it should not run the ExecStop(s).

You must log in to answer this question.

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