Jump to content

[SOLVED] Questions about cron jobs


djfox

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;'

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.