one.sh
#! bin/bash
command="cp 357.svg 000.svg"
echo "Executing Command";
exec $command;
from shell by executing sh one.sh runs perfact and even in php shell_exec("sh one.sh"); works fine.
two.sh
#! bin/bash
command="/usr/bin/inkscape -f 357.svg -e 357.png"
echo "Executing Command";
exec $command;
From shell sh two.sh works fine
but using php shell_exec("sh two.sh") not executing
can any one please tell why it is not executing?
try :
echo shell_exec("sh two.sh 2>&1;")
and see what the output is, maybe it will give you a permission denied error.
Maybe also worth checking which user you are running with (probably something like www-data)
Related
I have a bash code like this:
sudo docker exec -it container_id /bin/bash -c 'cp test1.txt test2.txt'
rm test3.txt
It worked well in terminal. But I want to call it from html. My html code is :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>run bash</title>
</head>
<body>
<?php
if(isset($_POST['submit']))
{
$output=shell_exec('sh /Users/path/script.sh');
}
?>
<p><?php echo $output; ?></p>
</body>
Basically, I want to click the button "submit" (the php code I put above will be called by another html file) and it will execute the bash code to login to docker and run the command cp test1.txt test2.txt.
But it only deleted test3.txt while it didn't copy the file on docker when I ran it from html. Also, I didn't get any output from website. This was weird cause it could work in terminal just by sh /Users/path/script.sh.
I am new to both html and docker. Will be really appreciate it if anyone could give me advice.
Since you are executing docker with "sudo" you'd need the command to be executed via shell_exec() with sudo as well.
Furthermore, if your server environment uses chroot, the docker binary might not be even available to the user running the PHP process.
I suggest checking with your system administrator.
/off As this is not a programming question, you might have better luck approaching people in Server Fault
First of all you need to allowed ( probably www-data user ) to use sudo without password for this one script.
You can do this via sudoers:
vim /etc/sudoers
Add line:
<user> ALL=(ALL) NOPASSWD: /path/to/your/script.sh
Then your shell_exec should looks like this:
$output=shell_exec('/usr/bin/sudo /path/to/your/script.sh');
But be careful, use sudo only if know what you are doing ;)
I also suggest to print output using "pre"
<pre><?php echo $output; ?></pre>
Good luck !
In my case, the error was not about sudo.
To get the stdout, I ran this
<pre><?php echo exec('sh /path/to/my/script/script.sh 2>&1', $output, $return_var); ?></pre>
<pre><?php print_r($output); ?></pre>
<pre><?php echo $return_var; ?></pre>
Noted that when an error occurs during the execution, or the process does not produce output, shell_exec() will return NULL. Therefore, using function shell_exec() cannot detect whether the process is successfully executed by the return value. If you need to check the exit code executed by the process, use the exec() function instead.
It outputed the input device is not a TTY.
And in my bash script, I changed sudo docker exec -it container_id /bin/bash -c 'cp test1.txt test2.txt' into docker exec -i container_id /bin/bash -c 'cp test1.txt test2.txt'.
Basically, ranning docker exec -i rather than docker exec -it fixed my problem. Hope this will help guys have same problem.
I tried many solutions but nothing it works :
echo '<pre>';
shell_exec("python /home/folder/python/mapfile_compress.py");
shell_exec("sudo -u wwwexec python escapeshellcommand(/home/folder/python/mapfile_compress.py) $uid");
shell_exec("sudo chmod +x /home/folder/python/mapfile_compress.py");
system("bash /home/folder/python/mapfile_compress.py");
passthru("bash /home/folder/python/mapfile_compress.py");
passthru("/home/folder/python/mapfile_compress.py");
exec("bash /home/folder/python/mapfile_compress.py");
echo '</pre>';
I launched indivdually them but in all cases, Firebug returned : '<pre>'
So I tried this code founded on Stack Overflow :
$command = escapeshellcmd('chmod +x /home/folder/python/mapfile_compress_test.py');
echo $command;
$output = shell_exec($command);
echo $output;
But firebug returned nothing.
My python file begin with #!/usr/bin/env python and if I launch it on server that works !
Do you knwo how can I launch my python file from PHP file ?
chmod will return 0 on success and > 0 on error.
Make sure that the file is able to run by just executing it as the web user. When +x is properly set, you can execute it by just calling $ /path/to/your/file.py, the shebang in the first line in your script #!/usr/bin/env python should define the correct python based on your env.
You can test this by running:
$ /usr/bin/env python /path/to/your/file.py
So check your file permissions to check if the file is executable by the user that runs the php script.
Just to test, you can just print a few lines in your python file
#!/usr/bin/env python
print "test line 1"
print "test line 2"
Then if you have verified permissions and the correct use of python, you can do this in your php.
$command = escapeshellcmd('/path/to/your/file.py');
$output = shell_exec($command); // get all output or use passthrough, exec will only return the last line.
echo "<pre>{$output}</pre>;
At first, Do you have enabled shell_exec/system/passthru commands in php.ini?
shell_exec("python /home/folder/python/mapfile_compress.py");
I think, it could be problem with your $PATH. Try something like: (use full path to python)
shell_exec("/usr/bin/python /home/folder/python/mapfile_compress.py");
shell_exec("/usr/local/bin/python /home/folder/python/mapfile_compress.py");
In my case that's work if I write this code :
$command = escapeshellcmd('python /path/to/your/file.py');
exec($command);
I have a shell script in Linux that performs SFTP to get some files. It works fine when I execute it from a terminal.
I am trying to call the script from PHP. It seems to work until the echo, and then it doesn't do anything.
The script and the PHP file are in the same folder.
This is the PHP code:
<?php
$comando = "sh ftpgesdoc.sh";
$result=exec($comando);
echo $result;
?>
And this is shell script. When I execute from the web, I can see the echo "ejecutando sftp", but nothing happens after this point.
#!/bin/sh
echo "ejecutando sftp"
folder="/aaa/bbb"
file="xxx.PDF"
sftp UserXX#nnn.nn.n.nn << EOF
cd $folder
get $file test.pdf
EOF
exec returns only the last line from the command output. If you want to capture entire output, use proc_open. See this answer, for instance.
you have to give the full path to file
and use this 2>&1 and know the error
try something like this
$comando = "sh pathTofile/location/ftpgesdoc.sh";
if(exec("$comando 2>&1", $output, $return_var))
{
print_r($output);
echo "<br>";
print_r($return_var);
}
I want to run this code from php
echo <password> | sudo -S /usr/sbin/asterisk -rx "dongle show devices"
but it's not working. Can anyone help?
You can just use the 'backtick' character (`) around your shell string like:
<?php
$output = `command_goes_here`;
echo $output;
?>
Keep in mind this will only work if the shell_exec() function would work on that server, which could also be used in a similar way.
Use php function shell_exec or exec to execute shell commands
For more details
http://www.php.net/shell_exec
http://php.net/manual/en/function.exec.php
u can try by this if u want to run shell script command in php file
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
u can also try other way by create .sh file for shell script and run that .sh file by php function
$output = shell_exec('./deploy.sh');
echo "<pre>".$output."</pre>";
I've been trying to run this php code on CentOS:
<?php
$command = "diff file1 file2 > file3";
exec($command, $output, $error_code);
if ($error_code != 0) {
echo "Error: $error_code";
}
?>
And it always echoes "Error: 1". Error 1 is "Operation not permitted" http://www.pegasoft.ca/resources/boblap/99_b.html. It looks like apache has no permissions to do certain things, right? How can I fix that?
I think it's because you're not using the right command. You're running $command like in a terminal, so you need to add a command indicating that you have permission. I think that on CentOS it's su. In Ubuntu, for example, you would do sudo -command-.
So try to add su before diff.
Edit:
You should check here for proper usage of su:
http://wiki.centos.org/TipsAndTricks/BecomingRoot
Oh >____<
I had this line at the end of my script:
exec("rm -f /var/local/out/upload/example_word/word/diff.diff");
The file was created and then deleted... That's why I could never see it. Sorry.