How to Use the timer in systemd
I recently wrote some scripts for performing backup. I decided to use systemd timers instead of the cron jobs that I am more familiar with to manage them.
When I used it, I had a lot of problems and needed to go to various places to find information. This process was very troublesome. Therefore, I want to record what I have done so as to facilitate my memory and help readers not to look for information around the world like me.
There are other options in the steps I mentioned below, but here is the simplest method. Before that, check the systemd. service, systemd.timer,and systemd.tar get help pages (man) to learn what you can do with them.
Run a simple script
Suppose you have a script named/usr/local/bin/myscript, and you want to run it every hour.
Service file
Step 1: create a service file and place it in the corresponding system directory (in Arch, this directory is/etc/systemd/system/or/usr/lib/systemd/system)
Myscript. service
- [Unit]
- Description = MyScript
-
- [Service]
- Type = simple
- ExecStart =/usr/local/bin/myscript
Note that you must set the value of the Type variable to "simple" instead of "oneshot ". The "oneshot" is used to make the script run only for the first time. After that, the system will think that you do not want to run it again, so as to turn off the Timer we created next time (Timer ).
Timer File
Step 2: Create a timer file and put it in the directory where the service file is placed in step 1.
Myscript. timer
- [Unit]
- Description = Runs myscript every hour
-
- [Timer]
- # The first Running takes 10 minutes after startup
- OnBootSec = 10 min
- # Interval of each running
- OnUnitActiveSec = 1 h
- Unit = myscript. service
-
- [Install]
- Wantedbypolicmulti-user.tar get
Authorize/run
The timer file is authorized and run, not the service file.
- # Start the timer as root
- Systemctl start myscript. timer
- # Enable the timer after the system is booted up
- Systemctl enable myscript. timer
Run multiple scripts on the same Timer
Now let's assume that you want to run multiple scripts at the same time. In this case, you need to make appropriate modifications in the above file.
Service file
As I said before, create your service file to run your script. However, each service file must contain the following content:
- [Install]
- Wantedbypolicmytimer.tar get
If there are some dependency sequences in your service file, make sure that you specify the parameters in After = something. service and Before = whatever. service by using the value in the Description field.
Another option is (or simpler). Create a packaging script to run the command in the correct order and use the script in your service file.
Timer File
You only need a timer file to create mytimer. timer, as I noted above.
Target File
You can create more than one target file on which all scripts depend.
Mytimer.tar get
- [Unit]
- Description = Mytimer
- # Lots more stuff cocould go here, but it's situational.
- # Look at systemd. unit man page.
Authorize/start
You need to authorize all service files and timer files.
- Systemctl enable script1.service
- Systemctl enable script2.service
- ...
- Systemctl enable mytimer. timer
- Systemctl start mytimer. service
Good luck.
Linux boot mode: systemd upstart sysV
Why is systemd so quickly adopted?
Systemd and sysVinit Color comparison table
Linux Kernel EMD -- start/stop/restart the service in RHEL/CentOS 7
So useful! Run the systemd command to manage the Linux system!
This article permanently updates the link address: