1

I start a Python file via webpage as user www-data. How can I kill the process at a later time by running a different Python script?

My PHP webpage contains this code to start the process:

echo shell_exec("sudo python /home/pi/Desktop/PiControl/motion_sensor.py");
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Rilcon42
  • 9,584
  • 18
  • 83
  • 167

1 Answers1

3

Method 1 (This answers your question on the title):

let www-data be a admin in your server.

adduser www-data sudo

Then remove the sudo password while executing commands

Reference: Execute sudo without Password?

sudo visudo

In the bottom of the file, type the follow:

www-data ALL=(ALL) NOPASSWD: ALL

Note: there is a VERY high security risk, hackers may shutdown you server, do anything you can imagine to your server! Do this at your own risk

Method 2:

Write a service(with something other than PHP or Python) and run as root/admin user, once it's called, kill all python on your server.

Method 3:

A stupid method: let your Python create a empty file some where on start up, set that file permission to 777 so that any user can delete it. check if file exist every 5 seconds, if not exist, exit itself.

Finally, If your want to stop your python, just let php delete that file.

Method 4:

I just noticed, www-data will run you python script as itself, why not you can't kill it? just have a try.

Community
  • 1
  • 1
Shiji.J
  • 1,561
  • 2
  • 17
  • 31