Trouble Using Posix_Kill in PHP

posix_kill not working when run from browser

The error message has nothing to do with your problem. It's just the browser asking for your site's icon, which you haven't configured.

Now, about the actual issue: the apache user likely doesn't have permissions to kill your process. If the process was started under a different user account, you can't kill it from within the web server. Imagine a multiuser system where any local user could log in and kill everyone else's processes - that isn't the Unix permissions model...

You might want to use a limited sudo with exec, or otherwise assume permissions before sending the signal. Or you could have the process listen for the web server to do something, and then kill itself. Or, better yet, don't use a web-serving language like PHP for systems-administration type tasks: use cron or a more appropriate tool.

Using PHP's posix_kill() in linux causing strange behaviour

It turns out that this was one of those bugs that makes you feel stupid! The launcher program was accidentally killing it's process group...

Kill processes using their name in php

You can kill process in safe way using the posix function "posix_kill".
Example:

// 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).

Now the problem is that you want to have the ffmpeg PID's, you can achieve that in a non-intrusive way reading the processes information from the "proc" filesystem.

Example that provides the process name:
/proc/1223/cmdline

In order to get the higher/maximum process number, you have to read the file /proc/sys/kernel/pid_max. Now you have to iterate across the process information in the "proc" filesystems until the pid_max is reached. You have have to check that process directory exists and that string returned from the /proc/xxxx/cmdline is contain the string "ffmpeg" or "libav" (Some linux distributions have renamed ffmpeg by libav or something like that).

Remember that in order to kill the ffmpeg processes the PHP script owner (Maybe Apache?) should be added to the same group that launch the ffmpeg processes.

how to check whether process ended or not with php?

If you know the PID of the process (which is a good idea - have it write its pid into a file), then you can check if it is alive with

posix_kill(PID,0)

Which returns TRUE only if the process is alive.

However, this is a bit bogus, as it might return TRUE if some other process has appeared with the same PID. PID reuse is quite likely, especially if the OS was rebooted since the process started.

A better way is to have the process lock a file (with e.g. flock) and then you can see if the lock is still present (by trying to lock the file yourself with FLOCK_NB)

Kill processes using their name in php

You can kill process in safe way using the posix function "posix_kill".
Example:

// 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).

Now the problem is that you want to have the ffmpeg PID's, you can achieve that in a non-intrusive way reading the processes information from the "proc" filesystem.

Example that provides the process name:
/proc/1223/cmdline

In order to get the higher/maximum process number, you have to read the file /proc/sys/kernel/pid_max. Now you have to iterate across the process information in the "proc" filesystems until the pid_max is reached. You have have to check that process directory exists and that string returned from the /proc/xxxx/cmdline is contain the string "ffmpeg" or "libav" (Some linux distributions have renamed ffmpeg by libav or something like that).

Remember that in order to kill the ffmpeg processes the PHP script owner (Maybe Apache?) should be added to the same group that launch the ffmpeg processes.

How to kill a linux process using pid from php?

Never, ever, give apache sudo permissions!

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



Related Topics



Leave a reply



Submit