hi guys, im trying to get this cron job to run every hour on the hour, however it is proving difficult for me, as ive never used them before.
in a short way of saying it, the script is ment to be triggered every hour on the hour, however i think ive gone wrong somewhere, the file is placed above the webroot, just like my host told me to.
below is a picture of my control panel and the 2 current cron jobs ive tried, so if i have done it wrong, can you please advise me.
also here is my script, so if anything is wrong, which i think some of the includes are, except for the connect.php one as thats in the same folder (above web root) however the functions one is in the includes folder which is in web root.
PHP Code:
<?php
include('connect.php');
include_once('includes/inc-functions.php');
$bulletquerys = mysql_query("SELECT * FROM bf WHERE producing !='No'");
// Loop through all rows in returned by the query
// This will iterate for every row returned by your query.
while($bulletquery = mysql_fetch_array($bulletquerys)){
$utime=$bulletquery['nextdue'];
$now=time();
$newbullets= $bulletquery['stock'] + $bulletquery['add'];
$nextdue= $now+(60*60);
mysql_query("UPDATE bf SET nextdue='".dbSafe($nextdue)."', stock='".dbSafe($newbullets)."', price='".dbSafe($newprice)."' WHERE id='".dbSafe($bulletquery['id'])."'");
To set the cronjob to be executed every hour you really need to state it as
0 * * * * php dosomejob.php
what You might add is to state default user who is going to run this job and add quotes to path (just to be sure and it looks nice ). Something like 0 * * * * cronuser php "dosomejob.php"
One thing that comes to my mind is if cronuser that is currently running Your tasks has right to run them. Mostly the one in your home directory.
Oooh and if you have access to logs you might find some useful info there!
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.
Dan, I think you need the path to php in the top of the script.
When you call a php script via the browser, the webserver is configured to pass it to php which does its thing and passes the generated content back to the webserver which then sends it to the users browser.
As a cron job there is no web server involved. That means that the operating system needs to know what program to open the file with. Unlike windows, linux systems don't always go by file extensions and often you'll need to put a path to the program on the top line - just like with perl scripts.
I may be wrong - some servers don't need it however I've had to do this on a couple of scripts running under cron.
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.
Yes only I can't remember if you might need the hash # in front. Also obviously you'd need to check the path to php on your server.
A google of cpanel php cron might answer the exact details re the path and hash but I think that might be the issue (I could be wrong though so no promises - it's been a while since I've used cron!).
Edit:
I looked at an email pipe script I used to use (no not a very reliable one before you ask hence I abandoned it) and at the top that has this:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.
In some languages, yes the # preceeds a comment however the operating system itself isn't a language - it's a system.
The line goes on the first line. It's not part of the php language it's just telling the system where the interpreter can be found so it does not go inside the <?php tags - only php code goes inside there.
As an example:
#!/usr/bin/php -q
<?php
// read from stdin
$fd = fopen("php://stdin", "r");
Again, I can't guarantee this will solve your problem but I think its a step in the right direction.
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.
ok so ive updated my coding and there is a picture below of what i have tried on cron.
Edit:
btw ive changed the time from 0 to another number inorder to test instead of waiting for the hour.
PHP Code:
#!/usr/bin/php -q <?php include('/home/crimewav/connect.php'); include_once('/home/crimewav/public_html/includes/inc-functions.php'); $bulletquerys = mysql_query("SELECT * FROM bf WHERE producing !='No'");
// Loop through all rows in returned by the query // This will iterate for every row returned by your query. while($bulletquery = mysql_fetch_array($bulletquerys)){ $utime=$bulletquery['nextdue']; $now=time(); $newbullets= $bulletquery['stock'] + $bulletquery['add']; $nextdue= $now+(60*60);
mysql_query("UPDATE bf SET nextdue='".dbSafe($nextdue)."', stock='".dbSafe($newbullets)."', price='".dbSafe($newprice)."' WHERE id='".dbSafe($bulletquery['id'])."'");
If you have the path to php in the file I think you can remove the /bin/php from the actual cron command.
If that makes no difference, then undo my recommendations as they've clearly not worked.
Edit:
Note that cron jobs in php are not easy to debug. If your require_once lines cause an error the script will probably abort and you'll see no errors.
To test that A script is executed successfully, run a simple script such as a one line call to mail() that will email you a confirmation that it ran.
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.
Your server probably restricts what can run above public_html. That said it may also restrict PHPs access outside of the public_html too which would explain your inclusion problems.
Not all servers allow you to put files outside of public_html / www so if you can't then you probably want to hide your connect.php somewhere in some random folders OR encode it and decode it with a hardcoded password in your script. Food for thought.
Anyway, good to hear you got the cron working - did you need the first line at the top of the file in the end?
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.
my host actually told me to put it above webroot :S lol connect.php works fine there, i figured out my problem from the last post by spelling something wrong, im still having issues with the cron job but i have emailed the host to see what they had to say about it.