I normally use pidof
to get the pid of a process, and KILL -SIGTERM <pid>
to terminate it.
The pipe should give the output of one command as an input to another.
So why doesn't the following command work?
pidof firefox | kill -SIGTERM
I normally use pidof
to get the pid of a process, and KILL -SIGTERM <pid>
to terminate it.
The pipe should give the output of one command as an input to another.
So why doesn't the following command work?
pidof firefox | kill -SIGTERM
The pipe should give the output of one command as an input to another.
That's correct, but kill
doesn't take any input on standard input. Instead you need to provide it as a command line argument:
kill -SIGTERM "$(pidof firefox)"
or:
pidof firefox | xargs kill
$(
is command expansion inside the shell, whereas xargs
is external.
However, these approaches have a number of corner cases, like what to do if there are multiple pids, no pids, etc -- this is why pkill exists:
pkill -TERM firefox
execve
. You can think of a pipe as being something like a file that can't be seeked, but command line arguments are simply metadata related to the process in question.
– Chris Down
Dec 15 '19 at 00:34
argv
in a C program?
– memememe
Dec 15 '19 at 00:36
argv
is arguments (argv
means "argument vector"). In C, stdin is in the form of a file, so you use functions like read
to access it instead.
– Chris Down
Dec 15 '19 at 00:48
Better run:
kill $(pidof firefox)
or
pkill firefox
Just for diversity's sake: How about running:
kill -SIGTERM "$(pgrep -f firefox)"
Unsure about case-insensitivity:
kill -SIGTERM "$(pgrep -fi FirEfox)"
Multiple process running with same name, and want to kill the first one:
kill -SIGTERM "$(pgrep -f firefox | head -1)"
Info about my pgrep
$ pgrep -V
pgrep (proctools 0.4pre1) http://proctools.sourceforge.net