How can I configure a systemd service to restart periodically?
Yes, you can make your service to restart it periodically by making your service of Type=notify
.
Add this option in [Service] section of your service file along with Restart=always
and give WatchdogSec=xx
, where xx is the time period in second you want to restart your service. Here your process will be killed by systemd after xx time period and will be restarted by systemd again.
for eg.
[Unit]
.
.
[Service]
Type=notify
.
.
WatchdogSec=10
Restart=always
.
.
[Install]
WantedBy= ....
systemd: automatically restart service after 24 hours?
Systemd has a built-in watchdog function, see this link for more info.
However you will have to patch your software to send out sd_notify events, so the watchdog knows that your software is still alive.
From 1:
First of all, to make software watchdog-supervisable it needs to be
patched to send out "I am alive" signals in regular intervals in its
event loop. Patching this is relatively easy. First, a daemon needs to
read the WATCHDOG_USEC= environment variable. If it is set, it will
contain the watchdog interval in usec formatted as ASCII text string,
as it is configured for the service. The daemon should then issue
sd_notify("WATCHDOG=1") calls every half of that interval. A daemon
patched this way should transparently support watchdog functionality
by checking whether the environment variable is set and honouring the
value it is set to.
An even more quick and dirty approach would be to set up a cron-job which kills the process every 24 hours and restarts your service.
How to use Systemd to restart a service when down?
If you are using a systemd service file to start your service, then add the lines below to your service file from where you are starting your service:
[Service]
Type=simple
ExecStart=here will be your service executable name
Restart=always
RestartSec=0
Restart=
Configures whether the service shall be restarted when the service process exits, is killed, or a timeout is reached. Takes one of the following values:
no
,on-success
,on-failure
,on-abnormal
,on-watchdog
,on-abort
oralways
. If set tono
(the default).RestartSec=
Configures the time to sleep before restarting a service (as configured with
Restart=
). Takes a unit-less value in seconds.
These two options have to be under the [Service]
tag in a service file.
How to automatically restart systemd service on failure?
Setting Restart=on-failure
to your service configuration should do it, but check Restart documentation for more options.
To send an email you could use an ExecStartPost=
clause with a mailx
call.
Automatically restart a service after unattended updates under systemd
Use Restart=always
if you want it to run at all times. When mysql
service update happens, this service does a clean stop
and therefore systemd
doesn't restart it. You have Restart=on-failure
set, which only restarts if the stop has a return code other than 0
.
Restart = always
RestartSec = 10
RestartSec
Configures the time to sleep before restarting a service (as configured with Restart=). Takes a unit-less value in seconds, or a time span value such as "5min 20s". Defaults to 100ms.
Do you know how to make the system automatically restart daemon service?
To respawn your service when it fails, add the following to the [Service]
block:
[Service]
Restart=on-failure
RestartSec=3
If you wish to always restart when your service is killed use Restart=always
The RestartSec
value is the delay between restart attempts.
See more info here: https://www.freedesktop.org/software/systemd/man/systemd.service.html
Related Topics
Add a Newline Only If It Doesn't Exist
Use Bash to Read a File and Then Execute a Command from the Words Extracted
How to Stop a Running R Command in Linux Other Than with Ctrl + C
Command to Get Time in Milliseconds
How to Use Aliased Commands with Xargs
Mount Remote Windows Share from Centos
Get Filesystem Mount Point in Kernel Module
Linux Rename Files to Uppercase
Given a Linux Username and a Password How to Test If It Is a Valid Account
How to Remove Dir Background in 'Ls -Color' Output
Connect to Host Localhost Port 22: Connection Refused
How to Parse Netstat Command in Order to Get Process Name and Pid from It
Differencebetween .Got and .Got.Plt Section