Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sh: /cat: No such file or directory

Posted on 2012-03-13
19
Medium Priority
?
637 Views
Last Modified: 2012-03-24
Hello,

I'm running a daily cron job which triggers a php script name ee-boom.php.  I've attached it to this question.

Essentially, what you will see is that the PHP script queries a "product_catalog.txt" file and extracts matching query string values and dumps them into another file called R.txt.  

That R.txt is then used on the front end of the site to serve up relevant data.  

There was nothing wrong with the script or the cron job, until I recently updated Plesk.  Upon updating Plesk, I began receiving a daily email stating:

Subject: Cron <root@domain> php -f /var/www/vhosts/domain.org/httpdocs/ee-boom.php

sh: /cat: No such file or directory

This issue has now prevented the cron job from triggering ee-boom.php successfully, thereby negating the update to R.txt.

Any insight to get this script running successfully again is greatly appreciated.

Thank you!
ee-boom.php
0
Comment
Question by:pmagony
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 6
  • 3
  • +1
19 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 37714756
try
$cat_cmd = 'cat ' . $txt_catalog . ' | grep "Value String" > /var/www/vhosts/domain.org/httpdocs/boom/R.txt';
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 37714770
I could imagine that changing

$cat_cmd = 'cat' . $txt_catalog  .....

to

$cat_cmd = '/usr/bin/cat' . $txt_catalog

would help, but that's just a wild guess ...
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37714772
The strategy I would follow here is to learn what the CRON job sees.  Make the appropriate changes in the email address and try this.  It is untested, but valid in principle.
<?php
ini_set("error_reporting", E_ALL);
ini_set("display_errors", TRUE);
ini_set("memory_limit","100M");

// START THE OUTPUT BUFFERS
ob_start();

// GET THE ENVIRONMENTAL INFORMATION
phpinfo();

// VISUAL DELIMITER
echo PHP_EOL . '****************************************************************';

$txt_catalog = '/var/www/vhosts/domain.org/httpdocs/product_catalog.txt';
$cat_cmd = 'cat ' . $txt_catalog . ' | grep "Value String" > /var/www/vhosts/domain.org/httpdocs/boom/R.txt';

$cmd   = fopen('/var/www/vhosts/domain.org/httpdocs/product_catalog.txt', 'w'); 
$lines = gzfile('/var/www/vhosts/domain.org/httpdocs/product_catalog.txt.gz'); 
$lines = implode(PHP_EOL, $lines); 
fwrite($cmd, $lines); 
fclose($cmd);

if(file_exists($txt_catalog)) {
	$ret2 = exec($cat_cmd, $ret_val2);
}
else
{
    echo "!file_exists($txt_catalog)";
}

// CAPTURE AND MAIL THE OUTPUT BUFFER
$buf = ob_get_clean();
mail ('You@Your.org', 'CRON INFO' $buf);

Open in new window

0
Optimum High-Definition Video Viewing and Control

The ATEN VM0404HA 4x4 4K HDMI Matrix Switch supports 4K resolutions of UHD (3840 x 2160) and DCI (4096 x 2160) with refresh rates of 30 Hz (4:4:4) and 60 Hz (4:2:0). It is ideal for applications where the routing of 4K digital signals is required.

 
LVL 9

Author Comment

by:pmagony
ID: 37714858
@Ozo,

Your suggestion is identical to what is already in the file.

@woolmilkporc, I will give your suggestion a try.  My only issue is, is that I won't know the actual results until tomorrow, after the cron job has execute.  Unless there is a way to manually trigger the cron without interrupting it's schedule?

@Ray, thank you for the lovely code.  I have amended the file with the additional information, however, I do not see a reason to output phpinfo(); into the message.  Any reason why this was recommended?

If I run ee-boom.php in the browser manually, I receive an email chock full of html code from the php info output.  But nothing else.  If I comment it out, and run the file, then I simply get an email with the PHP_EOL visual delimiter.  Should I be receiving any other output data?
0
 
LVL 84

Expert Comment

by:ozo
ID: 37714943
My suggestion is not identical to what is already in the file.
It contains an essential change.
(other changes may be called for, but try that first)
0
 
LVL 84

Expert Comment

by:ozo
ID: 37714954
http:#a37714772 also contains the change I suggested
0
 
LVL 9

Author Comment

by:pmagony
ID: 37715007
Thanks Ozo,

Are you referring to the space after t in 'cat '? If so, will implement, thank you!
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37715580
I do not see a reason to output phpinfo(); into the message.  Any reason why this was recommended?
Yes, it is there so that you can find and compare things like $_SERVER["DOCUMENT_ROOT"] and the basepath, any other things that might not be the same when you compare its operation as a CRON job to its operation as a web script.

If you're not getting enough information to begin understanding the script's operation, you might want to add some additional visualization to the script.  You might want to add var_dump($lines) after line 20.  Etc, until you can find something that smells fishy.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37715694
the space after t in 'cat '?
Yes, I think it belongs there.
0
 
LVL 9

Author Comment

by:pmagony
ID: 37719952
Good morning!

This morning I received the exact same CRON notification, it appears the issue still persists.

Subject: Cron <root@domain> php -f /var/www/vhosts/domain.org/httpdocs/ee-boom.php
Body: sh: /cat: No such file or directory

I can confirm the 'cat ' has a space in it.  There is one thing I have not tried and that is @woolmilkporc suggestion of putting '/usr/bin/cat ' for the $cat_cmd variable.

Just a friendly reminder, all this starting occurring after a PLESK update. Could it be a permission issue?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37719992
It could be permissions, but it is more likely a path issue.  Going down the directory tree, I would try to verify that /var/ is a directory, then /www/ is a directory, etc.  See:
http://php.net/manual/en/function.is-dir.php
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37719997
Have you had a chance to compare the outputs of phpinfo() yet?
0
 
LVL 9

Author Comment

by:pmagony
ID: 37720085
Hi Ray,

If this was working and the site has not been moved, how could a path change have transpired?  The site has always resided at /var/www/vhosts/domain.org/httpdocs/

I'm not sure what you mean by compare the outputs of phpinfo.  The email comes in as text, and all the html makes it nearly impossible to read it correctly.  What should I be comparing?

Thanks!
0
 
LVL 9

Author Comment

by:pmagony
ID: 37720095
I added the following to the headers to fix the HTML issue:

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";

Now I just need to know "what" to look for in the phpinfo output :^)
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37720447
Look at the Core, Environment and PHP Variables in the phpinfo() output.  You would want to see if they are the same in every environment where you run this script.  Some adjustments may be necessary for scripts that are started in different environments.
0
 
LVL 9

Author Comment

by:pmagony
ID: 37724508
Alright guys, I tried:

'cat '
'/usr/bin/cat '

with no luck.  Ray, I have compared the phpinfo criteria you pointed out and I do not see any differences.  I have also confirmed that /var, /www, /vhosts are directories. I walked through them via SSH to finally get to the site itself.

I'm still getting the same error:

sh: /cat: No such file or directory
0
 
LVL 9

Accepted Solution

by:
pmagony earned 0 total points
ID: 37729384
I think I was able to resolve the issue... I started to play around with the ownership of ee-boom.php and as it turns out, when running directives in a php file, they have to be run as the same owner as the CRON job that is executing it.

So I changed the ownership to apache.  I then ran the file in SSH and started receiving safe mode errors.

Oddly, when I logged into PLESK, to disable "safe mode" in the settings, it was already disabled.  This puzzled me so I opened up php.ini and sure enough, it was set to on.  Once I turned it off, and restarted services, the file now runs error free.

I'm going to get a definitive answer tomorrow morning once the CRON job runs. I 'll report back tomorrow with my findings.
0
 
LVL 9

Assisted Solution

by:pmagony
pmagony earned 0 total points
ID: 37738987
The issue is resolved based on an Ownership change of the files.
0
 
LVL 9

Author Closing Comment

by:pmagony
ID: 37760288
I was able to resolve the issue by analyzing the ownership of each file, and how CRON jobs execute shell commands.
0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
The viewer will learn how to count occurrences of each item in an array.

688 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