I have a php script that uses a shell_exec to call a shell script, I am using a ajax call to the php script,
In mac os, it does run without any issues, but when I try to run it in my ubuntu vm, it does not do the shell_exec command when called from the ajax, but if I run from the unix console using
php script.php
it does run the shell_exec normally.
Anyone has had this issue before?
The php code is this
<?php
$output = shell_exec('sudo sh /var/www/html/SIRSProject/WebApp/php/teste.sh');
echo $output;
?>
Most probably this is because when called via the web server the command will run as another user compared to when you call it directly on the command line.
Which web server are you using? If you're running apache for example you can see/change the user PHP is run as by the webs server in the User directive; under Ubuntu this www-data.
Another reason may be that sudo ask for a password which can not be provided by PHP. Note here: when running on the terminal sudo may cache the authorization for that terminal session. So maybe also test manually executing the PHP script in a new terminal session.
In both cases you may need to update your /etc/sudoers to allow for non-interactive execution of sudo.
But be aware: THIS IS DANGEROUS!
Calling an external command with sudo from a web server can easily become a huge security risk by allowing to execute commands with root privileges from the web. Only proceed with this if you really know what you are doing - especially never trust the user input!
Related
I am trying to build an web interface to execute the shell script on the linux environment.
I tried the same using the PHP shell_exec("test.sh") but it did not work properly. It is unable to execute few commands on the test.sh file and if i try executing the test.sh file it works properly though linux terminal.
<?php
shell_exec(command);
?>
I just want to execute shell script through web interface , language can be any(Php, Java, nodejs..)
Share more information for a better answer, by the way, it's probably caused by users permissions. When you run a command with shell_exec() the user running it is www-data.
Okay, this is going to be a very weird request/question.
There is a very long running PHP script that needs to be launched by the user (admin) who is not very technically adept. When running the script through apache, it throws a timeout (502 or 504 Bad Gateway).
Let's just assume that apache can't be configured to fix the timeout issues.
I want to create a button in the admin panel that sends an AJAX call to a PHP script on the server, that PHP script will act as a proxy of sorts to launch a shell command. The shell command will then execute the long running PHP script with certain arguments... but I don't want it to wait for the long running script to finish. The proxy PHP script can exit and return true/false based on if the shell command actually started (this part is optional).
Essentially, have PHP launch a shell command which launches a PHP script.
How can I pull something like this off?
Have you tried shell_exec. It worked for me...
http://php.net/manual/en/function.shell-exec.php
I'm trying to run notepad on the server (localhost for now).
exec() and system() functions are working fine when for example write ping 127.0.0.1.
But this does not work (working fine if I write the command directly in the command prompt):
$command = "C:\WINDOWS\system32\notepad.exe";
$result = system($command);
print_r($result);
Using Windows XP with xampp. Probably I don't have permissions because the command is executed from some other account but I don't know how to check this.
Any advices?
Edit:
As bwoebi said, I have opened processes but they are opened from a different user (SYSTEM) and I can't see when the application is opened. So, I have to paraphrase my question: how to change the user which is used when executing commands from a PHP script?
First you need to escape the backslashes in your command string if you're not using single quotes :
$command = "C:\\WINDOWS\\system32\\notepad.exe";
Also note that if Apache is running as a Windows service, it does not have desktop interaction permission, so it can't open a GUI, try running the script directly with PHP on the command line.
EDIT
The user used to run command is the user that is running PHP. To change the user running PHP, you'll have to change the user running Apache, if you want this user to have desktop interaction permission, you'll have to run Apache yourself and not as a service.
let the process a bit sleep after executing the shell command and search in the TaskManager for a Notepad... Then you'll see that this are two different users (and you don't see the other users Notepad)
Note pad is a GUI program so requires the windows TTY to be active.
Ping is command line so can be ran by the system directly and piped results into the program calling it.
With out getting into too much detail of how os's work basically it can't be done on a windows machine (its possible on unix machines but more difficult.)
At preset we have need to launch apps on a linux box remotely
To do this we have php script that is run at boot via the rc.local file. This php script watches a command file. This has commands written to it.
The php script has trouble running some apps. For instance it can boot X11, but it can't run an app that is meant for X11.
But, if we run the php script from a terminal, them the system works just just fine
Here is the contents of the rc.local file (this fails).
sudo -u jacob /usr/bin/php /home/listener/ListenerThread.php > /var/www/html/out.txt &
The user jacob as sudo root access with no need for passwords
Please help
Most likely if it's an X11 issue the children aren't having DISPLAY set in their environment, but without error messages we can't help you.
One solution I would suggest is to start X11 at boot and put the line that launches your script into your .xinitrc. This way your script will be able to run GUI programs correctly.
If you don't like that solution, then try running your gui apps from within the script like this: env "DISPLAY=:0.0" your_gui_app
I have to run 2 commands through exec();
the first command is a wrapper calling for (Plesk panel) subsription,
the second is also a plesk command,for dns.
Note: After i execute an add subscription, the apache WILL RESTART!,
So my Question is:
can i call the exec somehow, to execute both commands at linux side without loss of the second command?
Ex:
exec(("/wrapper2 3 --create ... && /wrapper2 4 --update-soa example.com ... ) > /dev/null 2>&1 );
Php will send both commands to linux to execute, or it will restart apache after the first command, and then i can't execute the second command?
Thanks
Um... I'm thinking bad deal. Generally it is a bad idea for a process to tell its parent to restart while the process needs to keep running. But, even if it were a good idea -- Apache is the parent process of PHP in that context (do ps -A, you'll not see PHP), I can't imagine that it would let you restart it and keep running at the same time.
I'd approach it this way: if you can bridge a delay, then have a cron job look for whether a specific file exists, if it does, then execute the two command that you need it to. At a worse-case scenario, make PHP output a file which has the two commands you want run and then have cron run that file.
Well from my understanding the issue lies in the fact that Apache is going to be the parent of the script that is running, when Apache gets shut down so will the script.
Barring that you can deal with a sort of derp-y setup, you can set up a cron job that looks for when it needs to restart the server (either a file you created via touch or something from PHP), which can handle everything outside of the context of Apache's process.
A sort-of-dirty idea. :(
Put the commands in a shell script and execute that script. It's less complicated and just in case you can call it with other tools as well like on apache restart or via cron.
I think why the apache restart is your command executes too long or cost to much system resource and makes apache sub process exits.
Try using fastcgi mode instead of mod_php.
You can make a shell file to execute two commands.