Jump to content
xisto Community
OpaQue

Cron Jobs - Cron Jobs In Cpanel Cron Jobs : Cron Jobs using PHP

Recommended Posts

How to setup Basic Cron Jobs.

 

Ok. Many people are yelling that they cannot configure the crons properly.

Here is a short tutorial which will help you set them. I have tried my best to explain the concept but there can be more advanced configurations.

 

Basically you will want to run a PHP script file in specific intervals. Suppose you want to execute a php file called maintanence.php every one hour. This is what you do :-

 

[ :P This tutorial is for noobs, Dont go into detail. Just do as instructed :P ]

 

The CRON Command is in the Following Format

 

[ Minute - Hour - Day - Month - Weekday ] - Command

The COMMAND, can be broken down in

[PATH OF PHP] [ARGUMENTS] [PATH OF PHP SCRIPT]

So the COMPLETE CRON command can be written as

 

[ Minute - Hour - Day - Month - Weekday ]  [PATH OF PHP] [ARGUMENTS] [PATH OF PHP SCRIPT]

The timing is spedified using * symbols

* * * * * => Execute every minute0 * * * * => Execute every Hour0 0 * * * => Execute every mid-night0 0 0 * * => Execute every Month0 0 0 0 * => Execute every Weekday

If you did not understand anything till now.. Good, this means you are a noob.. Read ahead :P

 

Since this is a UNIX command, You will have to Mention the PATH of PHP. At Xisto,

PATH TO PHP  : /usr/local/bin/php
( it also same at Xisto, if users over there want to use it )

 

These are the Possible Command line Arguments you can use. This will effect the output. In our case, we will use the -q ( Quiet mode ) argument.

  -a               Run interactively  -b <address:port>|<port> Bind Path for external FASTCGI Server mode  -C               Do not chdir to the script's directory  -c <path>|<file> Look for php.ini file in this directory  -n               No php.ini file will be used  -d foo[=bar]     Define INI entry foo with value 'bar'  -e               Generate extended information for debugger/profiler  -f <file>        Parse <file>.  Implies `-q'  -h               This help  -i               PHP information  -l               Syntax check only (lint)  -m               Show compiled in modules  -q               Quiet-mode.  Suppress HTTP Header output.  -s               Display colour syntax highlighted source.  -v               Version number  -w               Display source with stripped comments and whitespace.  -z <file>        Load Zend extension <file>.

The Path of the PHP file Must be Complete absolute path.

If you have an Account at TRAP, And your USERNAME is "tom", Your path will be

"/home/tom/public_html/"

 

I assume you are familier with the PUBLIC_HTML directory. That is the Root folder where you store your html files.

 

So lets say that Tom wants to execute my script "maintenance.php" every hour. And it is stored in "public_html/cron/maintenance.php";

 

So the Complete CRON command would be,

 

0 * * * * /usr/local/bin/php -q /home/tom/public_html/cron/maintenance.php

If tom wants to execute it every minute, he would use.

 

* * * * * /usr/local/bin/php -q /home/tom/public_html/cron/maintenance.php

If tom wants to execute it every Month, he would use.

 

0 0 0 * * /usr/local/bin/php -q /home/tom/public_html/cron/maintenance.php

There are more Complex forms of Assigning the TIMINGS for these scripts. You can go to CPANEL => Cron Jobs => Standard and set exact time when the script will be executed.

 

 

Also, this method used php, you can also use Curl .

 

-OpaQue

Share this post


Link to post
Share on other sites

if tom wants to execute it every minute, he would use.

 

0 0 * * * /usr/local/bin/php -q /home/tom/public_html/cron/maintenance.php

43926[/snapback]

cool tutorial, thanks a lot

 

but shouldn't you use * * * * * for the time if you wanted it to run every minute?

Share this post


Link to post
Share on other sites

I have been looking for this information for some time now. Thanx!All other Cron help was for experts. I could not figure the correct command and can now see what is ment by argument. Will be trying this info on my site. You see I need it to update information every 2 weeks.

Share this post


Link to post
Share on other sites

Errr... what's a cron  :P

44206[/snapback]

It does an action every "X B)

When you have a file : update.php

And you want to load it every day at midnight. You don't have to go on this page yourself. The cron does it and you don't have to do anything, it is really usefull B) You can insert some SQL requests on your file update.php, and it will be done everyday.

Exemple :

update.phpmysql_query("UPDATE members SET point = point + '1'");
If the cron is activated on the page update.php, everyday your members will get 1 more point :D

Share this post


Link to post
Share on other sites

Ok, so I still dont get the point of cron jobs. Are they just server settings that allow you to execute a command within a set ammount of time?

Share this post


Link to post
Share on other sites

Ok, so I still dont get the point of cron jobs. Are they just server settings that allow you to execute a command within a set ammount of time?

44280[/snapback]

Exactly ! :D It can be useful but it isn't essential. If you don't need it, don't use it :P

Share this post


Link to post
Share on other sites

I have a question, what if we don't have a cpanel, where do we set up the cron job?
This program has led me into making my own PHP cron >_>
I have a MySQL table named "other" with fields "name'" and "content".

if(mysql_result(mysql_query('content','other','name = \'cron\' AND content <= UNIX_TIMESTAMP()-60*60*24'), 0)){  // cron stuff goes here   // Update the cron time  mysql_query('UPDATE other SET content = UNIX_TIMESTAMP WHERE name = \'cron\'');}

This is sort of the way my script is, it's a good script 'cause it doesn't take much time out of the user. It's in the head file so it'll get run when needed. The cron stuff gets runned every day.

Share this post


Link to post
Share on other sites

Pretty interesting, I've never been with a host that lets me use Cron jobs before, and they sound pretty cool. dunno if I'll ever need to use em, but itz still an interesting how-to. Great job!

Share this post


Link to post
Share on other sites

it still doesn't work for me: my full nightly command (midnight EST):0 19 * * * /usr/local/bin/php -q /home/gamefaqs/public_html/cron_omgwtflolbbq/daily_purge.phpdid i forget something? am i supposed to put a "GET" in somewhere?please help! this is really important for my site!thanx in advance

Share this post


Link to post
Share on other sites

Finally... but I still don't get it. ;)7 different people tried it and none of them worked. 7 people.and none worked.I'm using the LUE2 source and I need cron jobs to run important parts of it.I need it for something called karma and something called gems, which is the curreny needed to buy certain items I put in the shop. It's pretty interesting; check it out some time.Anyway, I also have a game on there to, which also needs crons to give people experciance points and gold to buy stuff from the game's shop.But yea, if you could help me with it OpaQue, I'd really appreciate it. And I'll let you come to my site anytime you'd like. >_>

Share this post


Link to post
Share on other sites

OK im trying these cron jobs out on my cpanel too and I've gotten mixed messages.

I have a php file that just needs to be processed in order for it to work. So all i would do is type in the address of the php file, and it would start processing log files. I want to do this remotely from my webhost site. So I was told once that i could use

#wget http://forums.xisto.com/no_longer_exists/

and that would run that page at the time I wanted. But it didn't do anything.

Then i saw someone use:

get http://forums.xisto.com/no_longer_exists/

tried it, nothing happened.

so is there a way to just have the cron job access a remote file by the webaddress?

The way I have it set up is this.

3 Tasks.
Task 1) call on a remote url that is on my home pc to do a log file parse. This is done simply by accessing the php file through any browser, thats it.

Task 2) call on a remote url to do a database backup of the database on that server. Now thats the logs are parsed, it updated the database with all the parsed info.

Task 3) call on a local file to do a database restore to the mirrored database on the webserver, i.e. make the webserver db match my homepc db.


This all works fine if i do it manually. but what command allows the cron job to do it?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.