linux crontab scheduled tasks

1. Confirm whether crontab is installed:
Execute the crontab command if the command not found is reported, it means that it is not installed
or use the rpm -q vixie-cron command
2. Install crontab
and execute yum install -y vixie-cron
3. Confirm whether the installation is successful:
Execute crontab -l
4. See if it is set to automatically start
chkconfig --list crond

7 runlevels under        Linux :

0 The system is in shutdown state, and the default operating level of the system cannot be set to 0, otherwise it will not start normally and the machine will be shut down.

1 Single-user working status, root authority, used for system maintenance, remote login is prohibited, just like the safe mode login under Windows.

2 Multi-user state, no NFS support.

3 Complete multi-user mode, with NFS, enter the console command line mode after logging in.

4 The system is not used, and it is generally not used for reservation. It can be used to do some things in some special cases. For example, when the laptop's battery runs out, you can switch to this mode to make some settings.

5X11 console, enter graphical GUI mode after login, X Window system.

6 The system is normally shut down and restarted. The default run level cannot be set to 6, otherwise it will not start normally. Running init 6 the machine will reboot.
5. Start crontab
service crond start

First, confirm the location of the PHP executable -- for most Linux systems, it will almost certainly be /usr/bin/php. If you are unsure of its location, type which php at the command line and see the response.

Second, type the following code, making sure to replace /usr/bin/php with the actual path to the PHP executable.

<?php
#!/usr/bin/php -q
print date("Y-m-d H:i:s")."\n";
?>

 

权限与转码

#chmod +x test.php
#dos2unix test.php

 

crontab  -e

按其语法定义即可

例如5分钟运行一次

*/5 * * * * /website/test.php >> /dev/null &

或者另外一种形式直接在Crontab中配置

*/2 * * * * /usr/bin/php -f  /var/www/html/*.php

====================================

用了两天时间完成这个任务,其实一点也不难,只是因为对PHP不熟悉,才花了这么长时间。
本来以前研究过crond的,可是和PHP结合还是第一次,第一次嘛,原谅自己了。
就是可惜了我一个周末的休息时间...
OK,以下是配置步骤:
1.Linux下必须编译安装PHP解释器,在安装完成后,要将安装目录下${PHP}/bin/php 文件复制到
/usr/bin/下,并使用chmod +x ./php 为其添加可执行权限,ok了,
然后在linux命令行下输入:php -q *.php
执行一个php程序,以验证是否解释器工作正常
2.发布php文件:将编写好的php程序发布到apache的发布目录:如:/var/www/html下
加权限:chmod +x /var/www/html/*.php
3.创建定时任务:
(1)启动linux定时服务:service crond start
(2)查看当前定时任务: crontab -l
(3)添加新定时任务:crontab -e
在打开的文件中编辑:
*/2 * * * * /usr/bin/php -f  /var/www/html/*.php
表示每2分钟执行一次/var/www/html/*.php文件
(注: -f 很重要,不能修改成 -q )

日志
默认情况下,crontab中执行的日志写在/var/log下,如:

#ls /var/log/cron*

看 /var/log/cron这个文件就可以,可以用tail -f /var/log/cron观察

crontab特殊的符号说明:

"*"代表所有的取值范围内的数字。特别要注意哦!

"/"代表每的意思,如"*/5"表示每5个单位

"-"代表从某个数字到某个数字

","分散的数字

crontab文件的使用示例:

30 21 * * * 表示每晚的21:30

45 4 1,10,22 * * 表示每月1、10、22日的4 : 45

10 1 * * 6,0 表示每周六、周日的1 : 10

0,30 18-23 * * * 表示在每天18 : 00至23 : 00之间每隔30分钟

0 23 * * 6 表示每星期六的11 : 00 pm

* */1 * * * 每一小时

* 23-7/1 * * * 晚上11点到早上7点之间,每隔一小时

* 8,13 * * 1-5 从周一到周五的上午8点和下午1点

0 11 4 * mon-wed 每月的4号与每周一到周三的11点

0 4 1 jan * 一月一号的4点

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326536106&siteId=291194637