--- tags: - Systemd - Shellcategories: - Linuxdate:created: 2023-08-20# updated: 2024-01-19---
Execute Scripts on Period with SystemdThis tutorial aims to implement scheduled execution tasks throughTimerthat comes withSystemd¶
This article takesgit pullthat implements timing as an example to explain the timer ofSystemd
1. Create agit pullScriptThe main purpose of this script is to enter the specified git path and execute thegit pullinstruction> You need to make sure that there is a git repository that has been cloned locally in this path.### (1). The content ofvim gitpull.shis as follows:```¶
!/bin/sh¶
cd ${pwd_of_git_repo_path} git pull
> ${pwd_of_git_repo_path} represents the folder of the git repository### (2). After editing, save and exit`:wq`, and add running permissions to the script:```
sudo chmod +x gitpull.sh
(3). Test whether the script can be runRun./gitpull.shon the command line. When the command line displays the same output as the commandgit pullwhen running normally, it means that the script is normal.¶
2. Creategitpull.service### (1). Create a file namedgitpull.servicein the/etc/systemd/system/or/usr/lib/systemd/system/directory with the following content:```¶
[Unit] Description=gitpull service
[Service] User=\({USER} Group=\) ExecStart=/bin/bash ${pwd_of_scripts}/gitpull.sh
[Install] WantedBy=multi-user.target
> ${pwd_of_scripts} represents the folder where the script created in the previous step is located> ${USER} represents the user who executes the script, ${GROUP} represents the group to which the user belongs### (2). Test ServiceReload and run`gitpull.service````
sudo systemctl daemon-reload
sudo systemctl start gitpull.service
Systemdto determine whether the service is running normally and whether the output is the same as that of direct`git pull````
sudo systemctl status gitpull.service
## 3. Create`gitpull.timer`### (1). Create a timer with the following content:```
[Unit]
Description=run gitpull.sh every 1hour
[Timer]
OnUnitActiveSec=1h
Unit=gitpull.service
[Install]
WantedBy=multi-user.target
The name of
Timerneeds to be consistent with the name ofService>OnUnitActiveSecrepresents the interval at which the run timer runs>Oncalnedarmeans running at a specific time>Unitrepresents the service that needs to be run - Oncalnedar* *-*-* *:*:*-*- To signify the day of the week eg:- Sat,Thu,Mon -*-*-*- To signify the calendar date. Which means it breaks down to - year-month-date. - 2021-10-15 is the 15th of October - -10-15 means every year at 15th October - -01-01 means every new year. -*:*:*is to signify the time component of the calnedar event. So it is - hour:minute:second### (2). Run test`Timer```` sudo systemctl daemon-reload sudo systemctl start gitpull.timer
4. Run Timer on Boot```¶
sudo systemctl enable gitpull.service sudo systemctl enable gitpull.timer ```
REF[1]. http://www.ruanyifeng.com/blog/2018/03/systemd-timer.html¶
[2]. https://silentlad.com/systemd-timers-oncalendar-(cron)-format-explained
[3]. Systemd-a first exploration [4]. Cloudflare-DDNS-with-ShellScripts [5]. Auto-Archive-Files-with-ShellScripts