Home System Tutorial LINUX Scheduled tasks under Linux--crontab

Scheduled tasks under Linux--crontab

Jun 07, 2024 pm 08:24 PM
linux linux tutorial Red Hat linux system linux command linux certification red hat linux linux video

1. cron service

Cron is a scheduled execution tool under Linux that can run jobs without manual intervention.

service crond start //启动服务
service crond stop //关闭服务
service crond restart //重启服务
service crond reload //重新载入配置
service crond status //查看服务状态
Copy after login
2. Cron configuration file:

/var/spool/cron/ This directory stores the crontab tasks of each user including root. Each task is named after the creator. For example, the file corresponding to the crontab task created by tom is /var/spool/ cron/tom. Generally, a user can only have one crontab file at most.

3. /etc/crontab This file is responsible for arranging the crontab for system maintenance and other tasks specified by the system administrator.
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
Copy after login

MAILTO=root: It means that when an error occurs in the routine command in the /etc/crontab file, to whom will the error message or the message displayed on the screen be sent? Since root cannot receive emails from the client, I usually change this e-mail to my own account so that I can know the status of the system at any time!

01 * * * * root run-parts /etc/cron.hourly: In the commands after the #run-parts line, we can find that the five numbers are followed by root, and this line represents "Execute The level is root identity. Of course, you can also change this line to other identities! And run-parts means that the following /etc/cron.hourly is "all executable files in a directory (/etc/cron.hourly)". That is to say, at 01 minutes of every hour, the system will log in as root. Go to the directory /etc/cron.hourly to execute all executable files! The next three lines have similar meanings! You can go to /etc/ and take a look. The system has these 4 directories preset! You can write the commands you need to execute every day directly to /etc/cron.daily, and you don’t need to use the crontab -e program!

4. /etc/cron.d/ This directory is used to store any crontab files or scripts to be executed.

Scheduled tasks under Linux--crontab

1. MAILTO=root: It means that when an error occurs in the routine command in the /etc/crontab file, to whom will the error message or the message displayed on the screen be sent? Since root cannot receive emails from the client, I usually change this e-mail to my own account so that I can know the status of the system at any time!

2. 01 * * * * root run-parts /etc/cron.hourly: In the commands after the #run-parts line, we can find that the five numbers are followed by root , this line represents "The execution level is root identity". Of course, you can also change this line to other identities! And run-parts means that the following /etc/cron.hourly is "all executable files in a directory (/etc/cron.hourly)", that is to say, at 01 minutes every hour, the system will log in as root Go to the directory /etc/cron.hourly to execute all executable files! The next three lines have similar meanings! You can go to /etc/ and take a look. The system has these 4 directories preset! You can write the commands you need to execute every day directly to /etc/cron.daily, and you don’t need to use the crontab -e program!

5. Permissions

crontab权限问题到/var/adm/cron/下一看,文件cron.allow和cron.deny是否存在
用法如下:
1、如果两个文件都不存在,则只有root用户才能使用crontab命令。
2、如果cron.allow存在但cron.deny不存在,则只有列在cron.allow文件里的用户才能使用crontab命令,如果root用户也不在里面,则root用户也不能使用crontab。
3、如果cron.allow不存在, cron.deny存在,则只有列在cron.deny文件里面的用户不能使用crontab命令,其它用户都能使用。
4、如果两个文件都存在,则列在cron.allow文件中而且没有列在cron.deny中的用户可以使用crontab,如果两个文件中都有同一个用户,以cron.allow文件里面是否有该用户为准,如果cron.allow中有该用户,则可以使用crontab命令。

AIX 中 普通用户默认都有 crontab 权限,如果要限制用户使用 crontab ,就需要编辑/var/adm/cron/cron.deny
HP-UNIX 中默认普通用户没得crontab 权限 ,要想放开普通用户的crontab 权限可以编

六、创建cron脚本

第一步:写cron脚本文件,命名为crontest.cron。

15,30,45,59 * * * * echo "xgmtest....." >> xgmtest.txt 表示,每隔15分钟,执行打印一次命令
Copy after login

第二步:添加定时任务。执行命令 “crontab crontest.cron”。搞定
第三步:"crontab -l" 查看定时任务是否成功或者检测/var/spool/cron下是否生成对应cron脚本

注意:这操作是直接替换该用户下的crontab,而不是新增

七、crontab用法

crontab命令用于安装、删除或者列出用于驱动cron后台进程的表格。用户把需要执行的命令序列放到crontab文件中以获得执行。
每个用户都可以有自己的crontab文件。/var/spool/cron下的crontab文件不可以直接创建或者直接修改。该crontab文件是通过crontab命令创建的

在crontab文件中如何输入需要执行的命令和时间。该文件中每行都包括六个域,其中前五个域是指定命令被执行的时间,最后一个域是要被执行的命令。
每个域之间使用空格或者制表符分隔。格式如下:

minute hour day-of-month month-of-year day-of-week commands
合法值 00-59 00-23 01-31 01-12 0-6 (0 is sunday)
Copy after login

Scheduled tasks under Linux--crontab
除了数字还有几个个特殊的符号就是"*"、"/"和"-"、",",*代表所有的取值范围内的数字,"/"代表每的意思,"/5"表示每5个单位,"-"代表从某个数字到某个数字,","分开几个离散的数字。

-l 在标准输出上显示当前的crontab。
-r 删除当前的crontab文件。
-e 使用VISUAL或者EDITOR环境变量所指的编辑器编辑当前的crontab文件。当结束编辑离开时,编辑后的文件将自动安装。

八、例子:

每天早上6点

0 6 * * * echo "Good morning." >> /tmp/test.txt //注意单纯echo,从屏幕上看不到任何输出,因为cron把任何输出都email到root的信箱了。
Copy after login

每两个小时

0 */2 * * * echo "Have a break now." >> /tmp/test.txt
Copy after login

晚上11点到早上8点之间每两个小时和早上八点

0 23-7/2,8 * * * echo "Have a good dream" >> /tmp/test.txt
Copy after login

每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点

0 11 4 * 1-3 command line
Copy after login

1月1日早上4点

0 4 1 1 * command line SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root //如果出现错误,或者有数据输出,数据作为邮件发给这个帐号 HOME=/
Copy after login

每小时执行/etc/cron.hourly内的脚本

01 * * * * root run-parts /etc/cron.hourly
Copy after login

每天执行/etc/cron.daily内的脚本

02 4 * * * root run-parts /etc/cron.daily
Copy after login

每星期执行/etc/cron.weekly内的脚本

22 4 * * 0 root run-parts /etc/cron.weekly
Copy after login

每月去执行/etc/cron.monthly内的脚本

42 4 1 * * root run-parts /etc/cron.monthly
Copy after login

注意: "run-parts"这个参数了,如果去掉这个参数的话,后面就可以写要运行的某个脚本名,而不是文件夹名。

每天的下午4点、5点、6点的5 min、15 min、25 min、35 min、45 min、55 min时执行命令。

5,15,25,35,45,55 16,17,18 * * * command
Copy after login

每周一,三,五的下午3:00系统进入维护状态,重新启动系统。

00 15 * * 1,3,5 shutdown -r +5
Copy after login

每小时的10分,40分执行用户目录下的innd/bbslin这个指令:

10,40 * * * * innd/bbslink
Copy after login

每小时的1分执行用户目录下的bin/account这个指令:

1 * * * * bin/account
Copy after login

每天早晨三点二十分执行用户目录下如下所示的两个指令(每个指令以;分隔):

20 3 * * * (/bin/rm -f expire.ls logins.bad;bin/expire$#@62;expire.1st)
Copy after login

每年的一月和四月,4号到9号的3点12分和3点55分执行/bin/rm -f expire.1st这个指令,并把结果添加在mm.txt这个文件之后(mm.txt文件位于用户自己的目录位置)。

12,55 3 4-9 1,4 * /bin/rm -f expire.1st$#@62;$#@62;mm.txt
Copy after login

The above is the detailed content of Scheduled tasks under Linux--crontab. For more information, please follow other related articles on the PHP Chinese website!

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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1242
24
Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

git software installation git software installation Apr 17, 2025 am 11:57 AM

Installing Git software includes the following steps: Download the installation package and run the installation package to verify the installation configuration Git installation Git Bash (Windows only)

How to run sublime python How to run sublime python Apr 16, 2025 am 08:54 AM

How to run Python scripts in Sublime Text: Install Python interpreter configuration Interpreter path in Sublime Text Press Ctrl B (Windows/Linux) or Cmd B (macOS) to run the script If an interactive console is required, press Ctrl \ (Windows/Linux) or Cmd \ (macOS)

See all articles