0

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
memememe
  • 165

3 Answers3

6

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
Chris Down
  • 125,559
  • 25
  • 270
  • 266
  • What's the difference between an input and a parameter here? Aren't they both just text that whatever the implementation of the command is will simply interpret differently? – memememe Dec 15 '19 at 00:31
  • "Input" here is standard input -- the data passed on file descriptor 0 to the process. A parameter is a part of the command line arguments passed to 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
  • Hm, which of them are the ones you can access through 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
  • This answer properly addresses the core issue of the question and OP's misconception, hence +1. STDIN and positional parameters are very different things and serve very different purposes - that's what needs to be emphasized. – Sergiy Kolodyazhnyy Dec 15 '19 at 00:54
  • My misunderstanding comes from the fact that, from the point of view of bash commands, both are passed as additional strings after the command itself. If I was writing C code of a program that can be used by command line, how would I "take an input on stdin" instead of just parameters on a command line? And how does bash differentiate between those? – memememe Dec 15 '19 at 03:09
  • When you pass a file name as an argument, the code has to explicitly open the file to read the contents (and file may or may not exist). If its the standard input, the file exists and is already opened... – xenoid Dec 15 '19 at 10:34
  • @memememe you would read from stdin like it was a file. – D. Ben Knoble Dec 15 '19 at 14:53
1

Better run:

 kill $(pidof firefox)

or

pkill firefox
0

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
Kusalananda
  • 333,661