11 Replies - 2374 Views - Last Post: 28 May 2014 - 11:08 AM

#1 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 897
  • Joined: 06-July 13

Cron Jobs & PHP

Posted 24 May 2014 - 08:45 AM

I know that this probably isn't the correct place for cron jobs, I apologise for that. But the only reason that I posted it here is because I call a PHP file with the cron job. I'm very new to them and this is the first time I've tried something like this hence why it probably went wrong. I also set this up with cPanel pro 11.42.1.

Here is the code I have at the moment, I'm trying to not have to link this PHP file into all files in order for it to change because then it will slow my scripts down, and it isn't needed because it's only done once every hour.

minute, hour, day, month, weekday, command

0 * * * * /usr/bin/php -q /home/****DIRECTORIES****.php 



I know the path is correct because I double checked it, but I'm not quite sure what is wrong and why it's not triggering - I didn't get any errors when putting it in, it just said it was successful.

Is This A Good Question/Topic? 0
  • +

Replies To: Cron Jobs & PHP

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3099
  • View blog
  • Posts: 10,887
  • Joined: 08-August 08

Re: Cron Jobs & PHP

Posted 24 May 2014 - 08:55 AM

It's been a while since I've dealt with cron jobs, so I'll ask the obvious question: are you sure you need one? Let's say your site awards points on an hourly basis. You could do that with a cron job but you could more easily check the number of hours since the last activity for a user and calculate the points to add for each user, only when they do something instead of all users every hour.
Was This Post Helpful? 0
  • +
  • -

#3 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 897
  • Joined: 06-July 13

Re: Cron Jobs & PHP

Posted 24 May 2014 - 09:59 AM

It's not a per user thing, it's a part of the site's structure that changes every hour. Maybe I didn't explain it well enough above, looking back it seems pretty incomprehensible to myself.

Instead of this:

Quote

Here is the code I have at the moment, I'm trying to not have to link this PHP file into all files in order for it to change because then it will slow my scripts down, and it isn't needed because it's only done once every hour.


I meant this:

I have a file in a directory on the server which contains loads of include and require_once links. These are links to included files in every page of my site, such as database connections and other stuff. This is included but it's only triggered once every hour and it seems to me that it's just slowing down the rest of the code unnecessarily and adding things that don't really need to be there. And without a cron job, I can't really think of another way without manually having to visit the file.
Was This Post Helpful? 0
  • +
  • -

#4 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 897
  • Joined: 06-July 13

Re: Cron Jobs & PHP

Posted 25 May 2014 - 12:57 AM

Ok it was set to send an email every time it ran to my site's main email. This is the error I got in the email:

Quote

<br />
<b>Warning</b>: gethostbyaddr() [<a href='function.gethostbyaddr'>function.gethostbyaddr</a>]: Address is not a valid IPv4 or IPv6 address in <b>/home/*******counter.php</b> on line <b>68</b><br />
<br />
<b>Fatal error</b>: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'ip' cannot be null' in /home/*********/counter.php:135
Stack trace:
#0 /home/**********counter.php(135): PDOStatement-&gt;execute(Array)
#1 /home/*********.php(10): require_once('/home/*******/...')
#2 /home/************FILE SET TO CRON.php(2): require_once('/home/*****/...')
#3 {main}
thrown in <b>/home/**********counter.php</b> on line <b>135</b><br />

Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3099
  • View blog
  • Posts: 10,887
  • Joined: 08-August 08

Re: Cron Jobs & PHP

Posted 25 May 2014 - 08:52 AM

So cron is working since your script is getting run. Now you need to debug your script.
Was This Post Helpful? 0
  • +
  • -

#6 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 897
  • Joined: 06-July 13

Re: Cron Jobs & PHP

Posted 26 May 2014 - 02:04 AM

I know exactly what the error was: in the file cron was running, I included all the included files on my server, and of course all it needed was the database connection - so it was trying to log cron as a visitor, and of course it didn't have a valid IP range so the error got thrown. Because the IP was null, when it was trying to log the visitor information, it couldn't because I set the SQL to not null for the IP column - simple!

All I did was change the included file to JUST the database connection and now it working fine. :bananaman:
Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 1,816
  • Joined: 15-January 14

Re: Cron Jobs & PHP

Posted 27 May 2014 - 09:20 AM

You can determine at runtime whether or not PHP is running from a command line or from the server. The php_sapi_name function will return the interface that PHP is using:

http://www.php.net/m...p-sapi-name.php

The value that it returns for web server versus command line will differ based on server configuration, so you will need to write a page that shows that value and run it through the server and command line to see the difference. I have a line like this in one of my config files:

$php_command_line = strpos(php_sapi_name(), 'apache') === false && (strpos(php_sapi_name(), 'cgi') !== false || strpos(php_sapi_name(), 'cli') !== false);


After that, if $php_command_line is true, then I skip certain things (like creating a session for the user). There's a list of differences between the command line and other APIs here:

http://www.php.net/m...differences.php

Take note that things like the working directory will be different, so if you are including files in a command line environment then you should not use relative paths, use absolute paths (using __FILE__, __DIR__, etc). Command line scripts also have no execution time limit.
Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5388
  • View blog
  • Posts: 27,384
  • Joined: 10-May 07

Re: Cron Jobs & PHP

Posted 27 May 2014 - 09:23 AM

View Postchris98, on 24 May 2014 - 11:45 AM, said:

I know that this probably isn't the correct place for cron jobs

Correct! Cron jobs are not php. In fact, they are lauched by the ...

** moved to Web Servers & Hosting **
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: Cron Jobs & PHP

Posted 27 May 2014 - 09:59 AM

...crond process (daemon)
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9572
  • View blog
  • Posts: 36,243
  • Joined: 12-June 08

Re: Cron Jobs & PHP

Posted 27 May 2014 - 11:10 AM

.... crom process (lord of the mound).

Spoiler

Was This Post Helpful? 0
  • +
  • -

#11 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 1,816
  • Joined: 15-January 14

Re: Cron Jobs & PHP

Posted 28 May 2014 - 11:06 AM

... cram process (you are no longer hungry)
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9572
  • View blog
  • Posts: 36,243
  • Joined: 12-June 08

Re: Cron Jobs & PHP

Posted 28 May 2014 - 11:08 AM

Well played.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1