CRON Jobs and PHP

deadimp

New Member
Messages
249
Reaction score
0
Points
0
CRON jobs according to cPanel:
Cron jobs allow you to automate certain commands or scripts on your site. You can set a command or script to run at a specific time every day, week, etc. For example, you could set a cron job to delete temporary files every week so that your disk space is not being used up by those files.

This tutorial is here to provide basic information on executing php scripts for your CRON jobs.
There are two parts to your crontab: the command to run, and the interval you run it on. I won't go over how to set the intervals because cPanel has a nice interface that makes it simple for beginners or advanced users.

The way to have CRON execute your php script is to call the php interpereter and tell it to process that file. Keep in mind that it's a command and not a file (unless it's a shell/executable) you're entering. Example:
Code:
php -q /home/username/bin/script.php

For your php script, you enter the path on the server (according to the *nix filesystem), not as a URL.

You usually don't want to expose your CRON job script in your html/web directory (/public_html, /www), since the script most likely does maintainence that could be cumbersome and might be the base of a DOS attack or an even larger security risk.
I just put mine in the /etc directory. That way no one can access/execute it via normal HTTP access. [/bin may be a better choice. I'm not sure if really matters past organization.]

Also remember that in this way PHP is not running as a CGI module, meaning you won't have access to the environment variables you're used to, like $_GET, $_POST, etc.
If you want to refine your control, keep in mind that you have access to the arguments passed to the script via $_SERVER['argv'] (which are like $_GET). You can pass commands like this: "php -q /blah/script.php -clear".
If you used print_r($_SERVER['argv']), it would return a printout for array('/blah/script.php','-clear').

NOTES:
* I don't know what the -q flag does, though... Corey had suggested it here.
Can't find it via `php --help` on my XAMPP installation.
* You might be able to run the script just using the script's name without the php command - assuming the system will recognize its type via filename extension - but I'm not sure. I can't test it at the moment because CRON is disabled for me right now (1/30/08).
 
F

Fahad

Guest
Nice tut...
Curious - what would happen if you used an echo()?
Btw, it wouldn't recognize by extension, as this is linux, not windows.
 

deadimp

New Member
Messages
249
Reaction score
0
Points
0
Curious - what would happen if you used an echo()?
Gah! Forgot to mention that!

Any output from your script (i.e. echo) is sent to the email you've specified in your CRON options.
If you have CRON run a command `php -q /home/user/bin/test.php` with this as the file:
Code:
<?php
echo "This is a test";
?>
You'll receive a message like this on each execution:
Code:
Subject: Cron <user@server> php -q /home/user/bin/test.php
Sender: Cron Daemon <root@server>
This is a test
I would suggest creating a filter on your email client to lump all of your CRON messages into one folder, deleting them every so often.

Btw, it wouldn't recognize by extension, as this is linux, not windows.
Thanks for the info!
I'm wondering, though, if you could use specify execution options using the 'shell line' (like what you would do with a bash script) like so:
Command:
Code:
/home/user/bin/test.php
File:
Code:
#!/bin/php
<?php
//Not sure where php is...
echo "This is a test";
?>
Don't know how that would work out in php with the tags and such... Maybe someone else could shed some light on that.
 
Last edited:
F

Fahad

Guest
That would work, yes, but it would mean you HAVE to execute it without the php -q
 

deadimp

New Member
Messages
249
Reaction score
0
Points
0
Well, you could still execute it with the shell lines as a direct command to php, but the line would just be included on the output as some 'extra noise' if you don't clear the output buffer (something like ob_clean() on the first line of the php code). Of course, that would be too much work to be worth not manually typing out the command.
 

oiwio

New Member
Messages
214
Reaction score
0
Points
0
Nice tutorial. Ive been looking for one for a while

I have one question though, if you want to connect to the database, do you have to use a module, or a normal database query?
 

deadimp

New Member
Messages
249
Reaction score
0
Points
0
You can use just about any library/module you normally have access to under your CGI environment (ex. viewing your web pages).
As fahadsadah pointed out, you can use the mysql module.
 

Dan

Active Member
Messages
1,258
Reaction score
0
Points
36
Thanks for this tutorial!! Very Useful
Added to your Rep :)
 

FalseHope

Active Member
Messages
1,639
Reaction score
0
Points
36
Damn. nice tutorial! I never did know what they were for, well I knew they were some sort of automatic system. Thanks for the tutorial :D
 

bleachrp

New Member
Messages
46
Reaction score
0
Points
0
I'm Stuck >.<

Never mind.. I got it :) Yay!
 
Last edited:

mephis

New Member
Messages
39
Reaction score
0
Points
0
That would work, yes, but it would mean you HAVE to execute it without the php -q

you can pass args on the first line if I'm not mistaken:
Code:
#!/bin/php -q

(your code)

and if you're not sure where php is located you can probably use:
Code:
#!`which php` -q

(your code)
 
Last edited:

mattura

Member
Messages
570
Reaction score
2
Points
18
the -q means 'quiet mode' ie with reduced/no error/echo outputs.
Edit:
It should be pointed out that mail has been disabled in cron (presumably to prevent people from sending loads of spam)
 
Last edited:
Top