
Systemd is an essential part of many Linux distributions, including Ubuntu. It provides a standard process for controlling what programs run when a Linux system boots up. In this article, we’ll discuss how to install and run a systemd timer and service on Ubuntu Bionic.
To install and run a systemd timer and service on Ubuntu Bionic, you need to create a .service
file and a .timer
file in the /etc/systemd/system
directory. The .service
file defines the service you want to run, while the .timer
file specifies when and how often the service should run. After creating the files, you can install them by running sudo systemctl daemon-reload
, and then enable and start the timer with sudo systemctl enable my-custom-command.timer
and sudo systemctl start my-custom-command.timer
.
Introduction to Systemd
Systemd is a system and service manager for Linux operating systems. It is designed to be backward compatible with SysV init scripts, and provides a number of features such as parallel startup of system services at boot time, on-demand activation of daemons, or dependency-based service control logic.
Creating Systemd Service and Timer
To install a systemd timer and service, you need to create two files: a .service
file and a .timer
file. These files should be placed in the /etc/systemd/system
directory.
The .service
file defines the service you want to run. Here’s an example of a simple .service
file:
[Unit]
Description=My Custom Command
[Service]
ExecStart=/usr/bin/my-custom-command
In this example, the ExecStart
parameter specifies the command to run when the service starts.
The .timer
file defines when and how often the service should run. Here’s an example of a simple .timer
file:
[Unit]
Description=Runs my-custom-command every hour
[Timer]
OnCalendar=hourly
[Install]
WantedBy=timers.target
In this example, the OnCalendar
parameter is set to hourly
, which means the service will run once every hour.
Installing the Timer and Service
Once you’ve created your .service
and .timer
files, you can install them by running the following command:
sudo systemctl daemon-reload
This command reloads the systemd manager configuration. This is necessary whenever you create or modify a unit file.
Enabling and Starting the Timer
After installing your timer and service, you need to enable and start the timer. Enabling the timer makes it start automatically at boot time, and starting the timer makes it active immediately.
To enable the timer, run:
sudo systemctl enable my-custom-command.timer
To start the timer, run:
sudo systemctl start my-custom-command.timer
Remember, both enabling and starting the timer are necessary for it to function correctly.
Conclusion
Systemd timers are a powerful tool for scheduling tasks on a Linux system. They offer a lot of flexibility and are relatively easy to set up.
Remember to run sudo systemctl daemon-reload
whenever you make changes to your .service
or .timer
files, and to enable and start your timer with sudo systemctl enable my-custom-command.timer
and sudo systemctl start my-custom-command.timer
.
For more information on systemd timers, you can check the official systemd documentation. For more information on Ubuntu Bionic, visit the official Ubuntu documentation.
The .service
and .timer
files should be placed in the /etc/systemd/system
directory.
To enable the timer, run sudo systemctl enable my-custom-command.timer
. To start the timer, run sudo systemctl start my-custom-command.timer
.
If you set OnCalendar
to hourly
, the service will run once every hour.
Yes, you need to run sudo systemctl daemon-reload
whenever you make changes to the .service
or .timer
files. This command reloads the systemd manager configuration.
Yes, systemd timers can be used on other Linux distributions that use systemd as their init system. However, the specific commands and file locations may vary.