如何使用 php 中的 pid 杀死 linux 进程? [英] How to kill a linux process using pid from php?

查看:38
本文介绍了如何使用 php 中的 pid 杀死 linux 进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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 $pid is the process id which I'm getting from my php code.

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

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:ALL but I'm still not able to kill it. Somehow, the same kill command works from my putty console.

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

My code is on an Amazon EC2 instance.

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

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

推荐答案

永远,永远,给 apache sudo 权限!

Never, ever, give apache sudo permissions!

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

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

这篇关于如何使用 php 中的 pid 杀死 linux 进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆