...

View Full Version : Resolved Cron Job Problem



Dan13071992
02-27-2012, 08:05 PM
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
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);


if ($bulletquery['newprice'] != $bulletquery['price']) {
$newprice = $bulletquery['newprice'];
}
elseif ($bulletquery['newprice'] == $bulletquery['price']) {
$newprice = $bulletquery['price'];
}

mysql_query("UPDATE bf SET nextdue='".dbSafe($nextdue)."', stock='".dbSafe($newbullets)."', price='".dbSafe($newprice)."' WHERE id='".dbSafe($bulletquery['id'])."'");

$to = "dos1392@hotmail.co.uk";
$subject = "Cron Job";
$body = "it worked";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}



}

?>


http://crime-wave.co.uk/cron.png


Cheers in advance :)

Dan

TheCracker
02-27-2012, 08:37 PM
try

crontab -l\
to list all your cron jobs.

however your cronjobs looks good for me, you can try simple php code to test if its working or not


<?php
$fp = fopen('test.txt', 'w');
fwrite($fp, 'its working.....');
fclose($fp);
?>

and usually the address is /home/admin/..... if you are using vps

abadonn
02-27-2012, 08:41 PM
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!

tangoforce
02-27-2012, 08:43 PM
I think you might need the path to PHP at the top of the file.

Dan13071992
02-27-2012, 09:20 PM
i changed the includes to this:



include('/home/crimewav/connect.php');
include_once('/home/crimewav/public_html/includes/inc-functions.php');


however it is still not working, also im on shared hosting, not a VPS

tangoforce
02-27-2012, 09:52 PM
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.

Dan13071992
02-27-2012, 09:55 PM
so if i was to set the file type at the top of the page, would i do it like so:

[code]
/bin/php
[/php]

tangoforce
02-27-2012, 10:01 PM
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!).


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:

#!/usr/bin/php -q

Dan13071992
02-27-2012, 10:02 PM
doesnt the # comment it out?

also, would it go inside or outside of hte php tags "<?php" ?

tangoforce
02-27-2012, 10:16 PM
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.

Dan13071992
02-27-2012, 10:22 PM
ok so ive updated my coding and there is a picture below of what i have tried on cron.

http://crime-wave.co.uk/cron2.png


btw ive changed the time from 0 to another number inorder to test instead of waiting for the hour.




#!/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);


if ($bulletquery['newprice'] != $bulletquery['price']) {
$newprice = $bulletquery['newprice'];
}
elseif ($bulletquery['newprice'] == $bulletquery['price']) {
$newprice = $bulletquery['price'];
}

mysql_query("UPDATE bf SET nextdue='".dbSafe($nextdue)."', stock='".dbSafe($newbullets)."', price='".dbSafe($newprice)."' WHERE id='".dbSafe($bulletquery['id'])."'");

$to = "dos1392@hotmail.co.uk";
$subject = "Cron Job";
$body = "it worked";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}



}

?>


however its still not working :s am i missing anything else?

tangoforce
02-27-2012, 10:29 PM
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.


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.

Dan13071992
02-27-2012, 10:42 PM
done that, got the email :)



42 * * * * /bin/php /home/crimewav/public_html/email.php


that was the cron settings



it seems that using:

/bin/php /home/crimewav/email.php

didnt work even when i put that same file above the web root. any ideas why?

tangoforce
02-28-2012, 12:48 AM
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 :thumbsup: - did you need the first line at the top of the file in the end?

Dan13071992
02-28-2012, 12:50 AM
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.

tangoforce
02-28-2012, 01:05 AM
Ok, keep us updated. I'm off to bed now so will check tomoz..

Dan13071992
02-28-2012, 03:27 PM
hi guys, ive got a weird problem now, my below script isnt working even if i run the cron job from in the web root, below is the script:



<?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);


if ($bulletquery['newprice'] != $bulletquery['price']) {
$newprice = $bulletquery['newprice'];
}
elseif ($bulletquery['newprice'] == $bulletquery['price']) {
$newprice = $bulletquery['price'];
}

mysql_query("UPDATE bf SET nextdue='".dbSafe($nextdue)."', stock='".dbSafe($newbullets)."', price='".dbSafe($newprice)."' WHERE id='".dbSafe($bulletquery['id'])."'");

$to = "dos1392@hotmail.co.uk";
$subject = "Cron Job";
$body = "it worked";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}



}

?>


however if i run email.php which is this:



<?php

$to = "dos1392@hotmail.co.uk";
$subject = "Cron Job";
$body = "it worked";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}

?>


it runs completely fine :s

ive tried changing the include files on the above page to different paths to see if that was the problem, however it wasnt, so im really confused as to what is wrong now, also in addition to this if i run the email.php script from in the web root using a cron job, it runs fine, but if its above the web root it doesnt, that script at the top of this post works fine if i just got to it manually, but cron jobs dont seem to be working with it :s

any ideas would be great.

Thanks.

Dan


P.S.

below is a picture of my above web root, now i have a folder called private_html. nothing is in it, i was just wondering if i put anything in it, can anyone veiw it? or just the server as i wouldnt have a clue how to do it in a browser.

http://crime-wave.co.uk/folder.png

Dan13071992
02-28-2012, 04:49 PM
ok i think i figured it out, instead of including both of the files, i actually wrote the files into the cronjob.php file itself, so theres no includes, it worked first time when i have it in web root, now to try it above root i will report back.




having the file above web root and trying to trigger it through cron isnt working, theres no way to password protect the file itself so that the server is hte only person(i know its the wrong termanology) to be able to trigger it off, is there?

tangoforce
02-28-2012, 05:03 PM
theres no way to password protect the file itself so that the server is hte only person(i know its the wrong termanology) to be able to trigger it off, is there?


Yes, there is.

Remember that variable in the server array called REMOTE_ADDR? - If its not set and/or empty then the script hasn't been called by http ;)

Looking at the server array can also determine if there was a connection on port 80 :thumbsup:

I think you're probably familiar with if/else and die/exit :D

Dan13071992
02-28-2012, 05:06 PM
Yes, there is.

Remember that variable in the server array called REMOTE_ADDR? - If its not set and/or empty then the script hasn't been called by http ;)

Looking at the server array can also determine if there was a connection on port 80 :thumbsup:

I think you're probably familiar with if/else and die/exit :D

yes im familiar with if/else and die/exit commands, however not with the server commands such as REMOTE_ADDR, apart from calling for their ip address, i havent a clue.

tangoforce
02-28-2012, 05:13 PM
$_SERVER['REMOTE_ADDR'] will contain an IP address of the http connection. If its not set (use isset() to check) and is empty (use empty() to check) then there was no connection via http - that would mean it could only be cron triggering it.

Same goes for the port number too which can be found (or not) at $_SERVER['SERVER_PORT'].

Also the $_GET array should be empty / not set.

IF all those conditions are met then its cron triggering the script.

Dan13071992
02-28-2012, 08:30 PM
so could i do it like so:




if (isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR'])) {
exit;
}else {

//code here

}

tangoforce
02-28-2012, 09:34 PM
Yes, that should do it.

Dan13071992
02-29-2012, 12:06 AM
would it go above or below my connect to database part, as there is a session start in there, this is what i have as the very first set of lines:



if (isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR'])) {
exit();
}else {


session_start();
$mysql_server = "localhost";

//ect ect



is that the right way round?

tangoforce
02-29-2012, 11:27 AM
There is no 'right' way of doing it - just whatever works really :thumbsup:

As for the session_start(), its easier to tell people to put it at the top of the script but it doesn't actually have to go there. It just has to be placed somewhere before any output causes the headers to be sent - or with output buffering anywhere else. Many folks don't understand all that so it's easier for us just to say to put it at the top.

Dan13071992
02-29-2012, 07:32 PM
it worked the way i done it :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum