I have a problem when you run a script from php the problem is that it doesn't do what must be done, this is the php code
? php
echo "starting execution";
echo exec("./sc");
?>
and in the code of the script this is sc
mv text.txt /var/www/files/
does not work does not move the file archive there is I've added the chmod permismos but doesn't appreciate any suggestion
First, I would suggest to execute the script file manually from command line(console) and check if the script file is working properly. If that doesn't work, then it won't work in php too.
Try:
echo "starting execution";
echo exec("./sc", $output);
var_dump($output);
contents of sc :
mv text.txt /var/www/files/ 2>&1
If the file is not moved, it must throw an error which you should be able to see in $output.
My guess would be that the error must be related to permission.
Related
I've moved my code form one WAMP computer to another and the code that runs pdftk stopped working. I've compared the permissions on pdftk.exe and they are the same on both machines. When I run the same command from a command prompt, it works. I add exec("whoami") to the script and the user is the same on both computers. When I run something like exec('dir 2>&1', $out) it executes so I know exec is working form within php.
I've created a trivial php file to test and it doesn't work.
<?php
$String = 'pdftk.exe > "c:\temp\temp.txt"';
exec("$String");
exec("pdftk.exe > \"c:\temp\temp.txt\"");
?>
Both exec command result in a 0 byte file being created.
if I run
pdftk.exe > "c:\temp\temp.txt"
from a command line it puts the output of pdftk.exe into the temp.txt file as expected.
This seems like is must be some kind of permissions issue, but permissions on the executable seem to be the same. Losing my mind on this.
In my opinion, first line should be:
<?php
$String = 'pdftk.exe > "c:\temp\temp.txt"';
exec($String); //removed quotes
?>
And for the second line, what if you try this?
<?php
exec('pdftk.exe > "c:\temp\temp.txt\"', $outputAndErrors, $returnValue);
var_dump($outputAndErrors);
?>
Or if you remove your first ">"?
<?php
exec('pdftk.exe "c:\temp\temp.txt\" 2>&1', $outputAndErrors, $returnValue);
var_dump($outputAndErrors);
?>
These are some tests that may help you, thought.
I expected this to be quite simple but I cannot make it work.
I'm trying to run a php script in command line (to finally be able to automate it in a cron.)
The file is outside of the public web folder to avoid executing it from a webbrowser.
File is called 'daily.php' and contains :
#!/usr/bin/php
<?
echo "hello \n";
file_put_contents("daily.log", "executed...", FILE_APPEND | LOCK_EX);
?>
then in my terminal I run (as root):
/usr/bin/php daily.php
but it simple outputs me the full source code without the hashbang.
So I tried changing the file to 755, tried to chmod +x it but still outputs me the full source code.
I had a look in the man page and I found :
-f <file> Parse and execute <file>.
Tried this but still the same output.
Why is this? how can I interpret this file?
Ok
I found the answer after 2 hours of trouble, and just after posting this question.
Even if you hashbang the file, even if you call php directly, you need to implicitly tell it is php by using :
<?php
?>
and not
<?
?>
I'm trying to use a php file to call a shell script. First time.
#Shell script:
#!/bin/bash
echo "Hello, World!"
//PHP
echo "call shell";
$output = shell_exec('get_logs.sh');
echo "<pre>".$output."</pre>";
On the PHP page, I see the "call shell" printed. But no hello world and no errors. Can I see the errors somewhere or do I have a syntax error I'm not aware of?
you probably need to check if the assigned user for PHP/Apache has execute access to the file.
I think you may have to specify a full path to your shell script.
i.e.
/usr/local/bin/get_logs.sh
But it depends where you put it.
Make sure the script is marked executable
i.e. chmod +x /usr/local/bin/get_logs.sh
Make sure #!/bin/bash is the first line in the file, i.e. remove "#Shell script:"
Thomas, your script should simply contain:
#!/bin/bash
echo "Hello, World!"
After you've created it. Mark it with execute permissions.
i.e. in a terminal run "chmod +x [filename]"
Where filename is the name of the script which can include the path also.
I have a python file that I would want to execute whenever a php page is called. The python file is in the same folder as the php file. The python script on execution edits a textfile that the php file accesses.
I have the following code:
<?php
exec("python somefile.py",$output);
$file = fopen("test.txt",'r');
....
For some reason, the python script never gets executed. I know this certainly as I can see it from the changes in the text file.
Since I was not sure if the script was made executable, so I included "python" on the command.
I also ran:
chmod +x somefile.py
just to make sure this was not the reason. But this did not help too.
What should I change to make the php page execute the python script whenever it is called?
This is most likely a permission issue.
Try
echo exec("whoami");
This will let you know who php is running as. Then you need to verify this user can run the python script. If the file was not created by the same daemon that runs python, you will most likely be denied permission.
Update
This will let you know who owns all the files you are working with. The file being written to needs to be writable by the user that is running python. If you are running python from ssh, that is most likely not the same user as when you run python from exec.
echo exec('whoami') . "<br>";
echo exec("ls -l test.txt") . "<br>";
echo exec("ls -l somefile.py") . "<br>";
Update 2
Because I constantly forget this exists.
passthru('python somefile.py 1 2>&1');
This will exec your python file, and output stderr to stdout.
I am trying to call a simple python script
#!/usr/local/python25/bin/python
print "hello world"
from the following php script
<?php
echo exec("/usr/local/python25/bin/python myfile.py");
?>
But nothing was happened.
Please tell me what is wrong here? (I also checked other thread but I could not solve my problem)
Question Solved:
I forgot to give the permission to access /usr/local/python25/bin/python. After I did this, the problem solved.
Thank you so much for your help!
1.The exec function just return the last line from the result of the command.
2. The print statement in python (except python 3) automatically adds a newline at the end.
This is the reason you feel nothing was happened.
You can catch the whole output by this way.
exec("/usr/local/python25/bin/python myfile.py 2>&1", $output);
print_r($output);
Kind of an obvious point here, but can you run the python script from a terminal? Does it actually run?
Make sure the script is executable by whatever user PHP is running as - chmod 777 myfile.py, and just to be safe chmod 777 /usr/local/python25/bin/python. Also, make sure the python script is in the same directory as the PHP script, which is what your method of calling it requires.
Try changing your PHP script to this, and tell me what you see: (EDITED)
<?php
// Path to the python script - either FULL path or relative to PHP script
$pythonScript = 'myfile.py';
// Path to python executable - either FULL path or relative to PHP script
$pythonExec = '/usr/local/python25/bin/python';
// Check the file exists and PHP has permission to execute it
clearstatcache();
if (!file_exists($pythonExec)) {
exit("The python executable '$pythonExec' does not exist!");
}
if (!is_executable($pythonExec)) {
exit(("The python executable '$pythonExec' is not executable!"));
}
if (!file_exists($pythonScript)) {
exit("The python script file '$pythonScript' does not exist!");
}
// Execute it, and redirect STDERR to STDOUT so we can see error messages as well
exec("$pythonExec \"$pythonScript\" 2>&1", $output);
// Show the output of the script
print_r($output);
?>
If you want to capture the subprocess' stdout, you should use passthru
Also you don't need the first line of that python script if you're calling the python interpreter directly.