Exec python from php - php

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()"

Related

Getting exec() PHP to work like it does on the command line

The following command, which generates a .pdf file from a .tex file, works from the command line but not when I run it with PHP. The file has the appropriate permissions and I'm able to run other commands with exec() so not sure what is going on.
$file_path='uploads/some-path';
$full_path='uploads/some-path/file.pdf';
$cmd ="pdflatex -output-directory ".$file_path.' '.$full_path;
exec($cmd);
The flag -output-directory place the file in the file_path rather than the root directory.
Is pdflatex in the search path? Perhaps try specifying the full path to the executable and see if that makes a difference.

The "which" command won't work in PHP

I have two remote servers that need to have a certain codebase in sync. I have it all set up on the first, but while setting it up on the second, I ran into a problem:
They don't have hg installed in the same location. I can't just run hg <command> because it returns the error:
sh: hg: command not found
So I had been using the full path, but that won't work with two separate hg locations.
I thought it would be clever to run which hg and use the path from the response, so here's the new code:
$hg = trim(`which hg`, "\n");
$output = `{$hg} pull -u`
But $hg is NULL, so that doesn't work! I can ssh into the 2nd server and see that which hg definitely does work. I even appended 2>> path/to/log to see if there were any errors with the which command, but there was not. I made sure it was writing to the log, so it wasn't related to that.
I am not running in safe mode, and I am definitely allowed to run shell_exec because other commands work.
I know I could just create a symlink so they both had the same path and just hardcode the path, but it's driving me crazy why shell_exec('which hg') isn't working!!!
which hg will only work if hg is already in your path, otherwise it will return the empty string.
Obviously this is not what you want, since if it were already in your path, you could just use hg.
If you're using many machines with different configs, you could just create a bin directory in your home directory, add it to your path and make links to the commands you need in that directory. That will allow you to use the same commands on all machines.
For PHP, you'd need to put it in a directory accessible to the web server (but preferably outside the document root), besides that, you can use the same technique there by giving an absolute path to the common directory where you created your links.
try
shell_exec('/usr/bin/which hg');
instead. $PATH may not be set and that is a typical location of which

Executing a linux command with php exec() and running a shell script

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.

php can't execute a perl script via exec

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.

Problem executing bash file

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

Categories