linux下使用定时脚本的执行php文件,防止超时出现502页面。
首先,写入shell代码执行php方法,下面是crontab_export.sh里的代码
#!/bin/bash
#一分钟计划任务
work_dir=$(dirname "$0")
#执行另外一个sh文件
#/bin/sh $work_dir/crontab_real.sh
function run() {
ps -elf | grep "$work_dir/../web/index.php $1" | grep -v grep
if [ $? -ne 0 ]; then
/usr/bin/php $work_dir/../web/index.php $@
fi
}
#这里是我的php的方法
run /cron/export_data/Stat_all_role_level_active_nums20180403/run
第二,添加一分钟定时任务
编辑 crontab文件
vi /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
*/1 * * * * root sh /home/wwwroot/work/zjss/trunk/backstage/cron-sh/crontab_export.sh
当然,也可以定其实时间的任务,上面的minute对应的是分钟,hour小时,day天数,month月份,week星期几。
第三,查看执行情况
tail -f /var/log/cron
另外需要使用到的命令有:
service crond start //启动服务
service crond stop //关闭服务
service crond restart //重启服务
service crond reload //重新载入配置
service crond status //查看是否执行