Reputation: 57184
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
Upvotes: 0
Views: 496
Reputation: 711
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
Upvotes: 1