2

I'm very new to Linux. And as a matter of fact I use Cygwin now and not Linux itself.

I'm trying to install RVM (Ruby Version Manager).

I was doing rm -r ./.rvm -i command. I wanted to remove .rvm folder and see how I'd been asked to delete or not a file/directory. I saw a few and stopped it with Ctrl+Z and then removed the whole .rvm folder with Windows Explored, just sent it to the recycle bin.

But when I wanted to exit the terminal with exit command I got a message that I had stopped jobs:

$ jobs
[1]+  Stopped                 rm -r ./.rvm -i

Here I read how to kill the stopped job:

kill `jobs -p`

But I decided to try it with pipe command syntax I read about here:

The output of each command in the pipeline is connected via a pipe to the input of the next command

So I did these attempts but couldn't get the desired result:

$ jobs -p | kill
$ jobs -p | kill -n 15 # 15 is SIGTERM signal
$ jobs -p | kill -n=15 # got error wrong signal spec
$ jobs -p | kill -s SIGTERM

And I don't understand why it doesn't work. jobs -p lists process IDs:

$ jobs -p
340

So that 340 ID has to go to the kill -n 15 command and it should kill the job. But it doesn't happen. Why? Is it possible to use pipe in this case?

Green
  • 23

4 Answers4

4
  • Commands have several standard streams: Standard Out (stdout), Standard In (stdin), and Standard Error (stderr).
  • Pipes send a command's stdout to another command's stdin
  • Kill takes its paramters through its argument list, not any of the "standard" streams
  • Some commands allow you to specify "-" on the argument to accept input from stdin as arguments (not the kill command)
  • If you want to pipe stdout to another's argument list, you can use xargs, but care must be taken when there are potential spaces-- and sometimes in other cases).

In your case, jobs -p shouldn't have any issues with xargs and you can use the following:

jobs -p | xargs kill

If you like, you can also see the exact commands it executes by using --verbose (or -t)

jobs -p | xargs --verbose kill
jobs -p | xargs -t kill

xargs is a powerful tool. Be very careful passing input to it (find -print0, grep -Z, and xargs -0 get along well when working with files). Its well worth the effort to utilize it (and sometimes, time saved).

Reece45
  • 719
3

kill doesn't take arguments from standard input, which is where they will appear if you pipe them as you have done. The only way to use kill is to specify a PID, or list of PIDs, as command-line arguments.

Flup
  • 8,398
0

If SIGTERM (-15) does not work, then try SIGKILL (-9). It is recommended to try SIGTERM first though.

 kill `jobs -p`
 kill -9 `jobs -p`
Daniel t.
  • 9,619
0

In this case, you can just type: fg, then hit ctrl-C.

In case you want to directly kill your remaining process, under bash, you can easily issue a:

kill %1

Which will kill the first process in background, as identified by jobs.