0

I want to be able to kill some processes. I know the process id (that is given in file) and I do not want to do it one by one. Is there any way to do it something like calling kill command recursively and giving it input through file or standard input like we do with rm command...

1
  • 1
    I don't know what you mean by "recursively", and rm does not read from standard input.
    – Kusalananda
    Commented Mar 31, 2018 at 12:25

1 Answer 1

2

Given a file, pids.txt, with one process ID per line, you may do

xargs kill <pids.txt

This will invoke the kill utility with the process IDs from the file.

Other solutions include

kill $(cat pids.txt)

which IMHO is not so elegant, and

while read pid; do
    kill "$pid"
done <pids.txt

which is a lot to write to do a simple thing.


Note that none of the above variations are able to verify that the process IDs in the text file corresponds to the same processes that the process IDs were attached to at the time of creating the file.

On Linux systems, process IDs are usually incremented by one for each new process, and when the maximum allowed process ID has been used, the further IDs are allocated from some low number. This means that process IDs, over time, are re-used.

On some systems, like OpenBSD, process IDs are randomly allocated. Here too, there will be re-use of old process IDs after some time.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .