0
votes

I have a working shell script using killall to kill all instances of a program like below:

killall abc

Now, I write a php webpage to execute this script using shell_exec function:

shell_exec('sh ./myscript.sh');

Problem is that my php code works correct on commandline with "php myscript.php", but not works in browsers!. However, I know that the user in commandline is "root" and in php is "apache" (I get this with 'whoami'). The linux distribution is Centos 6 which uses SElinux. I changed the status of selinux to permissive.

Things I've checked:

  • PHP safe_mode is off
  • shell_exec() is not present in disable_functions in php.ini

Is there a way to run scripts with kill command using php?

Thank you for your help.

1
Any PHP error (check error_reporting and display_errors)? what is the exit code of executed command (use exec() to findout)? do you have permission to kill the target process? - Ariyan
no exit code and no errors are reported! exec () is also has a similar output, yea, I have permission to kill, but "apache" does not! - sajebr
there is always an exit code! on success it is 0 and non-zero is error code!when you don't have permission you can't ! when you run it from apache you are apache! - Ariyan
yea, but the question is how to do this work with apache?! - sajebr

1 Answers

0
votes

you either have to run apache as root (insecure) or, which would be much safer, you have to run the commands you try to kill as 'apache', or you configure your sudoers file to grant apache rights to killall command:

# vim /etc/sudoers
apache localhost=(ALL) NOPASSWD:/usr/bin/killall

and then change the myscript.sh to do sudo killall abc