Log in

View Full Version : Resolved php cron



ggalan
12-31-2011, 01:18 AM
i found this script which checks every few minutes if a file exists.
can anyone help me understand the highlight. it looks like a shell script but how does it trigger the php to look for the file?


#!/usr/local/bin/php -q
<?PHP
if( !file_exists( "./.pending-hup" ) )
{
exit;
}
$qmailSendPID = intval( `ps -axw | grep qmail-send | grep -v grep | awk '{ print $1 }'` );
if( $qmailSendPID )
{
if( !posix_kill( $qmailSendPID, 1 ) )
{
echo "ERRO: posix_kill( qmail-send, HUP ) falhou.\n";
}
else
{
@unlink( "./.pending-hup" );
}
}
?>

djr33
12-31-2011, 07:46 AM
I believe that line says that the following code should be run using the ".../php" program-- and that the -q flag should be used. I have no idea what "q" means, though. (It's probably just a default mode, like no errors or setting a maximum time limit).

But something else would need to make it occur every few minutes. That's just a one-time script. You can usually set up cron jobs in your cpanel or whatever equivalent you have. If not, you may need to set them up in apache directly via the command prompt.

ggalan
12-31-2011, 05:48 PM
code should be run using the ".../php" program
do you mean the path to php's directory?

traq
12-31-2011, 07:39 PM
yes; #!/usr/local/bin/php -q tells the server which program to use to execute the script (PHP, in this case). Most times, you don't end up needing it; but you do need to make sure that it's the correct path to your installation of php.

the -q flag suppresses HTTP headers (since you're not outputting html, you don't want them).

ggalan
12-31-2011, 08:05 PM
so then this file should be saved with which extension?
cron.sh
or
cron.php

traq
12-31-2011, 08:27 PM
what extension did the original script have?

I always used the .php extension; I've never used the #! directive (though the .php extension might be the reason I never needed it).

My instinct would be to either:
1. keep the #! directive and save the script as cron.sh (using the CGI interface, so you'd need the directive)
2. remove the #! directive and save the script as cron.php (simply using the cron to trigger a PHP script)
As I said, however, I don't know for sure. You'll need to try it out and let us know how it works... :)