Linux定时任务Crontab执行PHP脚本
[root@jiangbo ~]# crontab -u root -l no crontab for root [root@jiangbo ~]# crontab -u root -e no crontab for root - using an empty one crontab: installing new crontab [root@jiangbo ~]# crontab -u root -l */1 * * * * ls >> /tmp/ls.txt [root@jiangbo ~]#
*/1 * * * * /usr/bin/php /var/www/html/testcrontab.php
<?php $myfile = fopen("/var/www/html/newfile.txt", "a+");//这里写相对路径不行 //or die("Unable to open file!!"); $txt = "Bill Gates\n"; fwrite($myfile, $txt); $txt = "Steve Jobs\n"; fwrite($myfile, $txt); fclose($myfile); ?>
[root@jiangbo ~]# /usr/bin/php /var/www/html/xiaochu/test/testGame.php
PHP Warning: require_once(../Amfphp/app/util/Mpdo.class.php): failed to open stream: No such file or directory in /var/www/html/xiaochu/test/testGame.php on line 2
PHP Fatal error: require_once(): Failed opening required '../Amfphp/app/util/Mpdo.class.php' (include_path='.:/usr/share/pear:/usr/share/php') in /var/www/html/xiaochu/test/testGame.php on line 2
[root@jiangbo ~]# crontab -l
*/1 * * * * /usr/bin/php /var/www/html/xiaochu/test/testGame.php
You have new mail in /var/spool/mail/root
[root@jiangbo ~]#
/usr/bin/php 这样执行的话好像不认相对路径,只能通过URL方式定时执行了
[root@jiangbo ~]# /etc/init.d/mysqld start Another MySQL daemon already running with the same unix socket. 正在启动 mysqld: [失败] [root@jiangbo ~]# /etc/init.d/mysqld start 正在启动 mysqld: [确定] [root@jiangbo ~]#
删除/var/lib/mysql/mysql.sock 文件就行了
/sbin/service crond stop //关闭服务
/sbin/service crond restart //重启服务
/sbin/service crond reload //重新载入配置
/sbin/service crond start
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root //如果出现错误,或者有数据输出,数据作为邮件发给这个帐号
HOME=/ //使用者运行的路径,这里是根目录
# run-parts
01 * * * * root run-parts /etc/cron.hourly //每小时执行/etc/cron.hourly内的脚本
02 4 * * * root run-parts /etc/cron.daily //每天执行/etc/cron.daily内的脚本
22 4 * * 0 root run-parts /etc/cron.weekly //每星期执行/etc/cron.weekly内的脚本
42 4 1 * * root run-parts /etc/cron.monthly //每月去执行/etc/cron.monthly内的脚本