Solved

cron job says it ran, no results

Posted on 2016-08-25
25
93 Views
Last Modified: 2016-08-29
Linux server. I have tried everything to get a cron job to produce results but no go.  My web host says they can look in the cron log and see that it ran, no errors. I can run the exact same script via a browser and it works fine. It's just trying to run a php script. My web host says everything looks ok, crontab.txt is in order, must be the script. I do run crontab crontab.txt via SSH each time I alter the file. No results. But again, I can run it via browser no problem. What other variables could there be?
0
Comment
Question by:QMBB
25 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
User permissions is the first thing.  Running thru the web server, the path can simply be relative to the web root and it runs under the web server permissions.  Running from the command line or crontab, the path must be a complete operating system path to the PHP file.
1
 

Author Comment

by:QMBB
Comment Utility
It is, according to all my research of existing cron jobs that ARE working and according to the webhost. It's:

40 1 * * * /usr/local/bin/php /usr/www/users/myuser/myfolder/anotherfolder/script/SubmitFeed.php
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Try this one.  See if you get the email.
<?php // demo/cron_example_phpinfo.php
/**
 * This script can be run as a regular web page and as a cron job.
 * It sends an email -- insert your email address in the last line.
 * You can see the differences between the two php environments.
 */
error_reporting(E_ALL);


// START THE OUTPUT BUFFER TO CAPTURE THE STDOUT STRING
ob_start();

// PRINT THE PHP ENVIRONMENTAL DATA
phpinfo();

// COPY THE OUTPUT BUFFER
$info = ob_get_clean();

// EMAIL IT TO YOURSELF - SET YOUR OWN EMAIL ADDRESS HERE
mail('You@Your.org', 'CRON INFO', $info);

Open in new window

0
 

Author Comment

by:QMBB
Comment Utility
Worked in browser, testing via cron...
1
 
LVL 76

Expert Comment

by:arnold
Comment Utility
What is it supposed to do?



Make sure you use full paths to system calls within the php.

You have it setup to run at 1:40 am server time.

If there is any output from the script an email to the user under whose credentials the cron job will receive notification. Adding a .forward with your email address so any output from the cron will get to you......
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
This may be a red herring, but I'll mention it anyway.  Email pipes and Cron jobs that create browser output can be "confusing" for some operating systems.  Since there is no HTTP request that starts the process, there is no logical place to send the server response.  You might consider adding ob_end_clean() as the last line of the PHP script.
0
 

Author Comment

by:QMBB
Comment Utility
Ray,

That worked! I got the email when it ran via cron. So what does that say about my original script? MAYBE it's the fact that there are caps in the file name? Just realized that's the only thing I haven't changed. Trying that now...
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Could be!  

Some file systems are case-sensitive, some are not.  I try to treat all development and deployment environments as if they were case-sensitive (even though I know that PHP allows case-insensitive class and function names).  It's just a more "foolproof" approach.
0
 

Author Comment

by:QMBB
Comment Utility
Nope. Case made no diff. Wth?
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
So what does that say about my original script?
Don't know; haven't seen the original script.  Maybe strip it down to the SSCCE and start building it back up step-wise so you can see which of the steps causes the failure?  That's what I would do.
0
 

Author Comment

by:QMBB
Comment Utility
Doing that now, testing. Will report back tomorrow. Thanks for the help thus far.
0
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
My guess is that you have a problem within your original script. Maybe you're trying to use or rely on environmental variables that don't exist when the script is run from the command line. For example, when you run a script via the browser, you'll get variables like $_SERVER["REMOTE_ADDR"] and $_SERVER["HTTP_USER_AGENT"] but those will either not exist or be empty when run via cron. There are a handful of others, as well.

Your best bet is to attach your script (sanitized so that all usernames/passwords/domains/sensitive info is removed) for review.
1
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:QMBB
Comment Utility
Tested again after cleaning, no go. Cleaned version attached.
script.txt
0
 
LVL 9

Expert Comment

by:Brian Tao
Comment Utility
You're basically doing a bunch of "echo" function calls in the script.
Of course it works in browser and not in a cron job.  There's no place for the PHP to echo to.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
From the look of this code, it appears that you may be new to PHP.  If you want to learn the language, this article can help you get a foundation.  You can probably skip over parts of the background and basics, but at least it will point you to well-vetted trustworthy learning resources.

The "echo" statements are useless in a script that does not have browser output, but their output (which would otherwise go to a browser) can be captured via output buffering.  Please see the earlier comment about this.  The information from the echo statements may be useful for debugging.  Please see the script posted below.

The file paths may need to be fully qualified.  What works relative to the WWW root directory is not likely to work relative to the CRON root directory!  Please see the earlier comment about this.  Check "feed.txt" with var_dump() to see if you're getting what you think.

The PHP error control operator is a bad idea.  Read the man page, so you understand why you do not want to use it.  Instead, figure out what is causing the error messages and fix the root cause.  Remove the @-signs.  No more @-signs in your code, ever!  The error suppression could have been masking your problems all along, but there would be no way to know.

Suggest you try the script like this.  Check the comments carefully so you know what changed!  Put your own email address in the appropriate place near line 50.  When this runs, it will email its output to you, just like the other script did.  Then you can see what it did, what messages were produced, etc.  That should help you get started with the debugging process.  

Sidebar note: PHP echo is not a function -- it's a language construct.  You don't need the parentheses, so save yourself some typing :-)

Best regards, and best of luck, ~Ray
<?php
ob_start(); // COLLECT THE "BROWSER" OUTPUT
ini_set('display_errors', TRUE); // GET "BROWSER" OUTPUT FOR ERROR DISPLAY
error_reporting(E_ALL); // RAISE ERROR REPORTING LEVEL TO HIGHEST

include_once ('.config.inc.php'); 

$serviceUrl = "https://mws.amazonservices.com";

$config = array (
  'ServiceURL' => $serviceUrl,
  'ProxyHost' => null,
  'ProxyPort' => -1,
  'MaxErrorRetry' => 3,
);

 $service = new MarketplaceWebService_Client(
     AWS_ACCESS_KEY_ID, 
     AWS_SECRET_ACCESS_KEY, 
     $config,
     APPLICATION_NAME,
     APPLICATION_VERSION);

$feed = file_get_contents("feed.txt"); // IS THIS FILE PATH CORRECT?
var_dump($feed); // SEE IF WE GOT WHAT WE WANTED FROM "feed.txt"

$marketplaceIdArray = array("Id" => array('ABCD1234ETC'));

$feedHandle = fopen('php://memory', 'rw+'); // REMOVED ERROR CRONTROL OPERATOR
fwrite($feedHandle, $feed);
rewind($feedHandle);

$request = new MarketplaceWebService_Model_SubmitFeedRequest();
$request->setMerchant(MERCHANT_ID);
$request->setMarketplaceIdList($marketplaceIdArray);
$request->setFeedType('_POST_FLAT_FILE_LISTINGS_DATA_');
$request->setContentMd5(base64_encode(md5(stream_get_contents($feedHandle), true)));
rewind($feedHandle);
$request->setPurgeAndReplace(false);
$request->setFeedContent($feedHandle);
$request->setMWSAuthToken('<MWS Auth Token>'); // Optional

rewind($feedHandle);

invokeSubmitFeed($service, $request);

fclose($feedHandle); // REMOVED ERROR CRONTROL OPERATOR

$info = ob_get_clean(); // COPY THE OUTPUT BUFFER
mail('You@Your.org', 'SCRIPT INFO', $info); // EMAIL IT TO YOURSELF - SET YOUR OWN EMAIL ADDRESS HERE


  function invokeSubmitFeed(MarketplaceWebService_Interface $service, $request) 
  {
      try {
              $response = $service->submitFeed($request);
              
                echo ("Service Response\n");
                echo ("=============================================================================\n");

                echo("        SubmitFeedResponse\n");
                if ($response->isSetSubmitFeedResult()) { 
                    echo("            SubmitFeedResult\n");
                    $submitFeedResult = $response->getSubmitFeedResult();
                    if ($submitFeedResult->isSetFeedSubmissionInfo()) { 
                        echo("                FeedSubmissionInfo\n");
                        $feedSubmissionInfo = $submitFeedResult->getFeedSubmissionInfo();
                        if ($feedSubmissionInfo->isSetFeedSubmissionId()) 
                        {
                            echo("                    FeedSubmissionId\n");
                            echo("                        " . $feedSubmissionInfo->getFeedSubmissionId() . "\n");
                        }
                        if ($feedSubmissionInfo->isSetFeedType()) 
                        {
                            echo("                    FeedType\n");
                            echo("                        " . $feedSubmissionInfo->getFeedType() . "\n");
                        }
                        if ($feedSubmissionInfo->isSetSubmittedDate()) 
                        {
                            echo("                    SubmittedDate\n");
                            echo("                        " . $feedSubmissionInfo->getSubmittedDate()->format(DATE_FORMAT) . "\n");
                        }
                        if ($feedSubmissionInfo->isSetFeedProcessingStatus()) 
                        {
                            echo("                    FeedProcessingStatus\n");
                            echo("                        " . $feedSubmissionInfo->getFeedProcessingStatus() . "\n");
                        }
                        if ($feedSubmissionInfo->isSetStartedProcessingDate()) 
                        {
                            echo("                    StartedProcessingDate\n");
                            echo("                        " . $feedSubmissionInfo->getStartedProcessingDate()->format(DATE_FORMAT) . "\n");
                        }
                        if ($feedSubmissionInfo->isSetCompletedProcessingDate()) 
                        {
                            echo("                    CompletedProcessingDate\n");
                            echo("                        " . $feedSubmissionInfo->getCompletedProcessingDate()->format(DATE_FORMAT) . "\n");
                        }
                    } 
                } 
                if ($response->isSetResponseMetadata()) { 
                    echo("            ResponseMetadata\n");
                    $responseMetadata = $response->getResponseMetadata();
                    if ($responseMetadata->isSetRequestId()) 
                    {
                        echo("                RequestId\n");
                        echo("                    " . $responseMetadata->getRequestId() . "\n");
                    }
                } 

                echo("            ResponseHeaderMetadata: " . $response->getResponseHeaderMetadata() . "\n");
     } catch (MarketplaceWebService_Exception $ex) {
         echo("Caught Exception: " . $ex->getMessage() . "\n");
         echo("Response Status Code: " . $ex->getStatusCode() . "\n");
         echo("Error Code: " . $ex->getErrorCode() . "\n");
         echo("Error Type: " . $ex->getErrorType() . "\n");
         echo("Request ID: " . $ex->getRequestId() . "\n");
         echo("XML: " . $ex->getXML() . "\n");
         echo("ResponseHeaderMetadata: " . $ex->getResponseHeaderMetadata() . "\n");
     }
 }

Open in new window

0
 

Author Comment

by:QMBB
Comment Utility
Ray,

Tried your altered script, got a cron error email to my main server account email address:

Fatal error: Class 'MarketplaceWebService_Client' not found in /usr/www/users/myuser/myfolder/anotherfolder/script/SubmitFeed2.php on line 17

I looked for anything around that line altered but it looks the same as the version which doesn't throw an error.
0
 

Author Comment

by:QMBB
Comment Utility
I altered the version I attached before to fully qualify the paths to the .config file and the feed.txt file. Still no go. No errors, but again no results.
0
 

Author Comment

by:QMBB
Comment Utility
Ray,

Hopefully you have time and can look at what might have broken it in your version... I'd like to get that version working.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
I don't think I can fix this from where I sit, but maybe we can find a way forward.  The missing class was probably supposed to be included by the statement on line 6.  If so, you would need to find a way to give a correct URL on line 6.  This same issue may be there on line 24, too.

Try changing line 6 from include_once ('.config.inc.php'); to require_once ('.config.inc.php');

That should be expected to fail the first time because require() is expected to fail if the path is wrong or the file is missing.  Then when you change the path to correctly locate config.inc.php, you will know that you're getting the file because require() will not fail.
0
 

Author Comment

by:QMBB
Comment Utility
Ray,

Did this, ran it, got exact same error:

<?php
ob_start(); // COLLECT THE "BROWSER" OUTPUT
ini_set('display_errors', TRUE); // GET "BROWSER" OUTPUT FOR ERROR DISPLAY
error_reporting(E_ALL); // RAISE ERROR REPORTING LEVEL TO HIGHEST

require_once ('/usr/www/users/myuser/myfolder/anotherfolder/script/.config.inc.php');

$serviceUrl = "https://mws.amazonservices.com";

$config = array (
  'ServiceURL' => $serviceUrl,
  'ProxyHost' => null,
  'ProxyPort' => -1,
  'MaxErrorRetry' => 3,
);

 $service = new MarketplaceWebService_Client(
     AWS_ACCESS_KEY_ID,
     AWS_SECRET_ACCESS_KEY,
     $config,
     APPLICATION_NAME,
     APPLICATION_VERSION);

$feed = file_get_contents("/usr/www/users/myuser/myfolder/anotherfolder/script/feed.txt"); // IS THIS FILE PATH CORRECT?
var_dump($feed); // SEE IF WE GOT WHAT WE WANTED FROM "feed.txt"

$marketplaceIdArray = array("Id" => array('ABC12345678'));

$feedHandle = fopen('php://memory', 'rw+'); // REMOVED ERROR CRONTROL OPERATOR
fwrite($feedHandle, $feed);
rewind($feedHandle);

etc etc
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Are you sure these file paths are correct?  What do you see if you isolate this file and print out the contents?  In particular, is there the expected class definition for MarketplaceWebService_Client?

'/usr/www/users/myuser/myfolder/anotherfolder/script/.config.inc.php'
0
 

Author Comment

by:QMBB
Comment Utility
Ray,

I think you found it... the path is indeed correct, but if you notice at the bottom of the file I attached earlier, the code just stops, no php end. There are a few files up one level from .config which need to be there in order for it to run ( I moved them all into same folder as a test and it wouldn't run). That web service must be specified in one of those.

I could mush all of it together, but I don't see anywhere that the other files are called for in what I attached earlier... do you?
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
the code just stops, no php end
Can you please give me an example of this or paraphrase the explanation?  I'm trying to visualize it, but I'm coming up blank, sorry.
0
 

Author Comment

by:QMBB
Comment Utility
Here's what I attached before... you can see at the end that it just trails off, doesn't end php. That and my failed test when I put all the files in the same folder as the .config file tells me that the other files are being called and used when i run a successful test from a browser...

<?php

include_once ('.config.inc.php'); 

$serviceUrl = "https://mws.amazonservices.com";

$config = array (
  'ServiceURL' => $serviceUrl,
  'ProxyHost' => null,
  'ProxyPort' => -1,
  'MaxErrorRetry' => 3,
);

 $service = new MarketplaceWebService_Client(
     AWS_ACCESS_KEY_ID, 
     AWS_SECRET_ACCESS_KEY, 
     $config,
     APPLICATION_NAME,
     APPLICATION_VERSION);

$feed = file_get_contents("feed.txt");

$marketplaceIdArray = array("Id" => array('ABCD1234ETC'));

$feedHandle = @fopen('php://memory', 'rw+');
fwrite($feedHandle, $feed);
rewind($feedHandle);

$request = new MarketplaceWebService_Model_SubmitFeedRequest();
$request->setMerchant(MERCHANT_ID);
$request->setMarketplaceIdList($marketplaceIdArray);
$request->setFeedType('_POST_FLAT_FILE_LISTINGS_DATA_');
$request->setContentMd5(base64_encode(md5(stream_get_contents($feedHandle), true)));
rewind($feedHandle);
$request->setPurgeAndReplace(false);
$request->setFeedContent($feedHandle);
$request->setMWSAuthToken('<MWS Auth Token>'); // Optional

rewind($feedHandle);

invokeSubmitFeed($service, $request);

@fclose($feedHandle);
                                        
  function invokeSubmitFeed(MarketplaceWebService_Interface $service, $request) 
  {
      try {
              $response = $service->submitFeed($request);
              
                echo ("Service Response\n");
                echo ("=============================================================================\n");

                echo("        SubmitFeedResponse\n");
                if ($response->isSetSubmitFeedResult()) { 
                    echo("            SubmitFeedResult\n");
                    $submitFeedResult = $response->getSubmitFeedResult();
                    if ($submitFeedResult->isSetFeedSubmissionInfo()) { 
                        echo("                FeedSubmissionInfo\n");
                        $feedSubmissionInfo = $submitFeedResult->getFeedSubmissionInfo();
                        if ($feedSubmissionInfo->isSetFeedSubmissionId()) 
                        {
                            echo("                    FeedSubmissionId\n");
                            echo("                        " . $feedSubmissionInfo->getFeedSubmissionId() . "\n");
                        }
                        if ($feedSubmissionInfo->isSetFeedType()) 
                        {
                            echo("                    FeedType\n");
                            echo("                        " . $feedSubmissionInfo->getFeedType() . "\n");
                        }
                        if ($feedSubmissionInfo->isSetSubmittedDate()) 
                        {
                            echo("                    SubmittedDate\n");
                            echo("                        " . $feedSubmissionInfo->getSubmittedDate()->format(DATE_FORMAT) . "\n");
                        }
                        if ($feedSubmissionInfo->isSetFeedProcessingStatus()) 
                        {
                            echo("                    FeedProcessingStatus\n");
                            echo("                        " . $feedSubmissionInfo->getFeedProcessingStatus() . "\n");
                        }
                        if ($feedSubmissionInfo->isSetStartedProcessingDate()) 
                        {
                            echo("                    StartedProcessingDate\n");
                            echo("                        " . $feedSubmissionInfo->getStartedProcessingDate()->format(DATE_FORMAT) . "\n");
                        }
                        if ($feedSubmissionInfo->isSetCompletedProcessingDate()) 
                        {
                            echo("                    CompletedProcessingDate\n");
                            echo("                        " . $feedSubmissionInfo->getCompletedProcessingDate()->format(DATE_FORMAT) . "\n");
                        }
                    } 
                } 
                if ($response->isSetResponseMetadata()) { 
                    echo("            ResponseMetadata\n");
                    $responseMetadata = $response->getResponseMetadata();
                    if ($responseMetadata->isSetRequestId()) 
                    {
                        echo("                RequestId\n");
                        echo("                    " . $responseMetadata->getRequestId() . "\n");
                    }
                } 

                echo("            ResponseHeaderMetadata: " . $response->getResponseHeaderMetadata() . "\n");
     } catch (MarketplaceWebService_Exception $ex) {
         echo("Caught Exception: " . $ex->getMessage() . "\n");
         echo("Response Status Code: " . $ex->getStatusCode() . "\n");
         echo("Error Code: " . $ex->getErrorCode() . "\n");
         echo("Error Type: " . $ex->getErrorType() . "\n");
         echo("Request ID: " . $ex->getRequestId() . "\n");
         echo("XML: " . $ex->getXML() . "\n");
         echo("ResponseHeaderMetadata: " . $ex->getResponseHeaderMetadata() . "\n");
     }
 }

Open in new window

0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
This expert suggested creating a Gigs project.
The "end PHP" tag -- ?> -- is an Anti-Practice (see #14).  We avoid using that because it causes more problems than it's worth.  Most coding standards recommend against it.

I think at this point I've done all I can for you here.  You need an experienced PHP programmer to get "hands on" with your code and data, so I'll recommend that you explore E-E Gigs as a good way forward from here.

Best regards and best of luck with your project, ~Ray
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
This article offers some helpful and general tips for safe browsing and online shopping. It offers simple and manageable procedures that help to ensure the safety of one's personal information and the security of any devices.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now