djfox Posted January 2, 2008 Share Posted January 2, 2008 I`ve read pages on cron jobs and wanted to ask some questions before I get one started and end up having mistakes. 1. Am I correct in assuming that you use cron jobs to execute a php file that would do a certain thing that you want done at a time interval? 2. The "command to run" field when setting up a cron job, is that where you would enter a php file that you want ran? 3. If yes to 1, do I need to do anything special with the php file because cron job would be using it or is just the normal php coding I use would be fine? 4. Can I have setup more than one cron job? Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted January 2, 2008 Share Posted January 2, 2008 1. Am I correct in assuming that you use cron jobs to execute a php file that would do a certain thing that you want done at a time interval? Yes, a "cron job" is a task that a *nix OS executes at periodic intervals. In windows it's called a "scheduled task". 2. The "command to run" field when setting up a cron job, is that where you would enter a php file that you want ran? Yes, but you will also want to put the path to the executeable. a very simple example: * * * * * /path/to/php/binary /path/to/php/script 3. If yes to 1, do I need to do anything special with the php file because cron job would be using it or is just the normal php coding I use would be fine? Write your code for the command line. http://www.php.net/command_line Although, I have seen people use wget to execute a script as though it was a web script, rather than command line. 4. Can I have setup more than one cron job? As far as I know, you can have unlimited. Quote Link to comment Share on other sites More sharing options...
djfox Posted January 2, 2008 Author Share Posted January 2, 2008 3. If yes to 1, do I need to do anything special with the php file because cron job would be using it or is just the normal php coding I use would be fine? Write your code for the command line. http://www.php.net/command_line Although, I have seen people use wget to execute a script as though it was a web script, rather than command line. So I would first need have a file, say petstatsedit.php which would have the actual work: <?php session_start(); //Date: January 2 2007 //For: www.secrettrance.net //Description: Edit Pet Stats include("dbcon.php"); require_once "design.php"; require_once "auth.php"; $Title = "Secret Trance: Edit Pet Stats"; require_once "header.php"; require_once "hidestatus.php"; mysql_query("update ownedpets set food = (food - 10)"); mysql_close($con); ?> Then I would also need a file, say petcronjob.php, which would be: <?php -f petstatsedit.php ?> Then in the cron job "command to run" field, I would enter http://secrettrance.net/petcronjob.php Am I correct? Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted January 2, 2008 Share Posted January 2, 2008 Session's don't work from the command line. And you really don't want your cron job to return anything unless it's an error. Then in the cron job "command to run" field, I would enter http://secrettrance.net/petcronjob.php If you want it to execute in the "web" sense, then you would have to use wget... * * * * * /usr/bin/wget http://http://secrettrance.net/petstatsedit.php Note that I didn't use the petcronjob.php file. If you aren't familiar with php from the command line, now would be a good time to get acquainted. Quote Link to comment Share on other sites More sharing options...
trq Posted January 2, 2008 Share Posted January 2, 2008 Am I correct? No. Cron jobs should directly execute scripts or commands via the shell. There is not normally a good reason to call your php script via a url. Quote Link to comment Share on other sites More sharing options...
djfox Posted January 2, 2008 Author Share Posted January 2, 2008 If you aren't familiar with php from the command line, now would be a good time to get acquainted. Where would I learn more about that? * * * * * /usr/bin/wget http://http://secrettrance.net/petstatsedit.php Do I need to replace * * * * * /usr/bin/ with anything? Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted January 2, 2008 Share Posted January 2, 2008 http://www.php.net/command_line Quote Link to comment Share on other sites More sharing options...
trq Posted January 2, 2008 Share Posted January 2, 2008 Also note that unless you speicically need to use a feature of php, theres usually no need for it. What exactly do you want these (cron job) scripts to do? Quote Link to comment Share on other sites More sharing options...
djfox Posted January 2, 2008 Author Share Posted January 2, 2008 Also note that unless you speicically need to use a feature of php, theres usually no need for it. What exactly do you want these (cron job) scripts to do? What I`m wanting it to do is subtract 10 from food of all entries in ownedpets table of my database, every 2 hours. Quote Link to comment Share on other sites More sharing options...
trq Posted January 2, 2008 Share Posted January 2, 2008 Well, its probably easiest for you to use php as it has a good interface to mysql. Especially if php is the language you know best. Just letting you know that that this could be done directly from bash (the default linux shell).... foodupdate.sh #!/bin/bash mysql -u<username> -p<password> -e 'USE <yourdb>;UPDATE ownedpets SET food=food-10;' Replace <username> with your username and <password> with your password and <yourdb>........ Then your cron would look like.... * */2 * * * /path/to/foodupdate.sh Sorry if this just confuses the issue more.:-) You could then simplify this even further by putting the entire thing into your crontab. eg; * */2 * * * mysql -u<username> -p<password> -e 'USE <yourdb>;UPDATE ownedpets SET food=food-10;' And thats the whole job done. No need for any scripts at all. Quote Link to comment Share on other sites More sharing options...
djfox Posted January 2, 2008 Author Share Posted January 2, 2008 You could then simplify this even further by putting the entire thing into your crontab. eg; * */2 * * * mysql -u<username> -p<password> -e 'USE <yourdb>;UPDATE ownedpets SET food=food-10;' That does look much easier. The asterisks are still confusing me. When entering in the username and password for the line there, would I do something like mysql - u<george> - p<chips> - e 'USE <pets>;blahblah' Or would the info be entered without the signs? Quote Link to comment Share on other sites More sharing options...
trq Posted January 2, 2008 Share Posted January 2, 2008 The asterisks represent the different time parts. eg; [pre] * * * * * - - - - - | | | | | | | | | +----- day of week (0 - 6) (Sunday=0) | | | +------- month (1 - 12) | | +--------- day of month (1 - 31) | +----------- hour (0 - 23) +------------- min (0 - 59) [/pre] And no, you do not need the <>'s surrounding those values. Quote Link to comment Share on other sites More sharing options...
djfox Posted January 2, 2008 Author Share Posted January 2, 2008 I got an error message (saying it couldn`t connect). Did I enter everything correctly: (username and password changed since this is all public and I don`t want people having their hands on them) mysql -u username -p password -e 'USE secrett1_artgallery;UPDATE ownedpets SET food=food-10;' Quote Link to comment Share on other sites More sharing options...
trq Posted January 2, 2008 Share Posted January 2, 2008 There can be no spaces after -u or -p. mysql -uusername -ppassword -e 'USE secrett1_artgallery;UPDATE ownedpets SET food=food-10;' Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.