简体   繁体   English

如何以用户www-data终止进程

[英]How can I kill a process as user www-data

I start a Python file via webpage as user www-data. 我通过网页以用户www-data启动Python文件。 How can I kill the process at a later time by running a different Python script? 如何通过运行不同的Python脚本以后杀死进程?

My PHP webpage contains this code to start the process: 我的PHP网页包含此代码以启动该过程:

echo shell_exec("sudo python /home/pi/Desktop/PiControl/motion_sensor.py");

Method 1 (This answers your question on the title): 方法1(这将回答您关于标题的问题):

let www-data be a admin in your server. 让www-data成为您服务器的管理员。

adduser www-data sudo

Then remove the sudo password while executing commands 然后在执行命令时删除sudo密码

Reference: Execute sudo without Password? 参考: 没有密码执行sudo?

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: 方法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. 编写一个服务(使用PHP或Python以外的东西)并以root用户身份运行,一旦调用它,就可以杀死服务器上的所有python。

Method 3: 方法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. 一个愚蠢的方法:让你的Python在启动时创建一个空文件,将该文件权限设置为777,以便任何用户都可以删除它。 check if file exist every 5 seconds, if not exist, exit itself. 检查文件是否每5秒存在一次,如果不存在,则自行退出。

Finally, If your want to stop your python, just let php delete that file. 最后,如果你想停止你的python,只需让php删除该文件。

Method 4: 方法4:

I just noticed, www-data will run you python script as itself, why not you can't kill it? 我只是注意到,www-data将运行你自己的python脚本,为什么不能杀死它? just have a try. 试一试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM