Hello guys, I don't know whats wrong I followed every tutorial for adding editing cron jobs
here is what I did.
using putty

sudo crontab -e
1 * * * * /usr/bin/php /var/www/cronjob.php

then save file,
my cronjob.php for testing perposes

$DB_HOST="localhost";
$DB_USER="root";
$DB_PASS="123456";
$DB_NAME="cron";
$connect=mysql_connect("$DB_HOST","$DB_USER","$DB_PASS") or die ("error");
mysql_select_db("$DB_NAME",$connect) or die ("error");
$sql="INSERT INTO `jobs` (
`id` ,
`job`
)
VALUES (
NULL , '3333'
)";
mysql_query($sql);

its supposed to enter "333" value in database every 1 minutes but nothing was entered
is there somthing wrong with my command ?

Recommended Answers

All 11 Replies

Change both or die("error") with:

or die(error_log(mysql_error(),0));

Add one also to the query, if the id is something like int(9) not null auto_increment primary key then you don't need to declare with NULL value, otherwise it will return a boolean FALSE. After these changes check /var/log/php_errors.log or the specified file inside /etc/php5/cli/php.ini. Bye!

I've tried to test the php file alone and its working perfectly
I read about this and found if I want to run job every 5 minutes the command must be

*/5 * * * * /usr/bin/php /var/www/cronjob.php

but again the job not working, is the any method to test if my cron job is acually running ?

*/5 * * * * date >> $HOME/test

and what is the output for this ?

*/5 * * * * date >> $HOME/test

It will append the current date and time to the file $HOME/test every 5 minutes. ($HOME being your home directory.)

the code above works prefectly by writing date on file
I Need to excute php script every 5 minutes, is that possible ?

Yes. Make sure your script is executable.

commented: that was the problem +2

Do this.

At the top of your php script, add the line
"#!/usr/bin/php"

That will set the environment up so the script 'knows' what it is.

Then in cron, just issue the script.
1 * * * * /var/www/cronjob.php

Make sure the .php is set with execute permissions (as DoRight said above):
chmod +x /var/www/cronjob.php

and you should be good.

since he used /usr/bin/php to run cronjob.php, there is no need to put #!/usr/bin/php inside that .php script or even chmod +x it (cmiiw) - just make sure it is readable. I think cereal's idea is worth to try.

Good point, Marlo. Are the backticks correct in the SQL statement? I would think you would use ' not ` ?

thanks DoRight the error was in permission for file :d
I've changed it to excutible and its work fine, problem solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.