homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

How to create a PHP cron job
How do i automate a php script/programme

 9:43 am on Aug 7, 2012 (gmt 0)

How all,
Can someone give an idea of this? I have a script intended to run every day at 18:30 local time that takes data from a mysql table and inserts it in an another table but on an oracle server. The test has been conducted locally on the two database with success and i run it at command line like this: php /path/to/php/file

How can i make it run automatically every day at 18h:30 local time (please i'd appreciate if you could write here a working code). Also another issue i've not tested yet is the timeout issue. If for instance the data is too large to be retreived from mysql and inserted in oracle on the other side within 60 seconds, is the entire dataset going to be inserted in the oracle table?

Thanks for your kind and prompt reply



 10:33 am on Aug 7, 2012 (gmt 0)

Please bear in mind I've got limited experience in this area and the timeout issue needs someone with better mySQL skills than me but

You probably need to think of this as two tasks. One a php script to transfer the data, the other a cron task to execute the php script.

The php script would have an INSERT command into the Oracle table using a select statement from the mySQL table. Thinking about it, it may be easier to run this from the Oracle server.

Setting up the cron task is a bit convoluted and differs between hosting interfaces I believe. Check out this quick reference guide - [adminschoice.com...]

I know it's not a full solution but hopefully it will get you underway.


 11:28 am on Aug 7, 2012 (gmt 0)

There's more than one way of invoking the PHP parser so you'll likely need to experiment a bit to find the one that works for you.


 7:22 am on Aug 8, 2012 (gmt 0)

Hi g1smd,
Do you think this cron can be processed by a linux Op SYs?:
30 18 * * * php /path/to/php/file ? I know that if I replace php with rm for instance that's sure it'll be executed because rm is a linux command. What about this php?

Besides, on a linux system I have a file called crontab in /etc/crontab
Is it here where i have to write the previous command?

Thank you for your fast reply.


 12:07 pm on Aug 8, 2012 (gmt 0)

This is basic unix user level ...

Adding a line in the appropriate user's crontab is all what you need to do.

I'd replace "php" with the full path to the php command (type "which php" as command to find it) - assuming you have the command line php installed and in your PATH.


 2:53 pm on Aug 8, 2012 (gmt 0)

Hi swa66,
which php yields /usr/bin/php.
Thus, my php cron file begins so:

Notice the php script tag <?php at line 1 is omitted. Is this an error?
Besides, while creating the cron tab, the procedure i followed was this:
I logged in root, then typed 'crontab -e' where i typed the following as the crontab:
30 15 * * * /usr/bin/php /path/to/php/file > /tmp/export-to-oracle.log to be executed at the given time. I also did 'chmod' +x /path/to/php/file so that the file can be executed by the system.

Also notice that the ending php tag '?>' is omitted. So basically my whole php file looks like a text file.

To see whether the cron job was executed at all I open /var/log/cron and indeed i found something like:
Aug 8 16:42:01 barasukana-lp CROND[16214]: (root) CMD (/usr/bin/php /var/www/html/rcms/includes/exportMysqlToOracle.php > /tmp/export-to-oracle.log)
which show that the script was executed really. But, but, ...
when I open /tmp/export-to-oracle.log I found that the whole php batch file was fully written there.

In fact the php cron was to select data from mysql and just send the result in another oracle table. When I did 'SELECT count(*) FROM ora_table' i keep getting same count result over and over again. But running that same php file at command line sends the data to the appointed oracle table.

please help. If someone has come accross this, i'd appreciate if s/he show me the tutorial s/he used to get around this problem


 3:43 pm on Aug 8, 2012 (gmt 0)

If you're running multiple php scripts, make a file e.g. runphp.sh with the code

php /pathToScript/script.php
php /pathToScript/another-script.php

...then go to crontab & create cron job:

30 18 * * * sh /path/to/runphp.sh

If just one script, you can run it directly as such:

30 18 * * * php /path/to/script.php


 5:27 pm on Aug 8, 2012 (gmt 0)

your php file needs to start with and end with the <?php and ?> tags, otherwise it'll just echo it's input (just as is done on the web.

make sure that typing
php /path/file.php
on the command line works.


 12:35 pm on Aug 9, 2012 (gmt 0)

Hi harbs,
I created runphp.sh at /root/runphp.sh
I added php /path/file.php in it
I chmod +x /root/runphp.sh
In the cron, i typed:
30 18 * * * php /path/file.php
Still not executed
However, at command line the php script does what it is supposed to do.
Where am I wrong?


 1:04 pm on Aug 9, 2012 (gmt 0)

Since you created created runphp.sh at /root/runphp.sh, then in the cron, you should type:

30 18 * * * sh /root/runphp.sh


 5:00 pm on Aug 9, 2012 (gmt 0)

Hi harbs,
I did the command exactly as you suggest hereabove and still not working. Where do i read the cron execution error/log file should the script not perform well like in this case? Also, if i specify the minutes less than 10 in the minute part of the cron and write 01 instead of 1 does the zero at the left of 1 matter for script processing?


 5:24 pm on Aug 9, 2012 (gmt 0)

You may have to export your environment.

on linux terminal type "env"

get the PATH and insert into top of the script calling php:

export PATH=/whatever/your/path/is
php -q /whatever/path/to/php/script


 5:52 pm on Aug 9, 2012 (gmt 0)

The OP's path to php was /usr/bin/php, should not be needed to export anything.

The output of the command in cron is sent to the user who's crontab it is -> check if your local mail works and read it ...

What I've dealt with as a sysadmin years ago was a youngster reading the man pages and getting confused.
I'm wondering if that's not the case here.

What are you doing to add a line to cron ?
crontab -e ?

How do you check what's in your crontab ?
crontab -l ?
[let's hope you know how to use vi]

In the contab you add a line like
30 18 * * * /usr/bin/php /root/command.php

There is no need to add #!/bin/sh or anything like that. (luckily it's a comment in crontabs so you're safe)

The php command as said before needs to start with <?php and end with ?> and should work if invoked from the command line.

If you have trouble getting to know if it runs:
0 * * * * echo "works"
should send you an email once an hour, on the hour. It's sent locally to your account name - so unless you have it configured to send it elsewhere it'll arrive locally.

On thing: I don't use Linux, so its interface to set what cron does might be a bit off from the bsd unix way of doing things.


 2:53 pm on Aug 10, 2012 (gmt 0)

Dear swa66,
1) To add a line to a cron i do: crontab -e
2) To see what's in my crontab i do: crontab -l
3) Result of running crontab -l
on my machine is: 02 19 * * * sh /root/runphp.sh. Minutes and hour is changed frequently for testing purposes
4) Content of /root/runphp.sh using vi/vim:
/usr/bin/php /var/www/html/rcms/includes/exportMysqlToOracle.php

5) File exportMysqlToOracle.php start by <?php and end with ?>
6) permission on /root/runphp.sh as shown by ls -ltr /root/runphp.sh is: -rwxr-xr-x 1 root root 75 Aug 10 16:45 /root/runphp.sh

I don't see what i've missed for this not to work. Please help


 3:04 pm on Aug 10, 2012 (gmt 0)

"The OP's path to php was /usr/bin/php, should not be needed to export anything."

I recently encountered the same issue and found that cron passes a minimal or even a wrong set of environment variables to jobs.

If you can run the sript from a command line but it fails to run with cron, you have to set environment.


export PATH=/whatever/your/path/is ("env" PATH output)
/usr/bin/php /var/www/html/rcms/includes/exportMysqlToOracle.php

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved