Forums / General Questions / Any good advice on adding PHP Cron Jobs?

Any good advice on adding PHP Cron Jobs?

Views: 2,146

Results 1 to 20 of 20
1 Dec 2015, 8:01 AM
#1
spacemonkey avatar

spacemonkey

Zen Follower

Join Date:
Nov 2013
Location:
Dorset, UK
Posts:
124
Plugin Contributions:
0

Any good advice on adding PHP Cron Jobs?

Hi All.

I have been trying to add a cron job for the past couple of days to update one of my banners. As the tittle says I am trying to use PHP to deploy. Simply swapping the img file with cron won't do for this as it causes issues with temporary files etc.

I have separated the connection and the job as I always have. My process is as follows:

db-connect.php

 <?php
 // set db access info as constants
 define ('DB_USER','####');
 define ('DB_PASSWORD','####');
 define ('DB_HOST','localhost');
 define ('DB_NAME','####');
 // make connection and select db
 $dbc = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );
 mysql_select_db(DB_NAME) OR die ('Could not select the db: ' . mysql_error() );
 ?>
 
 

cron_job.php

<?php
 require_once('/home/####/public_html/cron/db-connect.php'); // connect to db
 $query = "REPLACE INTO `####`.`zen_banners` (
`banners_id` ,
`banners_title` ,
`banners_url` ,
`banners_image` ,
`banners_group` ,
`banners_html_text` ,
`expires_impressions` ,
`expires_date` ,
`date_scheduled` ,
`date_added` ,
`date_status_change` ,
`status` ,
`banners_open_new_windows` ,
`banners_on_ssl` ,
`banners_sort_order` 
)
VALUES (
 '15', 'Welcome', '', '', 'homepageslide', '<img src="images/banners/open_soon.png" alt="Open Daily 17:00hrs to 23:00hrs" class="banner-img"><div class="banner" /><p>Open at 17:00hrs<span></p></div>', NULL , NULL , NULL , '0001-01-01 00:00:00', '0001-01-01 16:45:00', '1', '1', '1', '0');
 ");
$result = @mysql_query($query); // run the query
 ?>

The SQL query works when run via php MyAdmin.

I am using the following line as my cron command line:

/usr/bin/php /home/####/public_html/cron/cron_job.php >/home/####/public_html/cron/new.txt 

I just need to update the banner image a couple of time during our day (open and closing times for our store). No matter what I try I can't get this to function.

Can anyone see the mistake? Any general advice?

My PHP is v5.4. Let me know if you need to know anything else. Any help gratefully received.

Thanks all

1 Dec 2015, 8:19 AM
#2
spacemonkey avatar

spacemonkey

Zen Follower

Join Date:
Nov 2013
Location:
Dorset, UK
Posts:
124
Plugin Contributions:
0

Re: Any good advice on adding PHP Cron Jobs?

p.s. My output file (new.txt) is blank and I can't find any logs for the cron job (I have sent a support request to find this).

I have spent hours picking at what was meant to be a quick job. :frusty:

Any help would be greatly received. :smile:

1 Dec 2015, 9:12 AM
#3
lruskauff avatar

lruskauff

Totally Zenned

Join Date:
Sep 2008
Location:
WA
Posts:
559
Plugin Contributions:
0

Re: Any good advice on adding PHP Cron Jobs?

Just a thought but did u check the file permission on new.txt?

1 Dec 2015, 12:54 PM
#4
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Any good advice on adding PHP Cron Jobs?

Php 5.4, probably need to use mysqli_ related functions if even going to directly access the database.

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

1 Dec 2015, 4:37 PM
#5
spacemonkey avatar

spacemonkey

Zen Follower

Join Date:
Nov 2013
Location:
Dorset, UK
Posts:
124
Plugin Contributions:
0

Re: Any good advice on adding PHP Cron Jobs?

mc12345678:

Php 5.4, probably need to use mysqli_ related functions if even going to directly access the database.

Ahhh, good point. I will update the connection and try again.

Thanks :hug:

1 Dec 2015, 4:41 PM
#6
spacemonkey avatar

spacemonkey

Zen Follower

Join Date:
Nov 2013
Location:
Dorset, UK
Posts:
124
Plugin Contributions:
0

Re: Any good advice on adding PHP Cron Jobs?

lruskauff:

Just a thought but did u check the file permission on new.txt?

Yeah, not a permission issue on occasion I have had the following message in the .txt

X-Powered-By: PHP/5.4.43
Content-type: text/html

I think mc12345678 might be on the right path. I will update later tonight once I update to mysqli.

Thanks

1 Dec 2015, 7:57 PM
#7
patclay avatar

patclay

New Zenner

Join Date:
Dec 2008
Location:
New Zealand
Posts:
88
Plugin Contributions:
1

Re: Any good advice on adding PHP Cron Jobs?

Remember that the cron job might not be run by the same user as the command line, making the permissions issues a little more murky.

Also, just to be clear, if you enter the command

/usr/bin/php /home/####/public_html/cron/cron_job.php >/home/####/public_html/cron/new.txt

on the command line (do you have access), does it work?

If it doesn't you might have the wrong path/command for php (I think they can vary)

If you want to give up and try a different tack you could use curl & a web page - cron calls curl & the web page which runs the query.

1 Dec 2015, 8:25 PM
#8
spacemonkey avatar

spacemonkey

Zen Follower

Join Date:
Nov 2013
Location:
Dorset, UK
Posts:
124
Plugin Contributions:
0

Re: Any good advice on adding PHP Cron Jobs?

PatClay:

Remember that the cron job might not be run by the same user as the command line, making the permissions issues a little more murky.

Also, just to be clear, if you enter the command

/usr/bin/php /home/####/public_html/cron/cron_job.php >/home/####/public_html/cron/new.txt

> on the command line (do you have access), does it work?
> 
> If it doesn't you might have the wrong path/command for php (I think they can vary)

Hi, 

I think I have the correct path to php, sometimes I get 

X-Powered-By: PHP/5.4.43
Content-type: text/html

On it's own I think this shows PHP has been started. Just nothing after it!! 

> **PatClay:**
>
> If you want to give up and try a different tack you could use curl & a web page - cron calls curl & the web page which runs the query.

It doesn't have to be done this way, just thought would have the quickest results!!! :no: Also my final version would not be stored in a public directory (just testing there). 

Question: Would the CURL approach work in home/####/TMP/ (ie not a public folder)? 

Regards
1 Dec 2015, 9:19 PM
#9
spacemonkey avatar

spacemonkey

Zen Follower

Join Date:
Nov 2013
Location:
Dorset, UK
Posts:
124
Plugin Contributions:
0

Re: Any good advice on adding PHP Cron Jobs?

I have confirmed the path to PHP is correct.

/usr/bin/php 

So that's ruled out. Something wrong with my PHP! I have updated to MySQLi as suggested by mc12345678

db-connect.php

 
 <?php
 $con = mysqli_connect("localhost","user","password","database");

// Check connection
 if (mysqli_connect_errno())
   {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }
 ?> 

Nothing in my txt to say could not connect, now it always says:

new.txt

X-Powered-By: PHP/5.4.43

Content-type: text/html

After some reading I have modified my SQL query as follows:

cron_job.php

<?php
 require_once('/home/####/public_html/cron/db-connect.php'); // connect to db
  
  $query = UPDATE `####`.`zen_banners` SET `banners_html_text` = '<img src="images/banners/opening_soon.png" alt="Open Daily 17:00hrs to 23:00hrs" class="banner-img"><div class="banner" /><p>Open daily 17:00 to 23:00hrs</p></div>' WHERE `zen_banners`.`banners_id` =15;
$result = @mysql_query($query); // run the query
 ?>

It's still won't run though. The only progress is my txt (output) now contains something every time.

Again when I test via PHP MyAdmin the SQL query works fine! Open to any suggestions. Can't hurt. :unsure:

Thanks

1 Dec 2015, 11:11 PM
#10
lruskauff avatar

lruskauff

Totally Zenned

Join Date:
Sep 2008
Location:
WA
Posts:
559
Plugin Contributions:
0

Re: Any good advice on adding PHP Cron Jobs?

SpaceMonkey:

Hi All.

I have been trying to add a cron job for the past couple of days to update one of my banners. As the tittle says I am trying to use PHP to deploy. Simply swapping the img file with cron won't do for this as it causes issues with temporary files etc.

I have separated the connection and the job as I always have. My process is as follows:

db-connect.php

<?php // set db access info as constants define ('DB_USER','####'); define ('DB_PASSWORD','####'); define ('DB_HOST','localhost'); define ('DB_NAME','####'); // make connection and select db $dbc = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() ); mysql_select_db(DB_NAME) OR die ('Could not select the db: ' . mysql_error() ); ?>
> 
> cron_job.php
> ```php
<?php
 require_once('/home/####/public_html/cron/db-connect.php'); // connect to db
 $query = "REPLACE INTO `####`.`zen_banners` (
`banners_id` ,
`banners_title` ,
`banners_url` ,
`banners_image` ,
`banners_group` ,
`banners_html_text` ,
`expires_impressions` ,
`expires_date` ,
`date_scheduled` ,
`date_added` ,
`date_status_change` ,
`status` ,
`banners_open_new_windows` ,
`banners_on_ssl` ,
`banners_sort_order` 
)
VALUES (
 '15', 'Welcome', '', '', 'homepageslide', '<img src="images/banners/open_soon.png" alt="Open Daily 17:00hrs to 23:00hrs" class="banner-img"><div class="banner" /><p>Open at 17:00hrs<span></p></div>', NULL , NULL , NULL , '0001-01-01 00:00:00', '0001-01-01 16:45:00', '1', '1', '1', '0');
 ");
$result = @mysql_query($query); // run the query
 ?>

The SQL query works when run via php MyAdmin.

I am using the following line as my cron command line:

/usr/bin/php /home/####/public_html/cron/cron_job.php >/home/####/public_html/cron/new.txt

> 
> I just need to update the banner image a couple of time during our day (open and closing times for our store). No matter what I try I can't get this to function. 
> 
> Can anyone see the mistake? Any general advice? 
> 
> My PHP is v5.4. Let me know if you need to know anything else. Any help gratefully received.
> 
> Thanks allthis is just 
a wild guess but i see 3 types of ''.  i see ' ', " ", and then a slanted version of a single txt.  Is that allowed?
1 Dec 2015, 11:21 PM
#11
lruskauff avatar

lruskauff

Totally Zenned

Join Date:
Sep 2008
Location:
WA
Posts:
559
Plugin Contributions:
0

Re: Any good advice on adding PHP Cron Jobs?

nevermind, just rread about them.

2 Dec 2015, 12:38 AM
#12
patclay avatar

patclay

New Zenner

Join Date:
Dec 2008
Location:
New Zealand
Posts:
88
Plugin Contributions:
1

Re: Any good advice on adding PHP Cron Jobs?

Question: Would the CURL approach work in home/####/TMP/ (ie not a public folder)?
Not sure, but my guess is no.

A couple of questions for troubleshooting:
if you enter

usr/bin/php /home/####/public_html/cron/cron_job.php >/home/####/public_html/cron/new.txt

on the command line, does it work? I know that you said that the path to php is correct, I am sure that there are other things that can cause problems
if you point your browser to https://www.YOURSITE.COM/cron/cron_job.php , does it work (it should)? This will (should) give you the benefit of logging errors to the apache error log.

My approach to troubleshooting here would be to litter the scripts with echo "location 1" so I can figure out where it dies.

2 Dec 2015, 12:40 AM
#13
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Any good advice on adding PHP Cron Jobs?

lruskauff:

this is just
a wild guess but i see 3 types of ''. i see ' ', " ", and then a slanted version of a single txt. Is that allowed?

lruskauff:

nevermind, just rread about them.

Too late, I already wrote the below so fgured worth sharing for others: :P

Multi "quotes" are acceptable provided they are 1) balanced, 2) escaped as necessary, and 3) used where appropriate.

The ` is/can be used around fields like it is, but also isn't generally necessary. ' used within a double quote (") represents text or a string within mysql. The double quote is/can be used to represent that everything within is text to php... Alternatively a single quote (') could have been used, but then all of the internal single quotes would have to be escaped (') to prevent them being treated as the end of the string to then be considered as concatenated (joined) with the content that followed and the content following being treated as php content...

Yes, the balance of the quotes was something I looked for first as without a php ide or context reviewer that is one of the first mistakes that happen, unfortunately... In this case none blatantly stood out, but that's not to say that a total balance was performed to ensure that there wasn't an odd "quote" out.

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

2 Dec 2015, 12:41 AM
#14
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,720
Plugin Contributions:
56

Re: Any good advice on adding PHP Cron Jobs?

include the application_top file

require('includes/application_top.php');

and then use Zen Cart functions to access the database.

$result = $db->Execute("some string");

This way you'll get regular error logs in logs/ if there's a problem.

That Software Guy. My Store: Zen Cart Support
Available for hire - See my ad in Services
Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
Do you benefit from Zen Cart? Then please support the project.

2 Dec 2015, 12:54 AM
#15
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Any good advice on adding PHP Cron Jobs?

SpaceMonkey:

I have confirmed the path to PHP is correct.

/usr/bin/php

> 
> So that's ruled out. Something wrong with my PHP! I have updated to MySQLi as suggested by mc12345678 
> 
> db-connect.php
> ```php
 
 <?php
 $con = mysqli_connect("localhost","user","password","database");

// Check connection
 if (mysqli_connect_errno())
   {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }
 ?> 

Nothing in my txt to say could not connect, now it always says:

new.txt

X-Powered-By: PHP/5.4.43

Content-type: text/html

> 
> After some reading I have modified my SQL query as follows: 
> 
> cron_job.php
> ```php
<?php
 require_once('/home/####/public_html/cron/db-connect.php'); // connect to db
  
  $query = UPDATE `####`.`zen_banners` SET `banners_html_text` = '<img src="images/banners/opening_soon.png" alt="Open Daily 17:00hrs to 23:00hrs" class="banner-img"><div class="banner" /><p>Open daily 17:00 to 23:00hrs</p></div>' WHERE `zen_banners`.`banners_id` =15;
$result = @mysql_query($query); // run the query
 ?>

It's still won't run though. The only progress is my txt (output) now contains something every time.

Again when I test via PHP MyAdmin the SQL query works fine! Open to any suggestions. Can't hurt. :unsure:

Thanks

Umm. Query is still using mysql_ commands not mysqli_

Haven't gone to pull the php manual, but I believe it also needs to have "credentials" as part of the query/almost any mysqli_ call/function. See http://php.net for further guidance.

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

2 Dec 2015, 12:57 AM
#16
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Any good advice on adding PHP Cron Jobs?

swguy:

include the application_top file

require('includes/application_top.php');

and then use Zen Cart functions to access the database.

$result = $db->Execute("some string");

This way you'll get regular error logs in logs/ if there's a problem.

I thought about this when the frst post was made, but then thought I remembered that there were "issues" with trying to use that in a file that was not index.php or wasn't in the directory "structure"... Could have been wrong, but thought that was an issue seen when someone else had tried to do something similar... Would certanly make things a lot easier and "user" friendly. :)

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

2 Dec 2015, 10:27 AM
#17
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,720
Plugin Contributions:
56

Re: Any good advice on adding PHP Cron Jobs?

I do this all the time. It works just fine.

That Software Guy. My Store: Zen Cart Support
Available for hire - See my ad in Services
Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
Do you benefit from Zen Cart? Then please support the project.

2 Dec 2015, 2:13 PM
#18
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,109
Plugin Contributions:
56

Re: Any good advice on adding PHP Cron Jobs?

swguy:

I do this all the time. It works just fine.
Likewise; just make sure that your script changes the current directory to the root directory for your store's files (since the cron-job's working-directory is most likely outside of your file-system):

if (isset ($_SERVER['SHELL'])) {
  chdir ('/var/www/html/mysite.com');  //-Use the same value here as is present in the DIR_FS_CATALOG in your /includes/configue.php file.
  
}
require ('includes/application_top.php');
2 Dec 2015, 3:27 PM
#19
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Any good advice on adding PHP Cron Jobs?

swguy:

I do this all the time. It works just fine.

Well between you and lat9 I been schooled. :) momentary failure in memory, sorry to you SpaceMonkey for not suggesting to use the built in capabilities of ZenCart to resolve your issue. As you either have now found out or will soon, your issue should be resolved and further you will have something that is portable to other versions of ZC, especially if you continue to use built in ZC operations.

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

3 Dec 2015, 11:30 PM
#20
spacemonkey avatar

spacemonkey

Zen Follower

Join Date:
Nov 2013
Location:
Dorset, UK
Posts:
124
Plugin Contributions:
0

Re: Any good advice on adding PHP Cron Jobs?

Thank you :yes:

Everyone you are stars!! Done!

mc12345678, SWGUY & lat9
You guys always have great input and I'm sure most users of the forum agree, always worthy. In this post and many others I read, your input is always constructive and knowledgeable.

Thanks everyone!