Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement crontab timing to execute tasks in Linux

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Xiaobian to share with you how to achieve crontab timing task execution in Linux, I believe most people still do not know how to share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

Basic format:

* * * * * command

time-sharing day-month-week command

The first column represents minutes 1 to 59, and every minute is represented by * or */1.

Column 2 represents hours 1 to 23 (0 represents 0 o'clock)

The third column represents dates 1 to 31

Column 4 represents months 1 - 12

Column 5 Identification Number Week 0~6 (0 means Sunday)

Column 6 Command to run

Some examples of crontab files:

30 21 * * * /usr/local/etc/rc.d/lighttpd restart

The above example shows restarting apache every night at 21:30.

45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart

The above example shows restarting apache at 4 : 45 on the 1st, 10th, and 22nd of each month.

10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart

The above example shows restarting apache every Saturday and Sunday at 1 : 10.

0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart

The above example shows restarting apache every 30 minutes between 18 : 00 and 23 : 00 every day.

0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart

The above example shows restarting apache every Saturday at 11 : 00 pm.

0 */1 * * * /usr/local/etc/rc.d/lighttpd restart

Restart apache every hour

0 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart

Restart apache every hour between 11pm and 7am

0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart

Restart apache on the 4th of each month and at 11:00 every Monday to Wednesday

0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart

Restart apache at 4:00 on january 1 st

Name: crontab

Access: All users

How to use:

crontab file [-u user]-Replace the current crontab with the specified file.

crontab-[-u user]-Replace the current crontab with standard input.

crontab-1[user]-Lists the user's current crontab.

crontab-e[user]-Edit the user's current crontab.

crontab-d[user]-Delete the user's current crontab.

crontab-c dir-Specifies the directory of crontabs.

crontab file format: M H D m d cmd.

M: minutes (0-59).

H: Hours (0-23).

D: Days (1-31).

m: Month (1-12).

d: Day of the week (0 - 6, 0 being Sunday).

cmd The program to run, the program is sent to sh execution, this shell only has USER,HOME,SHELL these three environment variables

Description:

crontab is used to allow the user to execute programs at fixed times or intervals, in other words, similar to the user's schedule. - u user means to specify

user's schedule, this premise is that you must have its permissions (such as root) to specify other people's schedules. If you don't use-u user, it means set

Set your own schedule.

Parameters:

crontab -e : Run a text editor to set the schedule. The default text editor is VI. If you want to use another text editor, set the VISUAL environment variable first.

to specify which text editor to use (say setenv VISUAL joe)

crontab -r : Delete the current schedule

crontab -l : List the current schedule

crontab file [-u user]-Replace the current crontab with the specified file.

The schedule format is as follows:

f1 f2 f3 f4 f5 program

Where f1 is the minute, f2 is the hour, f3 is the day of the month, f4 is the month, and f5 is the day of the week. Program means to execute

The procedure.

When f1 is *, it means that the program is executed every minute, f2 is *, it means that the program is executed every hour, and so on

When f1 is a-b, it means that it will be executed from minute a to minute b, when f2 is a-b, it means that it will be executed from minute a to hour b, and so on

When f1 is */n, it means that it is executed once every n minutes, f2 is */n, it means that it is executed once every n hours, and so on

When f1 is a, b, c,... A, B, C,... Minutes to execute, f2 for a, b, c,... A, B, C... hours to execute, and so on for the rest

Users can also save all settings in file first, and use crontab file to set the schedule.

Examples:

#Once a day at 7:00 am/bin/ls :

0 7 * * * /bin/ls

/usr/bin/backup : is executed every 3 hours between 6 a.m. and 12 a.m. for December

0 6-12/3 * 12 * /usr/bin/backup

Send a letter to alex@domain.name at 5:00 p.m. Monday through Friday:

0 17 * * 1-5 mail -s "hi" alex@domain.name

< /tmp/maildata 每月每天的午夜 0 点 20 分, 2 点 20 分, 4 点 20 分....执行 echo "haha" 20 0-23/2 * * * echo "haha" 注意 : 当程序在你所指定的时间执行后,系统会寄一封信给你,显示该程序执行的内容,若是你不希望收到这样的信,请在每一行空一格之 后加上 >

/dev/null 2>&1

Example 2 :

#Every morning at 6:10

10 6 * * * date

#Every two hours

0 */2 * * * date

#Every two hours between 11pm and 8am, 8am

0 23-7/2,8 * * * date

#11:00 a.m. Monday through Wednesday on the 4th of every month and every week

0 11 4 * mon-wed date

January 4 AM

0 4 1 jan * date

example

$crontab -l Lists the user's current crontab.

The above is "How to implement crontab timed task execution in Linux" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report