安装crontab:
网上有很多例子,安装启动服务等。
crontab常用命令:
crontab -e:编辑计时器设置
crontab -l:查看计时器列表
crontab -r:删除计时器设置
crontab -u:指定用户名
crontab格式
从网上搜的一些例子
基本格式:
* * * * * command
分 时 日 月 周 命令
第1列表示分钟1~59 每分钟用*或者*/1表示
第2列表示小时1~23(0表示0点)
第3列表示日期1~31
第4列 表示月份1~12
第5列标识号星期0~6(0表示星期天)
第6列要运行的命令
# Use the hash sign to prefix a comment
# +—————- minute (0 – 59)
# | +————- hour (0 – 23)
# | | +———- day of month (1 – 31)
# | | | +——- month (1 – 12)
# | | | | +—- day of week (0 – 7) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed
30 21 * * * /etc/init.d/nginx restart
每晚的21:30重启nginx。
45 4 1,10,22 * * /etc/init.d/nginx restart
每月1、10、22日的4 : 45重启nginx。
10 1 * * 6,0 /etc/init.d/nginx restart
每周六、周日的1 : 10重启nginx。
0,30 18-23 * * * /etc/init.d/nginx restart
每天18 : 00至23 : 00之间每隔30分钟重启nginx。
0 23 * * 6 /etc/init.d/nginx restart
每星期六的11 : 00 pm重启nginx。
* */1 * * * /etc/init.d/nginx restart
每一小时重启nginx
* 23-7/1 * * * /etc/init.d/nginx restart
晚上11点到早上7点之间,每 隔一小时重启nginx
0 11 4 * mon-wed /etc/init.d/nginx restart
每月的4号与每周一到周三 的11点重启nginx
0 4 1 jan * /etc/init.d/nginx restart
一月一号的4点重启nginx
*/30 * * * * /usr/sbin/ntpdate 210.72.145.20
每半小时同步一下时间
添加定时任务:
thinkphp可以在终端中直接用php执行根目录的index.php文件,后边可追加参数
先在终端测试是否可行
$ php /home/vagrant/Code/www/index.php Home/Index/test
结果报错:
PHP Warning: require(./ThinkPHP/ThinkPHP.php): failed to open stream: No such file or directory in /home/vagrant/Code/www/index.php on line 26
Warning: require(./ThinkPHP/ThinkPHP.php): failed to open stream: No such file or directory in /home/vagrant/Code/www/index.php on line 26
PHP Fatal error: require(): Failed opening required './ThinkPHP/ThinkPHP.php' (include_path='.:/usr/share/php') in /home/vagrant/Code/www/index.php on line 26
Fatal error: require(): Failed opening required './ThinkPHP/ThinkPHP.php' (include_path='.:/usr/share/php') in /home/vagrant/Code/www/index.php on line 26
修改index.php 改为绝对路径
// 检测PHP环境
if (version_compare(PHP_VERSION, '5.3.0', '
die('require PHP > 5.3.0 !');
}
define('APP_ROOT', dirname(__FILE__));
// 开启调试模式 建议开发阶段开启 部署阶段注释或者设为false
define('APP_DEBUG', true);
// 定义应用目录
define('APP_PATH', APP_ROOT.'/Application/');
// 引入ThinkPHP入口文件
require APP_ROOT.'/ThinkPHP/ThinkPHP.php';
// 亲^_^ 后面不需要任何代码了 就是如此简单
?>
再次执行php 成功。
终端中执行crontab -e,添加定时任务(第一次会让选择编辑器,以后也可用“select-editor”修改)
* * * * * php /home/vagrant/Code/www/index.php Home/Index/test
发现没有执行,将php也修改为绝对路径
* * * * * /usr/local/php/bin/php -f /home/vagrant/Code/www/index.php Home/Index/test