Results 1 to 7 of 7

Thread: How to pipe different commands output from one to another?

  1. #1
    Join Date
    Apr 2007
    Beans
    891
    Distro
    Ubuntu 10.04 Lucid Lynx

    How to pipe different commands output from one to another?

    I am trying to get a particular process as I know the name and then send that PID to kill program to terminate it. I have come far enough to find the process ID but I cannot find a way to feed it to kill command. I tried the following with no luck. Any help is welcome.
    Code:
    ps -ef |grep tpsServer |awk '{print $3}'|kill -s  9


    Thanks.
    Ubuntu 10.04
    ATI 3470
    4 GB RAM.

  2. #2
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: How to pipe different commands output from one to another?

    What about pgrep and pkill?

    Code:
    pgrep -x tpsServer
    When you find the process you want with pgrep, then you can use the same parameters with pkill.

    Also, in the pipe above shouldn't the PID be collected from $2 and not $3 in awk?

  3. #3
    Join Date
    Apr 2011
    Location
    Maryland
    Beans
    1,461
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: How to pipe different commands output from one to another?

    If you know the name of the application you want to kill, and there's only one process associate with it, you can skill finding the PID with pgrep and just issue:

    Code:
    pkill tpsServer
    If there's more than one process affiliated with it, I think you can also just run:

    Code:
    killall tpsServer
    That should kill all PIDs associate with it.

  4. #4
    Join Date
    Jul 2007
    Location
    Mississauga, ON
    Beans
    123
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: How to pipe different commands output from one to another?

    Quote Originally Posted by legolas_w View Post
    I am trying to get a particular process as I know the name and then send that PID to kill program to terminate it. I have come far enough to find the process ID but I cannot find a way to feed it to kill command. I tried the following with no luck. Any help is welcome.
    Code:
    ps -ef |grep tpsServer |awk '{print $3}'|kill -s  9


    Thanks.
    pkill or killall will do the job. Having said that, to follow your approach you should have tried:
    kill -9 `ps -ef|grep tpsServer|awk '{print $2}'`

    Note I changed the awk string to $2. In RedHat (at work), ps -ef shows the PID in the second column.

    Also note that you will get a warning because you will be trying to kill the grep process as it also shows the process name. To prevent that, you should filter it out:
    kill -9 `ps -ef|grep tpsServer|grep -v grep|awk '{print $2}'`
    Windows by necessity, Linux by choice
    Lubuntu and loving it!

  5. #5
    Join Date
    Apr 2007
    Beans
    891
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: How to pipe different commands output from one to another?

    Thank you all, solved. I used aromo's suggestion though other's work as well but in my case I have the partial name of the process and not all of it.
    Ubuntu 10.04
    ATI 3470
    4 GB RAM.

  6. #6
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: How to pipe different commands output from one to another?

    Quote Originally Posted by legolas_w View Post
    in my case I have the partial name of the process and not all of it.
    pkill (and pgrep) will work like that. They match partial names. They only match the exact name if you use -x.

    Using SIGKILL aka -9 in the kill won't give the process a chance to shut down gracefully, if it was going to. The default for kill and pkill is SIGTERM and that is generally better to use on the first try.

  7. #7
    Join Date
    Nov 2006
    Location
    Belgium
    Beans
    3,025
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: How to pipe different commands output from one to another?

    Code:
    kill $(pidof tpsServer)
    shoud work too. It's conceptually the same as the solution with the backticks, but it saves you the grep and awk trouble (and the apparent variation in which field to select)
    Also, backticks are deprecated, $() is the currently preferred form.

    for the more general case : if a pipe doesn't seem to work because one of the commands doesn't take the input but accepts arguments, xargs probably helps,
    like so

    Code:
    ps -ef |grep tpsServer |awk '{print $3}'|xargs kill 9

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •