PHP shell_exec which mysqld refuses to work - php

I attempt to run the command echo shell_exec('which mysqld'); in PHP which yields nothing.
I manually go to the terminal and run $ which mysqld which yields the answer.
I run the command echo shell_exec('whoami'); in PHP which gives me user.
I go back to the terminal and run $ which mysqld as the user PHP is running as and get the answer.
How do I force PHP to return echo shell_exec('which mysqld'); whether it "feels" like it or not?
Yes, the database is running.

It sounds like php doesn't know what the executable is, it's possible it doesnt know what the command is (it might not be in your $PATH), or it's a shell builtin, you may be better off running a command with bash -c "which mysql", or something of that nature, to force your script to run it in the context of an actual shell session.

Related

php shell_exec return null

I want run php ../cat1/index.php gr_s2/3/gr-n40 1200 command by php.
The shell_exec return NULL as result but when I try that command on cmd, output was shown correctly.
What happen in php and shell_exec ?!
Note: The command with different parameters (Like: php ../cat1/index.php gr_s2/3/gr-n40 800) works correctly in both (php and cmd).
There is a note in php manual page of shell_execute:
Note: This function can return NULL both when an error occurs or the
program produces no output. It is not possible to detect execution
failures using this function. exec() should be used when access to the
program exit code is required.
Source: http://php.net/manual/en/function.shell-exec.php
So your code running with error. Try with exec.
If you want, insert your code (or blocks) to be checked.
This is fixed by adding user used by web-server to sudoer and run the php command with
sudo php ..........
To start run this command sudo visudo
Add the following line at the end of sudoer file opened
www-data ALL=NOPASSWD: /usr/bin/php
But if you whant to execute all commad from php add this line instead of the above www-data ALL=NOPASSWD: ALL which is not recommended
and the run your commands with sudo php /path/to/you/file.php
For my case I was running ubuntu 14.04
Have fun please

php sudo shell_exec command runs from command line, but not from browser

I am running lighttpd with php-fpm. I have added the http user to my sudoers, as a nopasswd sudoer. I have the following script:
<?php
echo shell_exec("sudo sh -c 'echo \"wtf3\" > /etc/openvpn/auth.txt'");
?>
It runs fine when I do:
sudo -u http php testsudo.php
but fails when run through the browser.
shell_exec("whoami");
confirms that I am user http when in browser.
What gives?
Would it be out of the question to make a shell script that would run your command?
#!/bin/sh
sudo whoami
in the php:
$retVal = `/path/to/custom/script/myscript.sh &2>1`;
if ($retVal) echo 'SWEET';
else echo 'POOP: ' . $retVal;
{command} inside `` means the same as shell_exec('{command}') on a linux server.
If I'm not way off base here your goal is to add users to your VPN (among other things perhaps) in which case you could pass command-line parameters for username/password/etc to your custom shell script.
It's not exactly a pure shell_exec() answer to your question, but maybe you can run myscript.sh without doing sudo. Just basing this on the fact you said everything you tried worked in shell and path was set in echo $PATH, but not sudo echo $PATH in your PHP script. Seems for some reason that sudo is an issue in php, but not in shell so...let's move the sudo back to shell and try it that way.

exec/system() - script being called works until called from PHP

I have a bash script:
run.sh
#!/bin/sh
cd /var/www/project/bin/
CMD="./executable <full_path_to_file>;
$CMD
When I run this program from the terminal. (i.e. ./run.sh, it works fine)
However when I call it from PHP:
system("full_path_to_sh_file", $out);
It calls the script successfully, and even runs the executable, but now the executable throws an error saying that the supplied file can't be found.
Any ideas?
How do you run PHP script, from webserver or command line?
If from webserver, what user is it run (httpd or apache?)
Make sure the environment as same as when you are running from terminal (for example: same user)
Try this if running as different user:
sudo -u apache /fullpath/run.sh

Executing a bash file from a php page with root-only commands (Ubuntu)

I need to execute a bash file from a php page, with exec() function. The problem is that in this bash file, there's the command "adduser" ... Witch is a sudo command. I had the idea of modifying the sudoers so the user that run the script would have access to it, but who is this user ? I know apache2 is executated with www-data user...
Thanks!
You can find out which user PHP is running as by using system to run the command 'whoami' and display the output.
system('whoami');
That seems like a rather bad plan, giving the www-user sudo access. But yes, its www-data (by default, depending on linux flavor) that apache runs under.

Bash script to run php script

I have a php script that I want to be run using a bash script, so I can use Cron to run the php script every minute or so.
As far as I'm aware I need to create the bash script to handle the php script which will then allow me to use the Cron tool/timer.
So far I was told I need to put:
#!/pathtoscript/testphp.php
at the start of my php script. Im not sure what to do from here...
Any advice? Thanks.
If you have PHP installed as a command line tool (try issuing php to the terminal and see if it works), your shebang (#!) line needs to look like this:
#!/usr/bin/php
Put that at the top of your script, make it executable (chmod +x myscript.php), and make a Cron job to execute that script (same way you'd execute a bash script).
You can also use php myscript.php.
Sometimes PHP is placed in non standard location so it's probably better first locate it and then try to execute.
#!/usr/bin/env bash
PHP=`which php`
$PHP /path/to/php/file.php
A previous poster said..
If you have PHP installed as a command line tool… your shebang (#!) line needs to look like this: #!/usr/bin/php
While this could be true… just because you can type in php does NOT necessarily mean that's where php is going to be... /usr/bin/php is A common location… but as with any shebang… it needs to be tailored to YOUR env.
a quick way to find out WHERE YOUR particular executable is located on your $PATH, try..
➜which -a php ENTER, which for me looks like..
php is /usr/local/php5/bin/php
php is /usr/bin/php
php is /usr/local/bin/php
php is /Library/WebServer/CGI-Executables/php
The first one is the default i'd get if I just typed in php at a command prompt… but I can use any of them in a shebang, or directly… You can also combine the executable name with env, as is often seen, but I don't really know much about / trust that. XOXO.
You just need to set :
/usr/bin/php path_to_your_php_file
in your crontab.
I'm pretty sure something like this is what you are looking for:
#!/bin/sh
php /pathToScript/script.php
Save that with your desired script name (such as runPHP.sh) and give it execution rights, then you can use it however you want.
Edit: You might as well not use a bash script at all and just add the "php ..." command to the crontab, if I'm not mistaken.
Good luck!
The bash script should be something like this:
#!/bin/bash
/usr/bin/php /path/to/php/file.php
You need the php executable (usually found in /usr/bin) and the path of the php script to be ran. Now you only have to put this bash script on crontab and you're done!
a quick way to find out WHERE YOUR particular executable is located on your $PATH, try.
Even quicker way to find out where php is ...
whereis php
I'm running debian and above command showing me
php: /usr/bin/php /usr/share/php /usr/share/man/man1/php.1.gz
Hope that helps.
If you don't do anything in your bash script than run the php one, you could simply run the php script from cron with a command like /usr/bin/php /path/to/your/file.php.
I found php-cgi on my server. And its on environment path so I was able to run from anywhere. I executed succesfuly file.php in my bash script.
#!/bin/bash
php-cgi ../path/file.php
And the script returned this after php script was executed:
X-Powered-By: PHP/7.1.1
Content-type: text/html; charset=UTF-8
done!
By the way, check first if it works by checking the version issuing the command php-cgi -v
Create file.php with first line in files: file.php(#!/bin/php) file.sh(#!/bin/bash).
Check installed php.Run command in terminal:
which php
If set there will be an answer:
/usr/bin/php
Run file.php with command:
php file.php
if the file has started then you can write this command to file.sh:
#!/bin/bash
run_php=`php file.php`
echo $run_php
Be careful ' and ` different!!!

Categories