0% found this document useful (0 votes)
35 views

Timed Systemd Service Restarts

This document describes how to use systemd to periodically restart a service without using cron. It explains how to create a timer that activates a one-shot service every two hours to restart the main service. The code provided creates a two-hour timer that runs a one-shot service every two hours, which executes a command to restart the target service. You can check the status of timers using the systemctl list-timers command.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Timed Systemd Service Restarts

This document describes how to use systemd to periodically restart a service without using cron. It explains how to create a timer that activates a one-shot service every two hours to restart the main service. The code provided creates a two-hour timer that runs a one-shot service every two hours, which executes a command to restart the target service. You can check the status of timers using the systemctl list-timers command.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Timed systemd service restarts

April 15, 2017

If you have ever wanted to do periodic restarts of a service, and for whatever reason you dont
want to use cron, you can use systemd to accomplish that task.

Systemd has numerous knobs and you can do this task in several different ways. Heres one that
I got to work for me:

Concept
Create a two hour timer which activates a target every two hours. Our target is a one-shot service
which restarts the main service.

Heres the code (gist):

two-hour.timer

[Unit]
Description=2hour timer

[Timer]
OnBootSec=0min
OnCalendar=0/2:00:00
Unit=two-hour.service

[Install]
WantedBy=basic.target

two-hour.service

[Unit]
Description=Service that restarts my spread_goodness.service every two hours.

[Service]
Type=oneshot
ExecStart=/usr/bin/systemctl try-restart spread_goodness.service

You can check the current state of timers using systtemctl.

# Check that timers are active.


$ systemctl list-timers

NEXT LEFT LAST \


Mon 2017-04-15 22:00:00 UTC 1h 52min left Mon 2017-04-10 20:13:00 UTC \

PASSED UNIT ACTIVATES


4min 49s ago two-hour.timer two-hour.service
Goodbye.

You might also like