Merge pull request #1768 from johnhoow/master

translated
This commit is contained in:
Xingyu.Wang 2014-10-03 20:59:17 +08:00
commit a03d098875
2 changed files with 111 additions and 112 deletions

View File

@ -1,112 +0,0 @@
Translating by johnhoow...
How to Use Systemd Timers
================================================================================
I was setting up some scripts to run backups recently, and decided I would try to set them up to use [systemd timers][1] rather than the more familiar to me [cron jobs][2].
As I went about trying to set them up, I had the hardest time, since it seems like the required information is spread around in various places. I wanted to record what I did so firstly, I can remember, but also so that others dont have to go searching as far and wide as I did.
There are additional options associated with the each step I mention below, but this is the bare minimum to get started. Look at the man pages for **systemd.service**, **systemd.timer**, and **systemd.target** for all that you can do with them.
### Running a Single Script ###
Lets say you have a script **/usr/local/bin/myscript** that you want to run every hour.
#### Service File ####
First, create a service file, and put it wherever it goes on your Linux distribution (on Arch, it is either **/etc/systemd/system/** or **/usr/lib/systemd/system**).
myscript.service
[Unit]
Description=MyScript
[Service]
Type=simple
ExecStart=/usr/local/bin/myscript
Note that it is important to set the **Type** variable to be “simple”, not “oneshot”. Using “oneshot” makes it so that the script will be run the first time, and then systemd thinks that you dont want to run it again, and will turn off the timer we make next.
#### Timer File ####
Next, create a timer file, and put it also in the same directory as the service file above.
myscript.timer
[Unit]
Description=Runs myscript every hour
[Timer]
# Time to wait after booting before we run first time
OnBootSec=10min
# Time between running each consecutive time
OnUnitActiveSec=1h
Unit=myscript.service
[Install]
WantedBy=multi-user.target
#### Enable / Start ####
Rather than starting / enabling the service file, you use the timer.
# Start timer, as root
systemctl start myscript.timer
# Enable timer to start at boot
systemctl enable myscript.timer
### Running Multiple Scripts on the Same Timer ###
Now lets say there a bunch of scripts you want to run all at the same time. In this case, you will want make a couple changes on the above formula.
#### Service Files ####
Create the service files to run your scripts as I [showed previously][3], but include the following section at the end of each service file.
[Install]
WantedBy=mytimer.target
If there is any ordering dependency in your service files, be sure you specify it with the **After=something.service** and/or **Before=whatever.service** parameters within the **Description** section.
Alternatively (and perhaps more simply), create a wrapper script that runs the appropriate commands in the correct order, and use the wrapper in your service file.
#### Timer File ####
You only need a single timer file. Create **mytimer.timer**, as I [outlined above][4].
#### Target File ####
You can create the target that all these scripts depend upon.
mytimer.target
[Unit]
Description=Mytimer
# Lots more stuff could go here, but it's situational.
# Look at systemd.unit man page.
#### Enable / Start ####
You need to enable each of the service files, as well as the timer.
systemctl enable script1.service
systemctl enable script2.service
...
systemctl enable mytimer.timer
systemctl start mytimer.service
Good luck.
--------------------------------------------------------------------------------
via: http://jason.the-graham.com/2013/03/06/how-to-use-systemd-timers/#enable--start-1
作者Jason Graham
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[1]:https://fedoraproject.org/wiki/User:Johannbg/QA/Systemd/Systemd.timer
[2]:https://en.wikipedia.org/wiki/Cron
[3]:http://jason.the-graham.com/2013/03/06/how-to-use-systemd-timers/#service-file
[4]:http://jason.the-graham.com/2013/03/06/how-to-use-systemd-timers/#timer-file-1

View File

@ -0,0 +1,111 @@
如何使用系统定时器
================================================================================
我最近在写一些运行备份的脚本,我决定使用[systemd timers][1]而不是对我而已更熟悉的[cron jobs][2]来管理它们。
在我使用时,出现了很多问题需要我去各个地方找资料,这个过程非常麻烦。因此,我想要把我目前所做的记录下来,方便自己的记忆,也方便读者不必像我这样,满世界的找资料了。
在我下面提到的步骤中有其他的选择,但是这边是最简单的方法。在此之前,查看**systemd.service**, **systemd.timer**,和**systemd.target**的帮助页面(man),学习你能用它们做些什么。
### 运行一个简单的脚本 ###
假设你有一个脚本叫:**/usr/local/bin/myscript** ,你想要每隔一小时就运行一次。
#### Service 文件 ####
第一步创建一个service文件根据你Linux的发行版本放到相应的系统目录(在Arch中这个目录是**/etc/systemd/system/** 或 **/usr/lib/systemd/system**)
myscript.service
[Unit]
Description=MyScript
[Service]
Type=simple
ExecStart=/usr/local/bin/myscript
注意,务必将**Type**变量的值设置为"simple"而不是"oneshot"。使用"oneshot"使得脚本只在第一次运行,之后系统会认为你不想再次运行它,从而关掉我们接下去创建的定时器(Timer)。
#### Timer 文件 ####
第二步创建一个timer文件把它放在第一步中service文件放置的目录。
myscript.timer
[Unit]
Description=Runs myscript every hour
[Timer]
# Time to wait after booting before we run first time
OnBootSec=10min
# Time between running each consecutive time
OnUnitActiveSec=1h
Unit=myscript.service
[Install]
WantedBy=multi-user.target
#### 授权 / 运行 ####
授权并运行的是timer文件而不是service文件。
# Start timer, as root
systemctl start myscript.timer
# Enable timer to start at boot
systemctl enable myscript.timer
### 在同一个Timer上运行多个脚本 ###
现在我们假设你在相同时间想要运行多个脚本。这种情况,你需要在上面的文件中做适当的修改。
#### Service 文件 ####
像我[之前说过的][3]那样创建你的service文件来运行你的脚本但是在每个service 文件最后都要包含下面的内容:
[Install]
WantedBy=mytimer.target
如果在你的service 文件中有一些规则,确保你使用**Description**字段中的值具体化**After=something.service**和**Before=whatever.service**中的参数。
另外的一种选择是(或许更加简单)创建一个包装者脚本来使用正确的规则运行合理的命令并在你的service文件中使用这个脚本。
#### Timer 文件 ####
你只需要一个timer文件创建**mytimer.timer**,像我在[上面指出的](4)。
#### target 文件 ####
你可以创建一个以上所有的脚本依赖的target文件。
mytimer.target
[Unit]
Description=Mytimer
# Lots more stuff could go here, but it's situational.
# Look at systemd.unit man page.
#### 授权 / 启动 ####
你需要将所有的service文件和timer文件授权。
systemctl enable script1.service
systemctl enable script2.service
...
systemctl enable mytimer.timer
systemctl start mytimer.service
Good luck.
--------------------------------------------------------------------------------
via: http://jason.the-graham.com/2013/03/06/how-to-use-systemd-timers/#enable--start-1
作者Jason Graham
译者:[译者ID](https://github.com/johnhoow)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[1]:https://fedoraproject.org/wiki/User:Johannbg/QA/Systemd/Systemd.timer
[2]:https://en.wikipedia.org/wiki/Cron
[3]:http://jason.the-graham.com/2013/03/06/how-to-use-systemd-timers/#service-file
[4]:http://jason.the-graham.com/2013/03/06/how-to-use-systemd-timers/#timer-file-1