Recently contacted the needs of timed execution procedures, so learning to understand a bit crontab. This article first introduces Crontab's grammar knowledge, then makes a demo.
First, crontab grammar
1.crontab Basic Format
{minute} {Hour} {Day-of-month} {Month} {Day-of-week} {Full-path-to-shell-script}
2.crontab Syntax Examples
1#在 A: 01A.M Run2 1 0* * * */root/bin/backup.SH 3 #每个工作日11:59p.m run4 - One* *1,2,3,4,5/root/bin/backup.SH 5 - One* *1-5/root/bin/backup.SH 6 #每5分钟运行一次命令7*/5* * * * */root/bin/check-status.SH 8 #每个月的第一天1:10p.m run9 Ten - 1* */root/bin/full-backup.SH Ten #每个工作日11p. M Run One 0 at* *1-5/root/bin/incremental-backup.SH
Ii. Examples of crontab
1.crontab start-Up and stop
1 Service Crond Start/stop/restart
2.crontab-e way to add crontab tasks
1 crontab-e #添加任务 2 crontab-l #查看所有任务 3 crontab-r #删除任务
Preparatory work:
vim/home/helloworld/helloworld.py
vim/home/helloworld/helloworld.sh
1 #helloworld.py2 Import Time3With open ('Test.txt','w+') as F:4 f.write (str (time.time ()))5 6 7 #helloworld.sh8cd/home/helloworld/9python/home/helloworld/helloworld.py
Step1: After executing the CRONTAB-E command, go to edit mode and add the task you want to perform.
*/1 * * * * * bash/home/helloworld/helloworld. SH
Step2: Start the crontab service
Step3: View crontab Log
1 Tail-f/var/log/Cron2 #查看log文件3May A +: One: onlocalhost crond[12824]: (Root) CMD (Bash/home/helloworld/helloworld.SH)4May A +: One: onlocalhost crond[12823]: (Root) MAIL (mailed -bytes of output but got status0x007f#012)5May A +: A: onlocalhost crond[12865]: (Root) CMD (Bash/home/helloworld/helloworld.SH)6May A +: A: onlocalhost crond[12864]: (Root) MAIL (mailed -bytes of output but got status0x007f#012)7May A +: -: onlocalhost crond[12880]: (Root) CMD (Bash/home/helloworld/helloworld.SH)8May A +: -: onlocalhost crond[12879]: (Root) MAIL (mailed -bytes of output but got status0x007f#012)9May A +: -: onlocalhost crond[12895]: (Root) CMD (Bash/home/helloworld/helloworld.SH)TenMay A +: -: onlocalhost crond[12894]: (Root) MAIL (mailed -bytes of output but got status0x007f#012) One A #同时查看helloworld文件夹 -LL-T - #监测到 the[email protected] helloworld]# LL-T -Total dosage A --rw-r--r--1Root root -May A +: OneTest.txt --rw-r--r--1Root root -May A -: $HelloWorld.SH +-rw-r--r--1Root root94May A -: Ahelloworld.py -[email protected] helloworld]# LL-T +Total dosage A A-rw-r--r--1Root root -May A +: ATest.txt at-rw-r--r--1Root root -May A -: $HelloWorld.SH --rw-r--r--1Root root94May A -: Ahelloworld.py -[email protected] helloworld]# LL-T -Total dosage A --rw-r--r--1Root root -May A +: -Test.txt --rw-r--r--1Root root -May A -: $HelloWorld.SH in-rw-r--r--1Root root94May A -: Ahelloworld.py -[email protected] helloworld]# LL-T toTotal dosage A +-rw-r--r--1Root root AMay A +: -Test.txt --rw-r--r--1Root root -May A -: $HelloWorld.SH the-rw-r--r--1Root root94May A -: Ahelloworld.py *[Email protected] helloworld]#
3. Adding tasks by Vim/etc/crontab method
1#查看/etc/crontab2vim/etc/crontab3 4 #添加任务5shell=/bin/Bash6path=/sbin:/bin:/usr/sbin:/usr/bin7mailto=""8home=/9 Ten# for details seeMans 4Crontabs One # Example of job definition: A#.----------------Minute (0- -) -# | .-------------Hour (0- at) -# | | .----------Day of Month (1- to) the# | | | .-------Month (1- A) OR jan,feb,mar,apr ... -# | | | | .----Day of Week (0-6) (sunday=0Or7) OR Sun,mon,tue,WEd,thu,fri,sat -# | | | | | -# * * * * * * user-name Command to be executed +*/1* * * * root bash/home/helloworld/helloworld.SH
4. It is possible to monitor the duplication of tasks in the same way
Crontab scheduled execution of tasks