Home > Operation and Maintenance > Linux Operation and Maintenance > How to use Systemd and Crontab to execute scripts regularly in Linux systems

How to use Systemd and Crontab to execute scripts regularly in Linux systems

WBOY
Release: 2023-09-26 14:33:39
Original
939 people have browsed it

How to use Systemd and Crontab to execute scripts regularly in Linux systems

Title: Using Systemd and Crontab to execute scripts regularly in Linux systems

Text:
In Linux systems, we often need to execute some script tasks regularly, Such as backing up data, cleaning logs, etc. This article will introduce how to use Systemd and Crontab to implement the function of regularly executing scripts, and provide specific code examples.

1. Use Systemd to execute scripts regularly

Systemd is an initialization system and service manager under the Linux system. By creating Systemd scheduled tasks, we can easily execute scripts regularly.

  1. Create a .service file to define the relevant parameters of the scheduled task. Taking regular log cleaning as an example, you can create a file named cleanuplog.service.
[Unit]
Description=Cleanup Log Service

[Service]
Type=simple
ExecStart=/path/to/cleanuplog.sh

[Install]
WantedBy=default.target
Copy after login

Among them, ExecStart specifies the script path to be executed and needs to be modified according to the actual situation. Save and close the file.

  1. Create a .timer file to define the triggering conditions and execution logic of scheduled tasks. Taking execution at 1 am every day as an example, create a file named cleanuplog.timer.
[Unit]
Description=Cleanup Log Timer

[Timer]
OnCalendar=*-*-* 01:00:00

[Install]
WantedBy=timers.target
Copy after login

Among them, OnCalendar specifies the trigger time of the scheduled task, which is 1 am every day. Save and close the file.

  1. Move the two files to the /etc/systemd/system directory.
$ sudo mv cleanuplog.service /etc/systemd/system
$ sudo mv cleanuplog.timer /etc/systemd/system
Copy after login
  1. Enable and start scheduled tasks.
$ sudo systemctl daemon-reload
$ sudo systemctl enable cleanuplog.timer
$ sudo systemctl start cleanuplog.timer
Copy after login

Now, the task of regularly executing the script has been created and started successfully.

2. Use Crontab to execute scripts regularly

Crontab is a tool for periodically executing commands or scripts. By editing the Crontab file, we can easily customize the needs for scheduled execution of scripts.

  1. Edit the current user's Crontab file.
$ crontab -e
Copy after login
  1. Add a command to regularly execute the script in the open file. Taking execution at 2 a.m. every day as an example, you can add the following content.
0 2 * * * /path/to/cleanuplog.sh
Copy after login

Among them, 0 2 * * * represents 2 a.m. every day, /path/to/cleanuplog.sh represents the script path to be executed, It needs to be modified according to the actual situation.

  1. Save and close the file.

Now, the task of regularly executing the script has been added successfully.

Summary:
This article introduces how to use Systemd and Crontab to execute scripts regularly in Linux systems, and provides specific code examples. According to actual needs, choosing an appropriate method to implement scheduled tasks can improve work efficiency and automated management capabilities. I hope this article can help readers learn and apply the method of timing script execution.

The above is the detailed content of How to use Systemd and Crontab to execute scripts regularly in Linux systems. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template