1

I want to add a feature to a project that auto starts a service.

My service file contains:

[Unit]
Description=example Server
After=network.target

[Service]
Type=idle
ExecStart=/opt/xxx/xxx/sample.sh   
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target

The steps I follow are:

  1. reload daemon: systemctl daemon-reload

  2. enable service: systemctl enable sample

  3. start service: systemctl start sample

  4. check status: service sample status, which shows my service is running...

I want to auto start the service if it crashes or stops.

When I stop my service using systemctl stop sample, it doesn't autostart. Any idea how to achive this?

  • Basically, you want systemctl stop to effectively not work with your service, is that right? What would happen when you really wanted to stop your service? – Stephen Kitt Jun 13 '19 at 10:09
  • @stephen kitt i cant understand what you mean by that. I use systemctl to stop the service (I'm doing this to see if the service will auto start) and the service stop successful. For example if i see the status of after the systemctl stop the service appeared as stopped but i start only if i do it manually (systemctl start) and no automated – Nikos Kalantas Jun 13 '19 at 10:18
  • Try to kill it in some other way. For example killall sample.sh and see if it restarts. – deimos Jun 13 '19 at 10:20
3

The Restart setting doesn’t work like that; quoting the manual,

When the death of the process is a result of systemd operation (e.g. service stop or restart), the service will not be restarted.

systemctl allows you to specify the state you want a service to be in: systemctl start specifies that it should be running, systemctl stop specifies that it shouldn’t. systemd then takes whatever steps are necessary to align the system state with what you’ve requested.

The intent of Restart is to maintain the requested system state, not to keep a service running in all circumstances.

If you want to verify that systemd will restart your service if it dies, you need to kill the service’s main process directly, not ask systemd to stop it:

systemctl kill sample
|improve this answer|||||
  • Or, better: systemctl kill sample.service, will deliver a signal to the main process of the service (not other random sample.sh running in the system), furthermore no reason to use SIGKILL unless the process is trapping SIGTERM and not terminating from it (in which case, you might want to understand why it does, as that's most likely a bug.) – filbranden Jun 13 '19 at 11:42
  • Thanks @filbranden, I’ve updated my answer with your suggestions. I tend to use -KILL for such tests because I have indeed come across daemons which trap -TERM... – Stephen Kitt Jun 13 '19 at 11:59
  • There's also --kill-who=all and --signal=SIGKILL that can be passed to systemctl kill in case that's really needed. Thanks for updating your answer! – filbranden Jun 13 '19 at 12:05

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.