You are not logged in.

#1 2010-01-09 20:14:35

hatten
Arch Linux f@h Team Member
From: Sweden, Borlange
Registered: 2009-02-23
Posts: 736

Why doesn't this pipe work?

I recently found out how to pipe between programs, and I find it wonderful. But some combinations doesn't seem to work.
I have a file with a process id of a program. If i cat the file, and manually type kill XXXX it works. but

$ cat Tmp/alarm | kill
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

doesn't work.

Why? I'm probably missing something trivial, but my googling skills didn't help me.

Offline

#2 2010-01-09 20:20:04

wuischke
Member
From: Suisse Romande
Registered: 2007-01-06
Posts: 630

Re: Why doesn't this pipe work?

Hi,

cat Tmp/alarm | xargs kill

should do the job.

Your pipe will send the process id to stdin of kill (i.e. it's as if you type a text while the program is running) instead of an argument as you wanted. xargs will read from stdin and use the input as arguments for kill.

Offline

#3 2010-01-09 20:23:16

Profjim
Member
From: NYC
Registered: 2008-03-24
Posts: 658

Re: Why doesn't this pipe work?

Doing this:

command1 | command2

passes the stdout of command1 to the stdin of command2. Some commands2 like to get stdin, they know what to do with it. Other command2's need a bit of  tweaking, you might have to say "command2 -" to mean "hey command2, operate on stdin instead of on a file." But other command2s, like kill for instance, don't know or care about their stdin. They want arguments. So in this case you want to supply the output of command1 as an argument to kill. The way to do that is:

kill $(cat Tmp/alarm)

In fact, often the cat isn't even necessary and you can do this:

kill $(< Tmp/alarm)

But that's probably more complicated than you need right now. I highly recommend digging into a Bash tutorial or four.

EDIT: The technique I just described assumes you're only supplying one pid to kill. The technique wuischke describes would let you kill one or more pids.

Last edited by Profjim (2010-01-09 20:25:09)

Offline

#4 2010-01-09 20:29:19

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: Why doesn't this pipe work?

Useless cat!

xargs kill <Tmp/alarm

Some commands provide a flag to read arguments from stdin instead of from the command line.  AFAIK kill does not allow this.

Edit:  Whoops, too slow...

Last edited by Trent (2010-01-09 20:30:05)

Offline

#5 2010-01-10 12:50:11

hatten
Arch Linux f@h Team Member
From: Sweden, Borlange
Registered: 2009-02-23
Posts: 736

Re: Why doesn't this pipe work?

Thanks a lot, will look into a bash tutorial some day.

[going mad]too many options!!!![/going mad]

Offline

Board footer

Powered by FluxBB