I want to kill other application similar to killall
command as I know application but can not get PID
at run time.
This has to be done in C code. I know kill()
but it need PID
.
Any other way equivalent to killall ?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityI want to kill other application similar to killall
command as I know application but can not get PID
at run time.
This has to be done in C code. I know kill()
but it need PID
.
Any other way equivalent to killall ?
kill(2)
is the system call to send a signal to a process there isn't an equivilent to the killall
utility.
An easy way to do this in C would be to invoke killall
from your C program, using the system(3) library function or possible popen(3).
Alternatively you could read the manual page about the /proc
pseudo file system and search for the command names and find the pids yourself.
man 2 kill
man 3 system
man 3 popen
man 5 proc
man
before asking questions about UNIX commands and 2) there is a syscall
for almost every base command.
– ajeh
Apr 26 '18 at 14:43
To answer the question in the title, no there is no C library that does this matching. Not even libprocps does this.
killall is a simple program that:
Due to it being generic (e.g. it doesn't know what match criteria a user will use beforehand) it has lots of matches. You should know what you want to match already. Your question doesn't really say but it sounds like the name or the command line.
I really caution this entire approach. Processes should be really sure about what other processes they are touching. Name is a terrible match as I can trivially fake that. Also consider you may have two users or two systems using the same name, which process should be killed?
PID files or some other method that records the PID on program commencement is much better because you know exactly which process you are talking about (unless they fork)
As Richard points out above, the killall code is GPL2+ so you can reuse it with the same license. The project moved to gitlab though and is at https://gitlab.com/psmisc/psmisc
killall
: sourceforge.net/p/psmisc/code/ci/master/tree/src/killall.c#l432. Getting the PID by name is non-trivial. Since it is FLOSS, you may re-use the respective code in compliance with GPLv2. – Richard Neumann Apr 26 '18 at 11:06