Crond scheduled task

Cron timed task expression manual

Cron expression is applied in Unix and Unix like operating systems to make scripts and tasks execute periodically and repeatedly. Cron expression has poor and rich expression ability and can meet various time expression needs. This page arranges some basic grammar and common examples for your reference.

Basic grammar

Syntax description

There are 7 bits in total. The last bit is optional and can not be written. There are at least 6 bits. The positions from left to right are:

positionsignificanceValueSupported symbols
1second0-59, - * /
2branch0-59, - * /
3Time0-23, - * /
4day1-31, - * ? / L W C
5month1-12 or JAN - DEC, - * /
6week1-7 or MON - SAT, - * ? / L C #
7yearEmpty or 1970-2099, - * /

Note: Cron expression is insensitive to date abbreviations and special characters.

Symbol description

Symbolnamefunction
*asteriskIndicates that the cycle at the corresponding position is repeated, for example, the fourth digit indicates daily
,commaRepresents a list value, which represents multiple specified times, such as sat on the weekly position, and sun represents Saturday and Sunday every week
?question markMeaningless, placeholder, can only be in the day and week
-minus signIndicates a range. For example, 20-22 on the time level indicates 20, 21 and 22 points
/Slasha/b can represent the time series with a as the starting point and b as the step size. For example, 10 / 10 on the daily position represents 10 days, 20 days and 30 days
LLastOn the last day of the month or Saturday, the week , 6L , indicates the last Friday of the month
WWeekdayThe next nearest working day, 3W} 3, if it is Friday, it will be implemented on the 6th (Monday)
#Well numbera#b refers to the b-th week a of the current month, such as the first Friday of #6#1) the current month
CCalendarCalculation results of the associated calendar

Note:

  • After the value is given on the weekly position, it should be used on the daily position?
  • "L" and "W" can be used together in the day position. LW represents the working day of the last week of the month
  • The last working day specified by the "W" character cannot span months
  • W string can only specify a single date, not a date range
  • The maximum recommended daily position is 28, because February is sometimes 28 days

Week comparison:

0 or 7=Sun, 1=Mon, 2=Tue, 3=Wed, 4=Thu, 5=Fri, 6=Sat
1,3,5=Mon,Wed,Fri

Examples

# Every five minutes
0 5 * * * ?
# 5:30 every day
0 30 5 * * ?
# Every Friday at 18:18
0 18 18 ? * FRI *
# 8:00 to 23:00 every day, every hour
1 1 8-23 * * ?
# Monday to Friday, 8:00 to 22:00 every day, every three hours
1 1 8-22/3 ? * MON-FRI
# At 10:30, 14:30 and 17:30 on the 13th of the month
30 10,14,17 13 * *
# 6 to 8 o'clock every day, every 10 minutes
*/10 6-8 * * *
# At 00:05 on Monday
5 0 * * 1

crond service
Install crontab:

yum install crontabs

Service operation instructions:

/sbin/service crond start # start service
/SBIN / Service Cross stop # close service
/sbin/service crond restart # restart the service
/sbin/service crond reload # reload configuration

To view crontab service status:

service crond status

To start the crontab service manually:

service crond start

Check whether the crontab service has been set to startup and execute the command:

ntsysv


Add automatic startup:

chkconfig –level 35 crond on

Timing setting

Linux set scheduled tasks

Take unbuntu as an example:

# The format is: time-sharing day month week command to run
# week (0 - 6) = sun,mon,tue,wed,thu,fri,sat
# 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,...,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
# Edit and modify, centOS can use vi /etc/crontab, i enter the editing state,: q exit: wq save exit
sudo nano /etc/crontab
# Monitoring log cron_log manual creation
*/1 * * * * /bin/date >> /home/cron_log/log_$(date +\%Y-\%m\%d-\%H\%M)
 # Take effect
crontab /etc/crontab
# What you see takes effect
crontab -l
# Observe the implementation
tail -f /var/log/cron
# View recent crontab implementation
tail -f /var/spool/mail/root
# Others are similar to MAC, so I won't elaborate

Example of executing command line:

# Execute PHP script:
/usr/bin/php /home/username/public_html/cron.php

# Execute Python script:
/home/miniconda3/envs/my_env/bin/python /home/do.py

# Perform MySQL dump backup:
mysqldump -u root -pPASSWORD database > /root/db.sql

# Get URL information:
/usr/bin/wget --spider "http://www.domain.com/cron.php"

Windows set scheduled tasks

No, you can use my computer - right click 'management' - task scheduler to operate.

Mac set scheduled tasks

# Check to see if it starts
sudo launchctl list | grep cron
# If there is no file, create one
sudo touch /etc/crontab
vi /etc/crontab
crontab /etc/crontab
# State operation
sudo /usr/sbin/cron start | restart | stop
# View existing task list
sudo crontab -l
# Edit task
sudo crontab -e
# Save and refund esc, wq! Save and take effect by visual inspection
# Delete crontab file
sudo crontab -r
# If you do not execute (e.g. python), you may need to increase the Full Disk Access permission of cron
# System Preferences > Security & Privacy > Privacy > Full Disk Access:
# command+shift+G GOTO: /usr/sbin/cron, select Cron
# You can check whether there is a file permission problem
cd /Users/gai/Documents/Dev/bin/
chmod +x auto_run.sh

File format of crontab:

Time sharing day month week command to run
* Column 1 minute 0~59
* Column 2 hour 0~23(0 (indicates midnight)
* Column 3 date 1~31
* Column 4 month 1~12
* Column 5 week 0~7(0 And 7 for Sunday)
* Column 6 commands to run

Script command with parameters:

# The time parameter will be parsed into 2019-0911-2243, and multiple commands will be connected with &
15 11 * * * touch "/tmp/task-$(date +\%Y-\%m\%d-\%H\%M).sql"
# A time record will be added to the document every minute
*/1*/1 * * * * /bin/date >> /Users/hui/Downloads/time.txt
# Such as executing script
*/1 * * * * /bin/sh /Dev/bin/auto_run.sh
# For example, check the crontab environment variable
* * * * * env >> /Users/gai/Downloads/envs.txt

other:

If there is an error in the cron task, the system will send a letter to the local / var/mail/$user, and open the terminal to see "You have new mail.", Enter "mail" to view and delete the $user file after reading.

more: https://www.jianshu.com/p/7ecf40421cf2

Code application

Application in JAVA:

@Component
public class ScheduleTask {
// 5:30 every day
    @Scheduled(cron = "0 30 5 * * ?")
}

JavaScript:

var CronJob = require('cron').CronJob;
new CronJob('* * * * * *', function() {
  console.log('You will see this message every second');
}, null, true, 'America/Los_Angeles');

Update description

Online parsing of Cron expression:

  • http://cron.qqe2.com
  • https://crontab.guru
  • https://crontab-generator.org

Keywords: Linux Operation & Maintenance shell

Added by Mr_jmm on Fri, 04 Mar 2022 21:46:34 +0200