在php中使用他们的名字杀死进程

时间:2016-04-23 15:58:08

标签: php linux codeigniter

我有一个长期运行的进程的名称,如ffmpeg(或更多),我想在php中删除它 - 或者它们 - 并且不使用exec

这可能吗?它就像killall ffmpeg linux命令一样。

2 个答案:

答案 0 :(得分:2)

您可以使用posix函数" posix_kill"以安全的方式终止进程。 例如:

// We assumes that 1223 PID is one ffmpeg process

posix_kill(1223, SIGHUP);  // This send a polite termination signal to the process

posix_kill(1223, SIGINT);  // This send a interrupt signal to the process

posix_kill(1223, SIGTERM); // This send the termination signal and tried to clean-up the process (Use it if SIGHUP and SIGINT fails)

posix_kill(1223, SIGKILL); // This send an unpolite signal to the kernel explaining that we want to kill the process ASAP and unconditionally and we don't care if we loss some data and the process is going to generate some memory leak (Use this as last option).

现在问题是你想拥有ffmpeg PID,你可以以非侵入性的方式实现这一点,从" proc"文件系统。

提供进程名称的示例: / proc / 1223 / cmdline

为了获得更高/最大的进程号,您必须读取文件/ proc / sys / kernel / pid_max。现在,您必须遍历" proc"中的流程信息。文件系统,直到达到pid_max。您必须检查进程目录是否存在,并且从/ proc / xxxx / cmdline返回的字符串是否包含字符串" ffmpeg"或者" libav" (有些linux发行版已经通过libav或类似名称重命名为ffmpeg。)

请记住,为了终止ffmpeg进程,应将PHP脚本所有者(也许是Apache?)添加到启动ffmpeg进程的同一组中。

答案 1 :(得分:0)

实际上你可以通过“exec”函数使用linux命令,我不知道其他任何方式。看看这个:

http://php.net/manual/en/function.exec.php

相关问题