I run a script locally, which when finished makes an exec call to a script on a remote server.
exec("ssh user#server.com \"php /full/path/to/script.php\"", $output, $return);
It presents me with this error:
PHP Warning: require(../resources/vendor/autoload.php): failed to open stream: No such file or directory in /full/path/to/script.php on line 3
I have tried charging the required script to it's full path with no success. Any ideas?
Because PHP sets CWD (current working directory) to where you are after SSH login (presumably the user home folder) and references to includes in the PHP file are interpreted as being relative to CWD. You should change directory to the root of your project:
exec("ssh user#server.com \"cd /full/path/to/; php script.php\"", $output, $return);
Related
I have a php file that has a shell_exec call. The shell_exec functions runs a .sh file.
#!/bin/bash
filename=$(ls *.jpg -Art | tail -n 1)
codegen_dir=/usr/local/codegen/
cd "$codegen_dir"
out=$(./classifier /var/www/$filename)
echo $out
The executable 'classifier' exists in the codegen_dir and has 1 shared library dependency. The script runs correctly from the command line. The php file also runs correctly from the command line. however, when I run the php file as a http request I get the following in std_err:
"./classifier: error while loading shared libraries: libreader.so: cannot open shared object file: No such file or directory"
The .so file is in the same directory as the executable
My php server root is : /var/www
All files in the server root have the permissions:-rwxrwxrwx 1 www-data www-data
All files in 'codegen_dir' have the permissions: -rwxrwxrwx 1 ubuntu www-data
I am able to read other files in the codegen_dir
Shared libarary path might be not accessible by apache user. You can allow classifier program in sudoers file for apache and use sudo to run classifier application as apache user
Or
make shared libraray and its path accessible by all users by changing its permission
out=$(sudo ./classifier /var/www/$filename)
Try to login with apache user and run above script or try to access shared lib
su -s /bin/bash apache
The following execution syntax actually runs in cron job:
/usr/local/bin/php -q /home/pbjwbh0mgv9o/public_html/buildlistings.php
Yet I get the following in my error_log:
[05-May-2018 21:53:00 UTC] PHP Warning: unlink(property_a.xml): No such file or directory in /home/pbjwbh0mgv9o/public_html/buildlistings.php on line 63
[05-May-2018 21:53:05 UTC] PHP Warning: unlink(property_map.xml): No such file or directory in /home/pbjwbh0mgv9o/public_html/buildlistings.php on line 215
Once again the following part of script executes perfectly in browser, and attached below is screenshot of directory:
unlink('property_a.csv');
unlink('property_a.xml');
unlink('property_map.xml');
Am I formatting the cron job command incorrectly, or missing something within my script related to Linux?
When you execute the script via the web server, the CWD (current working directory) is what you expect.
When cron executes that job, it's not happening from the same location. You need to make sure it calls cd to change to wherever you expect the files to be.
Edit:
Here's an example for your cron job:
cd /home/pbjwbh0mgv9o/public_html && /usr/local/bin/php -q buildlistings.php
php /home/test9/public_html/degerlendir/test-4567.php "var1=18&var2=22"
I need to run one page at background with cron job. I tested my code with command at above. But I get this error:
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/extensions/no-debug-non-zts-20100525/imagick.so' - /usr/lib64/extensions/no-debug-non-zts-20100525/imagick.so: cannot open shared object file: No such file or directory in Unknown on line 0
PHP Warning: require_once(../config.php): failed to open stream: No such file or directory in /home/test9/public_html/degerlendir/test-4567.php on line 2
PHP Fatal error: require_once(): Failed opening required '../config.php' (include_path='.:/usr/lib64/php') in /home/test9/public_html/degerlendir/test-4567.php on line 2
The problem is page doesn't include config.php in the parent directory. The page working in browser normally. I tried to use different require_once variations like require_once ($_SERVER['DOCUMENT_ROOT']."/config.php"). I could not get it to work.
from the command line there is no $_SERVER['DOCUMENT_ROOT']. That one is only available from the http-server (Apache).
The working directory will not be automatically set. If you prompt is currently at /some/path/ the script will try to find config.php in /some/config.php.
Try cd to the current path by using __DIR__ in the start of your script
<?php chdir(__DIR__); ?>
Cron jobs always take full path of the included file from your root.
/home/test/.../your-file
In your cron job, you need to cd to the correct working directory to allow your PHP file to find it's includes. I do this by creating small shell scripts, and run the shell scripts from cron:
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd ${DIR}
php test-4567.php
exit 0
This also gives you the ability to do some useful things like checking to see if the script is already running to make sure you don't spin up multiple threads if that's something you want to avoid.
#!/bin/bash
FIND_PROC=`ps -ef |grep "php test-4567.php" | awk '{if ($8 !~ /grep/) print $2}'`
# if FIND_PROC is empty, the process has died; restart it
if [ -z "${FIND_PROC}" ]; then
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd ${DIR}
php test-4567.php
fi
exit 0
I have a C program that I wrote called convert3to5, originally written for CentOS / Fedora 32bit system in early 2010. I am moving it to new CentOS 6.x 64bit system host.
From a CentOS Putty console I can run the convert3to5 command just fine; here is a sample of it running from my console:
[root#cloud convert3to5]# ls
CircleStar convert3to5 Convert3To5.txt test.tif
[root#cloud convert3to5]# ./convert3to5 /var/www/webadmin/data/www/mydomain.com/uploads/SV-DIS160217B.tif
TIFFReadDirectory: Warning, /var/www/webadmin/data/www/mydomain.com/uploads/SV-DIS160217B.tif: wrong data type 7 for "RichTIFFIPTC"; tag ignored. Image has an undefined fillorder - using default: MSB2LSB
The above is a normal completion of convert3to5 and I get a SV-DIS160217B.bmp that is placed in /var/www/webadmin/data/www/mydomain.com/uploads/ So running it from console works fine.
Question - I am attempting to run the same exact command from PHP using the exec(command, output, return) command as follows:
chdir($sv_path.$c3to5_path); //change our working directory to "/convert3to5" directory
$command = "./convert3to5 $targetFile 2>&1";
$result = exec($command, $output, $return);
// the output of the above command - is a .bmp file it will be placed in the same path as the input .tif file
I get the following $result:
ERROR: Unable to convert
/var/www/webadmin/data/www/mydomain.com/uploads/SV-DIS160217B.tif to 5
color BMP file: Open file Error: Tiff_3_to_BMP_5_.lut!
My convert3to5 does need to open Tiff_3_to_BMP_5_.lut
Why does it find Tiff_3_to_BMP_5_.lut when I run convert3to5 from a console prompt but not from PHP exec(...) in both cases my pwd shows that I am in
[root#cloud convert3to5]# pwd
/var/www/webadmin/data/www/mydomain.com/myView/convert3to5
I have also verified pwd is correct from my PHP script after the
chdir($sv_path.$c3to5_path);
Tiff_3_to_BMP_5_.lut is in CircleStar directory - the path to CircleStar is /var/www/webadmin/data/www/mydomain.com/myView/convert3to5/CircleStar
Summary: ./convert3to5 works while PHP exec('convert3to5 ..) does not appear to work.
Can anyone suggest the difference and how to fix and/or debug?
Thanks
You're running the console from the convert3to5 directory, and I suspect your old C program used a relative path to the .lut file, possible relative to the .tif?
What if in the console example you did
cd ../..
./path/to/convert3to5/convert3to5 /var/www/webadmin/data/www/mydomain.com/uploads/SV-DIS160217B.tif
Might be related to $targetFile. Print that and see if it's the full path.
Finally, run
/full/path/to/convert3to5 fullTargetPath
If that works, then as a workaround, if you just do exec('/full/path/to/convert3to5 $fullTargetPath, ..) it should behave like the console.
Per my above comment to wonton:
From the console I was running as root (so fully privileged). I supposed my PHP script will run as the "apache" user on the server?
Here was the problem I believe: I looked at the CircleStar directory privileges where the Tiff_3_to_BMP_5_.lut file exists. CircleStar had rw-r--r-- (0644) when running as root from console this allowed my convert3to5 program to find and open Tiff_3_to_BMP_5_.lut file just fine. However not the PHP exec(...) once I changed the privilege on CircleStar to rwxr-xr-x (0755) PHP exec(...) ran fine!
So ultimately it was a permission issue.
I am attempting to create a cronjob under the user apache, but I get permission denied errors for files that are accessed by the program. The specific file that my php script cannot access is /var/www/html/amazon/amazon_data.txt. Here is me checking the permissions, and testing to see if I can write to the file:
bash-3.2$ whoami
apache
bash-3.2$ ls -l /var/www/html/amazon/amazon_data.txt
-rwxrwxr-- 1 apache apache 1082 Apr 3 15:43 /var/www/html/amazon/amazon_data.txt
bash-3.2$ vi /var/www/html/amazon/amazon_data.txt
Now I try to run a script that tries to access the file I get this warning:
bash-3.2$ /usr/bin/php /var/www/html/amazon/amazon_inventory_sync.php
PHP Warning: Module 'json' already loaded in Unknown on line 0
PHP Warning: fopen(amazon_data.txt): failed to open stream: Permission denied in /var/www/html/amazon/amazon_inventory_sync.php on line 26
Warning: fopen(amazon_data.txt): failed to open stream: Permission denied in /var/www/html/amazon/amazon_inventory_sync.php on line 26
Unable to open amazon_data.txt!bash-3.2$
Why can I access and edit the file with the user just fine, but not in the php script when executing it via command line? There is no issue when I run the script from a browser.
Edit: I can run it fine under the user soh, who is in the group apache. apache is also in the group apache.
The issue was due to the file being requested not being a literal location. This is the code which did not work:
$filename = "amazon_data.txt";
$file = fopen($filename, "a+") or die("Unable to open $filename!\n");
This may work fine when the script is run remotely via HTTP, but it may cause issues when running cron jobs or execution in the terminal. Changing the $filename to be the full location fixed this issue.
$filename = dirname(__FILE__)."/"."amazon_data.txt";
$file = fopen($filename, "a+") or die("Unable to open $filename!\n");
Why does this happen? I am guessing instead of the php file's folder being used, it was using a directory which the apache user did not have access to, such as the current working directory that the command in terminal was being executed from.