linux ubuntu Add crontab scheduled tasks

sudo apt-get install cron 安装crontab

Then add the task # crontab-e to run Select 2 nano for editing

*** Mandate commands require absolute path, as */60 * * cd root && /usr/bin/python3.7 test.py >> /root/cron-rdv.py.log 2>&1

Edit Add Task*/2 * / root/auto.sh run every 2 minutes

Add task*/60 * sh auto.sh >> /root/cron-rdv.py.log 2>&1

Note: Operation every 60 minutes means that the log output values in philthon/data5 are used to run curntest.py for two minutes, and of course the data entered into the specified file in program crungtest.py is not entered in this log file, which is the latter 2 >&One means that the wrong output is also exported to a standard output (two for errors, two for mistakes),&is equal to, and 1 is correct), so if you run wrong or correctly, the result will be exported into a previously defined log.

tail-f /var/log/syslog view system logs

Auto.sh Source File

Python3 / root/rdv-prefecture.py #Save to auto.sh file placed under/root/folder

Crontab -l look for task crontab -r remove job crontab-ir

Last service cron start or service cron restart

Examples

Example 1: MyCommand implemented every 1 minute

* * * * * myCommand

Example 2: 3rd and 15th minute per hour

3,15 * * * * myCommand

Example 3: at the 3rd and 15th minutes from 8 to 11 a.m.

3,15 8-11 * * * myCommand

Example 4: Implemented every two days between 8 and 11 a.m., 3rd and 15th minutes

3,15 8-11 */2  *  * myCommand

Example 5: Implementation at the 3rd and 15th minutes, Monday from 8 to 11 a.m.

3,15 8-11 * * 1 myCommand

Example 6: 21:30 restart smb per night

30 21 * * * /etc/init.d/smb restart

Example 7: 4:45 restarts smb

45 4 1,10,22 * * /etc/init.d/smb restart

Example 8: Saturday, Sunday at 1:10 smb

10 1 * * 6,0 /etc/init.d/smb restart

Example 9: Restart smb every 30 minutes between 1800 and 23:00

0,30 18-23 * * * /etc/init.d/smb restart

Example 10: Saturday night at 11:00 pm restarts smb

0 23 * * 6 /etc/init.d/smb restart

Example 11: resuscitation smb per hour

* */1 * * * /etc/init.d/smb restart

Example 12: Restart smb every hour between 11 p.m. and 7 a.m.

* 23-7/1 * * * /etc/init.d/smb restart
1)service cron start  /*启动服务*/
2)service cron stop /*关闭服务*/
3)service cron restart /*重启服务*/
4)service cron reload /*重新载入配置*/
可以通过以下命令查看cron是否在运行(如果在运行,则会返回一个进程ID):
# pgrep cron



*** 1安装cron 2添加任务 3重启任务 4查看任务pgrep cron 如图显示 
crontab每小时运行一次(转)
先给出crontab的语法格式

 
对于网上很多给出的每小时定时任务写法,可以说绝大多数都是错误的!比如对于下面的这种写法:
00 * * * * #每隔一小时执行一次 00 */1 * * * #与上面是相同的任务
这种是正确的吗?对照最前面给出的语法格式来看,这个定时任务在0分、每小时、每天、每月、每星期执行,看起来似乎很合理啊?前一个小时的0分和后一个小时的0分不就是相隔一个小时吗?这样的话不就是每隔一个小时执行吗?再仔细看看,好像有点不对劲,为什么是每小时的0分?如果我在11:55创建了定时任务,希望每小时执行一次,那么接下来这个定时任务是在12:55执行,还是在12:00执行?很显然,它是每小时的0分执行,也就是在12:00执行。
似乎没区别?那么请思考下,每隔3小时执行如何写?
网上给出的写法如下:
0 */3 * * * #错误的每隔3小时执行一次写法
这种也是错误的写法,这种能充分展示前面一小时执行一次带来的迷惑感,这种写法其实是每整3小时的0分执行一次。也就是说,它会在每天的3点、9点、12点...等3的整数倍时间点来执行,并不是真正意义上每隔3个小时执行,如同前面类似,如果你在11:56创建这种每3小时的定时任务,那么它会在12点就开始执行,而不是14:56执行。请看如下验证:
11:56分创建上面所谓的“每3小时执行一次”的定时任务:

 
4分钟后而不是3小时后,crontab执行了该定时任务:

 
事实上,对于这种 "0 */3 * * * " 这种分钟时间点固定,小时按每小时或者每3小时的写法,都是从1点开始算倍数,比如每一小时就是1点、2点、3点,而每2小时就是2点、4点、6点,每3小时就是3点、6点、9点等等,以此类推。
除此之外,网上还有人给出这种的每小时执行一次的定时任务写法:
* */1 * * * #错误的每隔一小时执行一次,事实上每分钟执行一次
这种比前面那种还不如,它压根不是每小时执行,而是每分钟执行!为什么?因为分钟要求的是每分钟执行,而小时却要求每一个小时执行,这2个分明是冲突的时间策略。最终以分钟为准,所以它是每分钟执行一次。
 
真正的每隔一小时、每隔105分钟执行的定时任务应该如何写呢?如下:
*/60 * * * * #每60分钟即每小时执行一次 */105 * * * * #每105分钟执行一次 
下面的例子,创建一个cron作业,它每分钟输出文本“test crontab is work or not”并把文本保存到/var/www/html/testcrontab.txt。
或者/root/testcron.txt
命令
* * * * echo "test crontab is work or not" >> /root/testcron.txt
写入以下的脚本,保存退出,一旦保存了此cron脚本文件,就可以把var它添加到计划的作业中。
* * * * echo "test crontab is work or not" >> /var/www/html/testcrontab.txt

Cron表达式详解可参考
运行结果:

————————————————  

未经允许不得转载:萌萌Apprentissage » linux ubuntu Add crontab scheduled tasks

Like (0) 打赏

Comments 0

Protected with IP Blacklist CloudIP Blacklist Cloud
  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

//下一个脚本特效
//下一个脚本