Jump to content

cron job


dannyd

Recommended Posts

Here is a full description on how to use cronjobs: http://www.hmug.org/man/5/crontab.php

 

There are 2 parts to a cronjob: when and what.

-The when is easy. It will be '0 0 * * *'. That is every day at midnight.

-The what is not always so easy, as not every PHP install is the same. Basically we want to test running it from the command line first. You can read about all of the PHP switches with typing 'php -h' on the command line. But, it should end up looking something like this:

/usr/bin/php -f /full/path/to/script.php

Make sure you use the full path to both the PHP executable and the PHP script. If you are unsure of the path to the PHP executable, type 'which php' and it should tell you.

 

Putting the above together, your cronjob should look like this:

0 0 * * * /usr/bin/php -f /full/path/to/script.php

 

REMEMBER: It will run the job as whatever user you are logged in as. So be careful if you are logged in as a privileged user.

 

Hope that helps  :)

 

Link to comment
Share on other sites

Part one is correct. It will run the command every day at 10:30am

 

Part two *may* work. There are 2 ways to execute a PHP script from the command line:

 

-One way (the way you just posted) is to execute the script directly. This requires that the PHP compiler is in the systems PATH environmental variable. It also requires that you have the following code as the first line of the script:

#!/usr/bin/php

To the operating system, your script is just a text file. It doesn't know what do with it. This line tells the system to use the PHP compiler to run the script. Finally, you must make sure the script has executable permission. You can do this by running the following command:

chmod +x test

 

-The other way (which is the way I posted before and my preferred way) is to execute the PHP compiler directly and pass your script as an argument to it. This does not need any of the previous options requirements.

 

 

Either way, whatever you choose to be part two of the cronjob, it needs to work when execute it directly on the command line. So try executing it that way first, make sure it works, then use it in your cronjob.

 

 

 

Both ways are discussed here: http://us.php.net/features.commandline

Link to comment
Share on other sites

  • 2 weeks later...

In my cron log im getting this error:

 

Mar  6 11:57:01 intra1 crond[2286]: (tmp.XXXXGHJVjz) ORPHAN (no passwd entry)

Mar  6 11:57:01 intra1 crond[2286]: (root) RELOAD (cron/root)

Mar  6 11:57:01 intra1 crond[2286]: (tmp.XXXXFqiL6E) ORPHAN (no passwd entry)

 

Im guessing the cron isnt running as root. How can i specify a cron job to run as the root user ?

 

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.