search
HomeOperation and MaintenanceLinux Operation and MaintenanceHow 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

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

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

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
  1. Enable and start scheduled tasks.
$ sudo systemctl daemon-reload
$ sudo systemctl enable cleanuplog.timer
$ sudo systemctl start cleanuplog.timer

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
  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

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!

Statement
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
crontab定时任务不执行的一些原因,你知道吗?crontab定时任务不执行的一些原因,你知道吗?Mar 09, 2024 am 09:49 AM

crontab定时任务不执行的一些缘由总结更新时间:2019年01月09日09:34:57作者:田野上的希望这篇文章主要给你们总结介绍了关于crontab定时任务不执行的一些缘由,对每种可能发生的诱因都给出了解决方式,对遇见这个问题的同事们具有一定的参考学习价值,须要的同学们下边随着小编来一起学习学习吧序言近来在工作中遇见了一些问题,crontab定时任务竟然不执行,后来我在网上找的时侯发觉网上主要说了这5个诱因:1crond服务未启动crontab不是Linux内核的功能,而是依赖一个cron

如何使用Systemd和Crontab在Linux系统中实现任务的并行执行如何使用Systemd和Crontab在Linux系统中实现任务的并行执行Sep 26, 2023 pm 06:37 PM

如何使用Systemd和Crontab在Linux系统中实现任务的并行执行在Linux系统中,任务的并行执行是提高系统效率和性能的重要手段之一。本文将介绍如何使用Systemd和Crontab两个工具,在Linux系统中实现任务的并行执行,并提供具体的代码示例。一、Systemd介绍Systemd是一个用于管理Linux系统启动流程和服务管理的工具。通过配置

linux注释crontab文件及crontab执行sh的坑怎么解决linux注释crontab文件及crontab执行sh的坑怎么解决May 15, 2023 pm 09:58 PM

linux注释crontab文件及crontab执行sh的坑原来,在Linux下写了很多crontab,来定时执行某些任务,现在有以下需求:需求:是要注释某些crontab任务,方法:只要在所要取消的crontab任务前,加'#'即可。e.g.54**sunecho"runat5after4everysunday"注释:#54**sunecho"runat5after4everysunday"就这么简单。遇坑1看下面例子最近

linux crontab 错误日志怎么看linux crontab 错误日志怎么看Mar 07, 2023 am 09:29 AM

linux查看crontab错误日志的方法:1、查看文件目录“/var/log/cron”;2、通过“tail -f /var/log/cron”命令实现实时文件尾部查看;3、通过“vim /var/log/cron”命令实现通过高级文本查看器查看即可。

如何使用Systemd和Crontab在Linux系统中自动重启应用程序如何使用Systemd和Crontab在Linux系统中自动重启应用程序Sep 28, 2023 pm 03:35 PM

如何使用Systemd和Crontab在Linux系统中自动重启应用程序在Linux系统中,Systemd和Crontab是两个非常重要的工具。Systemd是一个系统和服务管理器,而Crontab则是一个用于在指定时间自动执行任务的工具。本文将以一个具体的例子,介绍如何使用Systemd和Crontab在Linux系统中自动重启应用程序。假设我们有一个No

怎么通过Systemd编译Mysql5.7.11怎么通过Systemd编译Mysql5.7.11May 29, 2023 pm 06:46 PM

MySQL5.7主要特性:原生支持Systemd更好的性能:对于多核CPU、固态硬盘、锁有着更好的优化更好的InnoDB存储引擎更为健壮的复制功能:复制带来了数据完全不丢失的方案,传统金融客户也可以选择使用MySQL数据库。此外,GTID在线平滑升级也变得可能更好的优化器:优化器代码重构的意义将在这个版本及以后的版本中带来巨大的改进,Oracle官方正在解决MySQL之前最大的难题原生JSON类型的支持更好的地理信息服务支持:InnoDB原生支持地理位置类型,支持GeoJSON,GeoHash特

如何在Linux系统中使用Systemd和Crontab实现系统自启动如何在Linux系统中使用Systemd和Crontab实现系统自启动Sep 26, 2023 am 11:57 AM

如何在Linux系统中使用Systemd和Crontab实现系统自启动引言:在Linux系统中,我们经常需要将一些常用的服务或脚本设置为系统自启动,以便系统重启后能够自动运行。在本文中,将介绍如何使用Systemd和Crontab这两个工具来实现系统自启动,并给出具体的代码示例。一、Systemd的使用Systemd是Linux操作系统中常用的系统和服务管理

如何利用Systemd和Crontab在Linux系统中设置定时任务的优先级如何利用Systemd和Crontab在Linux系统中设置定时任务的优先级Sep 27, 2023 am 08:25 AM

如何利用Systemd和Crontab在Linux系统中设置定时任务的优先级,需要具体代码示例在Linux系统中,我们经常需要设置定时任务来执行一些重复性的操作,例如定时备份文件、定期清理日志等。然而,不同的任务可能具有不同的优先级,有些任务需要更高的优先级来确保其准时执行,而有些任务则可以稍后执行。本文将介绍如何利用Systemd和Crontab来设置定时

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),