Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, In a script I have written, I have extracted a couple of processes by using (ps -ef) and (grep) and placed them in a file called (kill_PID). Then after ...
  1. #1
    Just Joined!
    Join Date
    Sep 2008
    Location
    Washington D.C, USA
    Posts
    3

    kill < file (Feeding kill from file), PID, <, awk, grep

    Hi, In a script I have written, I have extracted a couple of processes by using (ps -ef) and (grep) and placed them in a file called (kill_PID). Then after using (awk) I am left with 2 PID's that I need to feed to (kill) command. Here is the scenario simplified:

    File called (kill_PID) has 1 column with 2 values, lets give them the following values:

    9645
    9646

    What do I need to add to my script in order to extract these to values from file (kill_PID) and feed them into kill command in order to kill both PID's.

    I tried

    kill < kill_PID

    But this did not work. Feedback is appreciated.

    Thanks
    Last edited by jaffd; 04-09-2010 at 03:39 AM. Reason: edit title

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    When you extract the PID's to kill, add "kill " before the PID in "kill_PID". Then you just need to source it, as in ". kill_PID"

    Your kill_PID file would look like this:

    kill 9645
    kill 9646
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Sourcing this is kind of a bad idea. Sourcing is more designed for importing variable definitions into the current session.

    I also disagree with adding kill to the file.

    If you have a list of PIDs and want to kill each of them, things are actually fairly simple:
    Code:
    #!/bin/bash
    
    while readline pid; do
        kill "$pid"
    done
    You would call this script as:
    Code:
    ./kill_script < kill_PID
    This script reads each line of its input and then runs kill with that PID.

    Do you understand how this script works?
    DISTRO=Arch
    Registered Linux User #388732

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Quote Originally Posted by Cabhan View Post
    Sourcing this is kind of a bad idea. Sourcing is more designed for importing variable definitions into the current session.

    I also disagree with adding kill to the file.

    If you have a list of PIDs and want to kill each of them, things are actually fairly simple:
    Code:
    #!/bin/bash
    
    while readline pid; do
        kill "$pid"
    done
    You would call this script as:
    Code:
    ./kill_script < kill_PID
    This script reads each line of its input and then runs kill with that PID.

    Do you understand how this script works?
    I would agree that yours is the better approach. It does the same thing, as well as having the ability to have input piped from the output of another program.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

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