简体   繁体   中英

Trouble running a PHP script with CRON

I created a php file I want to run all the time. I then created a basic wrapper I want CRON to run to insure the script is still running - and restart it if needed.

My crontab -e entry is like this:

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

The contents of cron.php look like this.

#!/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.

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

#!/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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