I am playing around with pipes to learn how to use them.
I am trying this command to kill a process by name: pgrep <some_process> | kill
but the output I am getting is the usage instruction for kill, like it didn't receive any input.
of course I know there are easier way to accomplish the same task like pkill
or killall
, but I want to know why is this not working.
Thank you
kill
is not reading anything from stdin but expects some command line args and throws an error if it doesn't get them.Piping only works if the program on the right side of the pipe is actually reading from the pipe. Not all programs do.
As Florian explained,
kill
expects command line inputs.To go around the problem you can use
xargs
to build command line arguments for a command, e.g.,pgrep vivaldi | xargs kill
or better yetpgrep vivaldi | awk 'BEGIN{ORS=" "} 1' | xargs kill
kill $(pgrep vivaldi)
killall
which does pretty much the same job as pgrep and kill combined , e.g.,killall vivaldi
pkill
, another utility to kill based on name, e.g.,pkill vivaldi
For killing a program by its name, if you actually know the process' name, you could just use the command
skill <your process name>
;Now for pipes, a good way to understand them is playing around with tools like
echo
,more
,less
,tail
,head
, etc. There are many programs that can actually read from a pipe... one I have always used isgrep
.