I have a php script where some system commands are running fine and others are not. The commands that are not running can be copy and pasted to the shell and be ran just fine.
System: OSX 10.9.2 (everything is updated).
I have tried many different commands like the following.
backticks, exec(), shell_exec(), system(), passthru()
This command works fine.
exec("drush si -y --db-url=mysql://user:pass#localhost:3306/dbname");
But these commands do not run.
exec("drush sql-sync #remote.staging #dev.anme -y");
exec("git ls-remote --heads git#github.com:blablaname/name.git");
The commands that do not run can be copy and pasted into the shell and run great. I have made sure the script is being ran in the proper directory using the getcwd() function.
If you call php program having exec() from web browser,It executes as www user. So www user may not have privilege to connect/sync to remote host.That's why it works on localhost and failing on remote host.
So one solution is
1)save the command as bash script
2)set uid bit(It can be root or user having sufficient privilege).
3)execute that bash script by exec so that it will run as previlged user.
4)You should ip restrict your program since setuid is dangerous.
setuid
Related
I have a php script that works perfectly when ran form command line; including the exec().
The exec I do are calling some .sh scripts.
These .sh script are of type bash
So I call them:
exec('/bin/bash /home/user/soft/bin/mybash.sh > /dev/null');
It works great when I run my php from command line I see all the instructions; works also when use with an alias. but when I it via a daemon:
sudo -u myuser php -f /home/user/bin/serverwait.php eveything that should be done in the .sh are not being done. but the rest of the file is doing what it is suppose to do (it checks for open ports and write to a log file)
I tried looking at my php.ini for cli and all seems right.
Im trying to run a .sh script that contains "s3cmd sync.." command and it is not working.
When running the same sh from shell prompt it works perfect.
It seems that the execute permissions for s3cmd are insufficient when running from php system / shell_exec commands.
note : ( when running another .sh script from system() function that uses mysqladmin util , it works fine.)
Any ideas?
Thanks
When I use the PHP exec() command on a mounted shared folder I do not get an output. When I run the same command via the command line it works fine. Example:
My W:\ drive is a shared drive. When I am in CMD.exe and I run this command it works:
dir W:\
When I run this command in PHP I do not get an output:
shell_exec("dir W:\")
I don't think its a permissions issue; the user manually running the command on CMD.exe is the same user that PHP is using to run the script. I verified this by running shell_exec("whoami").
Please note, when I run shell_exec("C:\") it works fine, its simply not working on shared drives.
on my local machine I execute the following:
exec('sudo nohup '.PHP_BINDIR.'/php '.ROOT.'/index.php controller=imports action=import id=12 > /dev/null 2>&1 &');
where "ROOT" is a constant with the complete path.
Everything works fine and the script is executed asyncronously with no problems.
When I run the same script on production environment (a bitnami LAMP stack running on AWS) instead: if I run it directly from the terminal it works, when I run it from php it doesn't.
I have nothing in the apache error_log and if I run it with shell_exec the returned value is NULL.
If I try to execute a different script from php (like exec('whoami')) it works, so I don't think is a permissions thing.
I also tried replacing php command with php-cli, nothing changes.
Hope someone can help.
Thanks in advance.
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