linux下定时任务执行php文件

linux下执行定时任务再不过平凡了,这个应该要会的

首先要安装如果没有的话

  • service crond status //查看是否已经有了这个服务
  • yum install crontabs //一般是系统已经自带了不需要安装的
  • 说明:
    /sbin/service crond start //启动服务
    /sbin/service crond stop //关闭服务
    /sbin/service crond restart //重启服务
    /sbin/service crond reload //重新载入配置

编辑定时任务 crontab

  • -e  编辑该用户的计时器设置。
  • -l  列出该用户的计时器设置。
  • -r  删除该用户的计时器设置。
  • -u<用户名称>  指定要设定计时器的用户名称。

基本格式

分钟 小时 日 月 星期 命令

* * * * * * /usr/local/php/bin/php /xxx.php

第1列表示分钟1~59 每分钟用*或者 */1表示
第2列表示小时1~23(0表示0点)
第3列表示日期1~31
第4列 表示月份1~12
第5列标识号星期0~6(0表示星期天)
第6列要运行的命令
第7列运行的文件位置

  • /1 * * * * curl http://live.**.com/redis_to_mysql.php //执行curl

执行不成功的问题尝试

  • 修改需要执行文件权限 755 ...
  • tail -f /var/log/cron //查看文件是否有执行
  • */1 * * * * ./etc/profile; php /usr/local/httpd/htdocs/cronrun.php //要加上./etc/profile文件否则会无效
  • 如果你在用文件写入测试用./代表当前路径会无法写入,应该用DIR来才可以成功
  • tail -f /var/log/cron //查看定时器执行情况

成功的配置文件

  • */1 * * * * curl http://192.168.0.105/cronrun.php
  • */1 * * * * /usr/local/php/bin/php /usr/local/httpd/htdocs/cronrun.php

带用户名和密码的访问

  • curl -u username:password http://example.com

你可能感兴趣的:(linux下定时任务执行php文件)