Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-27-2012, 07:05 PM   PM User | #1
Dan13071992
Regular Coder

 
Join Date: Dec 2010
Location: Kent, UK
Posts: 573
Thanks: 23
Thanked 10 Times in 10 Posts
Dan13071992 is an unknown quantity at this point
Cron Job Problem

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


    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>");
  }

   
    
}

?>



Cheers in advance

Dan
__________________
http://360-tactics.co.uk/forum/index.php

Crime-Wave

please post your code wrapped in tags
please post your PHP wrapped in tags

Last edited by Dan13071992; 02-29-2012 at 06:33 PM..
Dan13071992 is offline   Reply With Quote
Old 02-27-2012, 07:37 PM   PM User | #2
TheCracker
New Coder

 
Join Date: Sep 2010
Posts: 65
Thanks: 4
Thanked 0 Times in 0 Posts
TheCracker is an unknown quantity at this point
try
Code:
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 Code:
<?php
$fp 
fopen('test.txt''w');
fwrite($fp'its working.....');
fclose($fp);
?>
and usually the address is /home/admin/..... if you are using vps

Last edited by TheCracker; 02-27-2012 at 07:42 PM..
TheCracker is offline   Reply With Quote
Old 02-27-2012, 07:41 PM   PM User | #3
abadonn
New to the CF scene

 
Join Date: Feb 2012
Location: Newbury Uk
Posts: 7
Thanks: 0
Thanked 2 Times in 2 Posts
abadonn is an unknown quantity at this point
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!
abadonn is offline   Reply With Quote
Old 02-27-2012, 07:43 PM   PM User | #4
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,507
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
I think you might need the path to PHP at the top of the file.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//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. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 02-27-2012, 08:20 PM   PM User | #5
Dan13071992
Regular Coder

 
Join Date: Dec 2010
Location: Kent, UK
Posts: 573
Thanks: 23
Thanked 10 Times in 10 Posts
Dan13071992 is an unknown quantity at this point
i changed the includes to this:

PHP Code:
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
__________________
http://360-tactics.co.uk/forum/index.php

Crime-Wave

please post your code wrapped in tags
please post your PHP wrapped in tags
Dan13071992 is offline   Reply With Quote
Old 02-27-2012, 08:52 PM   PM User | #6
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,507
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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 wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//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. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 02-27-2012, 08:55 PM   PM User | #7
Dan13071992
Regular Coder

 
Join Date: Dec 2010
Location: Kent, UK
Posts: 573
Thanks: 23
Thanked 10 Times in 10 Posts
Dan13071992 is an unknown quantity at this point
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]
__________________
http://360-tactics.co.uk/forum/index.php

Crime-Wave

please post your code wrapped in tags
please post your PHP wrapped in tags
Dan13071992 is offline   Reply With Quote
Old 02-27-2012, 09:01 PM   PM User | #8
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,507
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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:

#!/usr/bin/php -q
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//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. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 02-27-2012, 09:02 PM   PM User | #9
Dan13071992
Regular Coder

 
Join Date: Dec 2010
Location: Kent, UK
Posts: 573
Thanks: 23
Thanked 10 Times in 10 Posts
Dan13071992 is an unknown quantity at this point
doesnt the # comment it out?

also, would it go inside or outside of hte php tags "<?php" ?
__________________
http://360-tactics.co.uk/forum/index.php

Crime-Wave

please post your code wrapped in tags
please post your PHP wrapped in tags
Dan13071992 is offline   Reply With Quote
Old 02-27-2012, 09:16 PM   PM User | #10
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,507
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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 wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//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. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 02-27-2012, 09:22 PM   PM User | #11
Dan13071992
Regular Coder

 
Join Date: Dec 2010
Location: Kent, UK
Posts: 573
Thanks: 23
Thanked 10 Times in 10 Posts
Dan13071992 is an unknown quantity at this point
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);


    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?
__________________
http://360-tactics.co.uk/forum/index.php

Crime-Wave

please post your code wrapped in tags
please post your PHP wrapped in tags
Dan13071992 is offline   Reply With Quote
Old 02-27-2012, 09:29 PM   PM User | #12
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,507
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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 wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//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. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.

Last edited by tangoforce; 02-27-2012 at 09:32 PM..
tangoforce is offline   Reply With Quote
Old 02-27-2012, 09:42 PM   PM User | #13
Dan13071992
Regular Coder

 
Join Date: Dec 2010
Location: Kent, UK
Posts: 573
Thanks: 23
Thanked 10 Times in 10 Posts
Dan13071992 is an unknown quantity at this point
done that, got the email

PHP Code:
42    *    *    *    *    /bin/php /home/crimewav/public_html/email.php 
that was the cron settings

Edit:

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?

__________________
http://360-tactics.co.uk/forum/index.php

Crime-Wave

please post your code wrapped in tags
please post your PHP wrapped in tags

Last edited by Dan13071992; 02-27-2012 at 09:46 PM..
Dan13071992 is offline   Reply With Quote
Old 02-27-2012, 11:48 PM   PM User | #14
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,507
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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 wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//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. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 02-27-2012, 11:50 PM   PM User | #15
Dan13071992
Regular Coder

 
Join Date: Dec 2010
Location: Kent, UK
Posts: 573
Thanks: 23
Thanked 10 Times in 10 Posts
Dan13071992 is an unknown quantity at this point
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.
__________________
http://360-tactics.co.uk/forum/index.php

Crime-Wave

please post your code wrapped in tags
please post your PHP wrapped in tags
Dan13071992 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:09 PM.


Advertisement
Log in to turn off these ads.