I'm trying to execute a perl script from php.
That perl script is reading a webpage and producing an xml file with some data.
If i exec it via shell everything works as expected but when I'm trying to automate the whole thing via php nothing works.
I've tried this:
exec("perl /absolute/path/to/perl/script/parser.pl", $output, $result);
when i echo the $result variable it displays 2.
I've switched the php safe_mode On and Off several times but nothing changed.
Also tried to set the safe_mode_exec_dir without positive results.
When i exec a simple ls
exec("ls", $output, $result);
everything goes well, i get the list of files and $result is 0.
The perl script has chmod 777 so that it would not be a problem of permission. Also his folder has 777.
I'm almost sure that the issue is something about the server but I'm not able to find out what.
Any help would be really appreciated.
Thanks
You probably need to specify the full path to your perl executable.
Try typing which perl to see what the full path is, then place that full path in your exec call.
Your example seems to be missing the end quote for the first argument. I would also make sure that the executable for perl is in your path. Lastly, not only does the folder need to have the execute bit set, but all of the directories up to root need to be set to allow perl to access the script. It is fairly common for a directory to be 750 such that a group can get in, but not the world. As a user you may be able to cd there and see the script, but you must look at it from the perspective of the users that is executing the script -- presumably the id your webserver is running as.
Related
I'm trying to run a python script from php with
$res = '';
exec('./home/Desktop/Scripts/fetch_matches.py', $res);
My python file starts with #!/usr/bin/python and has exec rights. For now I only have a print in there but it's not working(var_dump($res) gets me an empty array). What's missing?
Also, if I'll have different methods in that script, how would I call them?
If you change your exec to include /usr/bin/python this should work as expected:
exec('/usr/bin/python ./home/Desktop/Scripts/fetch_matches.py', $res);
In these circumstances you should probably use absolute paths (check realpath as well as chdir and any argument escaping you need to do).
I'm not completely familiar with how the shebang in files is run but I know if calling from PHP it's far better just to include the interpreter in the command you run.
You need to get rid of the . before the path. If it's a normal Unix/Linux system, /home is in the root of the filesystem. . means the current directory, so unless you've recreated the /home filesystem in the directory from which you're running the PHP program, the path is incorrect. Also, unless your username is Desktop, you're missing a directory between /home/ and Desktop/ - it should be your username. The pwd command will never return a . before the present working directory.
For your second question, please refer to the docs on calling Python from the command line. You can execute arbitrary code from the command line, which could be something along the lines of
python -c "from fetch_matches import Fetcher; Fetcher()"
I have a Python script which is encoding a video and then calling a shell script which uploads the new video to dropbox. It works fine from the command line but I needed to make it so others could execute it so I have a PHP script calling the python script.
I don't want the PHP script to run forever (it takes 15-30 mins for it to complete), I just want it to kick off the python script and be done. I figured out what I need to make that happen and like I said it works on the command line. But when it is called via PHP, the video encodes but the file never uploads. I can see the dropbox script was kicked off and is listed as a process using some percent of CPU, that percent never changes, it seems stuck/dead.
the command looks like this, being run using cmd()
script.py -options &>/logs/phptopython.log &
The shell script is kicked off using Popen
Any suggestions?
thanks
It sounds like this could be a permissions issue. Double check the permissions on the directory to which you are trying to upload the video. If you are on Linux you can modify the permissions on that directory like this:
chmod 755 /path/to/dir
This gives the file owner read, write and execute permissions (7). The group and other users get read and execute permissions (5).
Apache is likely running as a different user than when you run the command yourself in bash. A quick test to see if it's a permission issue would be to grant 777 on that directory. I wouldn't leave it that way though – it'd just be a way to quickly identify if permissions are the issue.
If the script works with 777 permissions, you could either change the owner of the directory to the user Apache runs as or add the Apache user to the directory's group and grant the group write permisssions.
Edit:
I just noticed you said you use cmd(), so I'm guessing you are on Windows. My comments might still be relevant but the chmod command won't work on Windows.
i am trying to run this piece of php code on my server:
<?php
$cmd = 'echo "this is a test" > /home/ubuntu/scripts/test_file';
echo exec($cmd);
?>
From my understanding it should add the piece of text to the file test_file . The file exists in the appropriate location and i have tried chmod 755 and chmod 777 on the php file. But i dont see the text being added to the text_file . I tried running the linux command directly on the server and it works. Could some one tell me what i am doing wrong?
Also, i am trying to create a virtual host file on the server through a php script. Rather than running the commands through php exec() , i thought it would be better to run a shell script, with the shell script reading the required parameters from a text file and setting the directory path in the virtual host file. I am new to linux, is this a good approach or is there a better way in going about this? All this is being done to setup a magento based site programatically. Thanks.
Your code is OK. The problem probably either lies with your php being in safe mode (though it's deprecated, see link) or with file/directory permissions.
No need to give the file permissions 0777 since that makes the file executable, 0666 should suffice. It is not enough however for the file to have the right permissions, each directory on the path must be traversable. Try a different directory to which the user with whose privileges the php code runs has access, /tmp is a good start.
General way to debug problems like this is to execute a different command which gives you extra information about the context in which echo is executed, e.g.
<?php
echo exec("id");
echo "<br/>";
echo exec("ls -l /home/ubuntu/scripts/test_file");
?>
(remember exec() only returns the last line of command's output, these display just one line though). These commands will tell you the user which runs the code and whether they can see the file at all.
As the comment already said: this is actually bad way to accomplish what you're trying to do, as writing Apache configuration based on user input through web could open you up to multiple issues.
What you might consider, is to have the PHP side write the required information to a file, or a database, which is then polled every now and then via a cron script or similar by a different process that does the actual configuration changes. This eliminates the need to exec() from PHP (which is always bad). With this, your process that runs PHP wouldn't need to have write permissions to important system files.
HI there!
I've run into some problem while learning to combine .sh files and PHP. I've create a file test.sh and in that file I call a PHP file called test.php.
If I double click on the .sh file then it runs perfectly but when I try to run it from the terminal I get "command not found". I'm in the exact folder as my .sh file but it wont work. Here's my test.sh:
#!/bin/bash
LIB=${0/%cli/}
exec php -q ${LIB}test.php one two three
exit;
When I doubleclick on the test.sh file then it returns the argv array like it suppost to. But why can't I run it from terminal?
use ./filename.sh
no matter if your are in the same folder or not, without giving ./ the system is searching the path variable. (/bin/, /usr/bin and so on)
Is execute bit enabled?
chmod +x test.sh
Your $PATH variable may not include '.' - so the shell may not be able to find the command to run.
As others have said, sh ./file.sh will work...
It is possible that your environment is different when launching from the Terminal and when launching via a double-click. Try executing which php and echo $PATH from the Terminal to see what the deal is.
EDIT 1
As others have noted, if your "command not found" is referring to the shell script and not php, then you probably forgot to include the "./" before the name of the script (i.e. ./test.sh). Also, don't forget to make it executable by invoking chmod a+x test.sh. The reason for this is that PATH does not include the current directory (i.e. "."), because doing so would be a security risk (e.g. folders with a ton of files in them including a fake "ssh" which could then intercept your password or the like).
EDIT 2
Also, I don't know about you, but ${0/%cli/} is evaluating to -bash from within my Terminal. Are you sure that's what you wanted it to expand to? Perhaps you should specify the exact filename.
Another option is to run it with sh (or bash, if sh on your machine isn't bash and the script uses bashims)
sh filename.sh
bash filename.sh
This will work whether or not the file is executable or in $PATH
Scenario:
I have a php page in which I call a python script.
Python script when run on the command line (Linux) shows output on the command line, as well as writes the output to a file.
Python script when run through php, doesn't do either.
Elaboration:
I use a simple system command in PHP to run the python script as:
/var/www/html/1.php:
system('/usr/python/bin/python3 ../cgi-bin/tabular.py 1');
/var/www/cgi-bin/tabular.py
--This python file basically parses a data file, uses python's regular expression to search for specific headings and outputs the headings to the stdout, as well as write it to a file.
This python script has a few routines in it which get executed, so I put print statements to debug. I noticed only a few initial print statements' output in the PHP page, all the ones from the function that actually does something are not seen.
Also, as part of my test, I thought well the py script is in a different folder so let me change it to the /var/www/html folder, no go.
I hope I captured the problem statement with sufficient detail and someone is able to reproduce this issue at their end. If I make any progress on this one myself, I'll annotate this question. Thanks everyone.
Gaurav
I bet your py script has some bug which couses it to break when called from inside PHP.
Try
passthru('/usr/python/bin/python3 ../cgi-bin/tabular.py 1 2>&1');
to investigate (notice 2>&1 which causess stderr to be written to stdout).
A permission problem is most likely the case.
If apache is running as apache, then it will not have access to write to a file unless
The file is owned by apache
The file is in the group apache and group writable
The file is world writable
This is a "sticky" problem on a multi-user machine, as different people have access to Apache.
Try chmod 666 output.txt on the file and then re-run your test.
Considerations:
Have the python script write the output to a database
Use PHP's popen functionality to open the process and communicate over pipes
Re-write using PHP's regular expressions
Write the output file to /tmp and then read the results using PHP as soon as the python script is done.
etc...
Check that the user the python script is running is has write permissions in CWD. Also, try shell_exec() or passthru() to call the script, rather than system().