2

I have the code and everything which is:

     pkill python

However I wanted to run it from a php script like this:

  echo shell_exec("pkill python");

I get an output which says:

bash: pkill: (1503) - Operation not permitted

I know what the problem is, which is that pkill is su command. Anyway to change this so that the php script can run it?

  • You'll find a very nice answer on the [ask ubuntu forums](http://askubuntu.com/questions/155791/how-do-i-sudo-a-command-in-a-script-without-being-asked-for-a-password) – np87 Jan 13 '14 at 16:21
  • You should give the Apache user more rights, so it can execute the command. – Niels Jan 13 '14 at 16:21
  • Maybe this link will help? http://stackoverflow.com/questions/3166123/how-to-call-shell-script-from-php-that-requires-sudo – Albzi Jan 13 '14 at 16:21

1 Answers1

6

The problem is, that the process you want to kill does not belong to the apache user (apache usually runs as www-data with group www-data). If you give apache more rights (say run it as user root), your PHP script would run with more rights and could do things like this. But this would be dangerous, because if there is a security flaw in apache or your php script, a malicious attacker could take over your system.

Instead I suggest using the setuid bit.

  1. Create a file kill.sh with the content pkill python
  2. Make it executable (chmod a+x kill.sh)
  3. Make it belong to root (chown root:root kill.sh)
  4. Make it setuid (chmod u+s kill.sh)
  5. Invoke this script from your php script
iblue
  • 29,609
  • 19
  • 89
  • 128
  • hey i tried doing this (and many other suggestions online) for the past 6 hours and still this doesnt work. any idea? same "pkill: killing pid 25782 failed: Operation not permitted" – Joel Jul 09 '20 at 19:37