简体   繁体   English

使用CRON运行PHP脚本时出现问题

[英]Trouble running a PHP script with CRON

I created a php file I want to run all the time. 我创建了一个我想一直运行的php文件。 I then created a basic wrapper I want CRON to run to insure the script is still running - and restart it if needed. 然后,我创建了一个我希望CRON运行的基本包装器,以确保脚本仍在运行-并在需要时重新启动它。

My crontab -e entry is like this: 我的crontab -e条目是这样的:

20 * * * * /var/www/bot/cron.php

The contents of cron.php look like this. cron.php的内容如下所示。

#!/usr/bin/php
<?php
@exec ('ps aux | grep loop', $output, $ret_var);

$running = false;
foreach ($output as $line)
{
    if (strpos($line, 'bot.php') !== false)
    {
        $running = true;
        break;
    }
}

if (! $running)
{
    @exec('/usr/bin/nohup php ' . __DIR__ . '/bot.php >/var/log/bot_out 2>&1 &');
}

die();

However, I'm having trouble getting this working. 但是,我无法正常工作。 Is there something I'm missing? 有什么我想念的吗?

I'm not getting anything on any error log, and /var/log/bot_out does show some runtime errors so I know PHP must be called. 我没有在任何错误日志中得到任何信息,并且/var/log/bot_out确实显示了一些运行时错误,因此我知道必须调用PHP。

PHP Warning:  Module 'apc' already loaded in Unknown on line 0
PHP Warning:  Module 'suhosin' already loaded in Unknown on line 0
20 * * * * /var/www/bot/cron.sh

then contents of cron.sh 然后是cron.sh的内容

#!/bin/bash
KP=$(pgrep -P 1 -f bot.php)
if [ "X$KP" = "X" ]
  then
    /usr/bin/nohup php PATH_TO_YOUR_SCRIPT/bot.php 
fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM