How to Use the timer in systemd

Source: Internet
Author: User
Tags comparison table

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

  1. [Unit]
  2. Description = MyScript
  3.  
  4. [Service]
  5. Type = simple
  6. 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

  1. [Unit]
  2. Description = Runs myscript every hour
  3.  
  4. [Timer]
  5. # The first Running takes 10 minutes after startup
  6. OnBootSec = 10 min
  7. # Interval of each running
  8. OnUnitActiveSec = 1 h
  9. Unit = myscript. service
  10.  
  11. [Install]
  12. Wantedbypolicmulti-user.tar get
Authorize/run

The timer file is authorized and run, not the service file.

  1. # Start the timer as root
  2. Systemctl start myscript. timer
  3. # Enable the timer after the system is booted up
  4. 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:

  1. [Install]
  2. 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

  1. [Unit]
  2. Description = Mytimer
  3. # Lots more stuff cocould go here, but it's situational.
  4. # Look at systemd. unit man page.
Authorize/start

You need to authorize all service files and timer files.

  1. Systemctl enable script1.service
  2. Systemctl enable script2.service
  3. ...
  4. Systemctl enable mytimer. timer
  5. 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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.