Linux服务器上使用CronTab定时执行php脚本任务

linux 2017-12-15 浏览(6888 评论(0

首先,进入命令行模式。作为服务器的linux一般都默认进入命令行模式的,当然,我们管理服务器也一般通过putty或者xshell等工具远程连接到服务器。在命令行中键入:

crontab -e

之后就会打开一个文件,并且是非编辑状态,则是vi的编辑界面,通过敲键盘上的i,进入编辑模式,就可以编辑内容。如果要提高效率,直接用php去执行本地php文件是最佳选择,例如:

00 */2 * * * /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index

这条语句就可以在每2小时的0分钟,通过linux内部php环境执行test/index,注意,这里可不是通过url访问,通过服务器环境来执行,而是直接执行,因为绕过了服务器环境,所以效率当然要高很多。

好了,点击键盘上的Esc键,输入“:wq”回车,这样就保存了设置的定时任务,屏幕上也能看到提示创建了新的定时任务。

crontab时间格式:

分 时 日 月 周
第1列表示分钟1~59 每分钟用或者 */1表示,/n表示每n分钟,例如*/8就是每8分钟的意思,下面也是类推
第2列表示小时1~23(0表示0点)
第3列表示日期1~31
第4列表示月份1~12
第5列标识号星期0~6(0表示星期天)

crontab规则详细

  • 1、每天6:00执行
0 6 * * * /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index

  • 2、每周六凌晨4:00执行
0 4 * * 6 /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 3、每周六凌晨4:05执行
5 4 * * 6 /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 4、每周六凌晨4:15执行
15 4 * * 6 /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 5、每周六凌晨4:25执行
25 4 * * 6 /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 6、每周六凌晨4:35执行
35 4 * * 6 /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 7、每周六凌晨5:00执行
5 * * 6 /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 8、每天8:40执行
40 8 * * * /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 9、每天8:30执行
30 8 * * * /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 10、每周一到周五的11:41开始,每隔10分钟执行一次
41,51 11 * * 1-5   /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index

1-59/10 12-23 * * 1-5   /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 11、在每天的10:31开始,每隔2小时重复一次
31 10-23/2 * * * /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 12、每天15:00执行
0 15 * * *  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 13、每天的10:30开始,每隔2小时重复一次
30 10-23/2 * * * /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 14、每天15:30执行
30 15 * * *  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 15、每天17:50执行
50 17 * * *  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 16、每天8:00执行
0 8 * * *  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 17、每天18:00执行
0 18 * * *  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 18、每天8:30执行
30 8 * * *  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 19、每天20:30
30 20 * * *  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 20、每周一到周五2:00
0 2 * * 1-5 /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 21、每周一到周五9:30
30 9 * * 1-5 /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 22、每周一到周五8:00,每周一到周五9:00
0 8,9 * * 1-5  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 23、每天23:59
59 23 * * *  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 24、每周六23:59
59 23 * * 6  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 25、每天0:30
30 0 * * *  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 26、每周一到周五9:25到11:35之间、13:00到15:00之间,每隔10分钟运行一次
25,35,45,55  9 * * 1-5  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index

5-59/10  10 * * 1-5  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index

5,15,25,35  11 * * 1-5  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index

*/10  13-15 * * 1-5  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 27、每周一到周五8:30、8:50、9:30、10:00、10:30、11:00、11:30、13:30、14:00、14:30、5:00分别执行一次
30,50 8 * * 1-5  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index

30 9 * * 1-5  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index

*/30 10-11 * * 1-5  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index

30 13 * * 1-5  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index

0,30 14-15 * * 1-5  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 28、每天23:50执行
50 23 * * *  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 29、每天10:00、16:00执行
0 10,16 * * *  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 30、每天5:30执行
30 5 * * *  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 31、每周一到周五9:30执行
30 9 * * 1-5  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 32、每周一到周五13:00执行
0 13 * * 1-5  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index

- 33、每天7:51执行

51 7 * * *  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 34、每天7:53、12:40分别执行一次
53 7 * * *  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index

40 12 * * *  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 35、每天7:55执行
55 7 * * *  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 36、每天8:10、16:00、20:00分别执行一次
10 8 * * *  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index

0 16 * * *  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index

0 20 * * *  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 37、每天7:57、8:00分别执行一次
57 7 * * *  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index

0 8 * * *  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index
  • 38、每天晚上7:00到凌晨5:00,每隔一個小時执行一次
0 19-23,0-5/1 * * *  /user/local/php/bin/php /home/wwwroot/yii(项目根目录) test/index

打赏

如果此文对你有所帮助,请随意打赏鼓励作者^_^

黄信强博客