Users are only allowed to kill or otherwise signal their own processes.
You say that the process is started by a daemon running as root. Does that process need to run as root as well? If it doesn't, make it drop privileges and run as the desired user.
If the process must run as root, you'll have to provide a way for the agent to elevate its privileges to kill the process. This can take the form of a setuid root helper executable, or an entry in the sudoers
file (with the NOPASSWD
tag). The sudo entry has the advantage that its use will be logged.
Note that there is no atomic way to send a signal to a process. Sending a signal is asynchronous: it is possible that you obtain the PID of a process, then you send a signal to that process, but the process has died in the meantime and its process ID has already been reused by another process.
There's a way to avoid this involving the parent of the process you want to kill, but it's complicated. The process ID will not disappear until its parent acknowledges the child's death (a zombie process remains until then). To use this effectively, you need the parent to know that it must hold on until no agent it going to want tot kill the child.
If you're not concerned with the race condition, you can give the agent the permission to run pkill name_of_process_to_kill
, if you know that there will be a single process with that name. If you can identify the process by a file it has open, you can use fuser -k /path/to/file
.
If you can, modify the process so that it listens to a termination request on a pipe or socket. Set up the permissions or authentication on the pipe or socket according to your needs.
/etc/sudoers
? – enzotib Sep 22 '11 at 17:03sudo
so that it does not ask for a password. You tell it that such-and-such a user can run thus-and-so program with root privileges without a password. That is what theNOPASSWD
bit does in the question Michael pointed you to. Once you set it up correctly, your script can saysudo kill $somepid
, and it will do what you're asking. – Warren Young Sep 22 '11 at 18:37