如何使用 php 中的 pid 杀死 linux 进程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19239840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:02:13  来源:igfitidea点击:

How to kill a linux process using pid from php?

phplinuxapacheamazon-ec2

提问by kishan

I am facing one issue regarding killing a Linux process from my php code. I am running a Scrapy tool from my php code using the proc_open()function in the background.

我正面临一个关于从我的 php 代码中杀死 Linux 进程的问题。我正在使用proc_open()后台的函数从我的 php 代码运行一个 Scrapy 工具。

It works fine, but now I want to kill this process using its process id. To do that I'm using exec("sudo kill -9 $pid");where $pidis the process id which I'm getting from my php code.

它工作正常,但现在我想使用其进程 ID 终止该进程。为此,我使用从我的 php 代码中获取的进程 IDexec("sudo kill -9 $pid");在哪里$pid

The problem is this process is running on behalf of the apache user. I thought there might be some permissions issue, so I added apache user to the sudoers file like this apache ALL=(ALL) NOPASSWD:ALLbut I'm still not able to kill it. Somehow, the same kill command works from my putty console.

问题是这个进程是代表 apache 用户运行的。我认为可能存在一些权限问题,所以我像这样将 apache 用户添加到 sudoers 文件中,apache ALL=(ALL) NOPASSWD:ALL但我仍然无法杀死它。不知何故,同样的 kill 命令在我的腻子控制台上工作。

My code is on an Amazon EC2 instance.

我的代码位于 Amazon EC2 实例上。

My question is, how can I kill that process identified by a pid from php?

我的问题是,如何终止由 php 中的 pid 标识的进程?

采纳答案by Sudipta Chatterjee

Never, ever, give apache sudopermissions!

永远不要给 apachesudo权限!

Use exec("kill -9 $pid");- your apache process started it, it can kill it :)

使用exec("kill -9 $pid");- 你的 apache 进程启动了它,它可以杀死它:)

回答by linux_fanatic

You can use exec with sudo privileges for skill that is more safer check out kill users processes in linux with php

您可以使用具有 sudo 权限的 exec 来获得更安全的技能,使用 php检查linux 中的杀死用户进程

Thanks & Regards,
Alok Thaker

感谢和问候,
Alok Thaker

回答by Viacheslav aka Lordz

Try posix_kill:

尝试posix_kill

bool posix_kill ( int $pid , int $sig )

Send the signal sig to the process with the process identifier pid.

bool posix_kill ( int $pid , int $sig )

将信号 sig 发送到进程标识符为 pid 的进程。