Find the answer to your Linux question:
Results 1 to 5 of 5
I am writing a script that needs to get a pid for a process I'll use pidof() for that Then I have to kill the process and everything associated with ...
  1. #1
    Just Joined!
    Join Date
    Dec 2007
    Posts
    10

    Script to kill a process

    I am writing a script that needs to get a pid for a process
    I'll use pidof() for that
    Then I have to kill the process and everything associated with it.
    So I must do a "kill -9 pidof()" ........
    But the syntax will not work that way.

    Any suggestions on how to accomplish this?

    Thanks

    emp

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    With the bash shell, putting something in $(...) means that the part between the parentheses is to be executed as a shell command, and the output is to be placed at that point in the overall line.

    Example:
    Code:
    ls -t
    lists the names of the non-hidden files in the current directory, sorted by time of most recent modification, so the files which have been modified most recently appear at the beginning.
    Code:
    ls -t | head -1
    lists the name of the single most recently modified non-hidden file in the current directory.
    Code:
    head -5 $(ls -t | head -1)
    displays the first five lines of that file.
    Code:
    pidof fred
    will get the pid of the process(es) running a program named fred.
    Code:
    pidof -s fred
    does the same, but also includes any instance of bash which is running a script by that name. If you're trying to kill a bash script, you'll want the -s option.
    Code:
    kill -9 $(pidof -s fred)
    should do what you want.

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    You might also try "pkill".

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Quoth the highly esteemed burschik:
    You might also try "pkill".
    Absolutely correct. I had no idea that pkill even existed.

    emp1953, if you read the man page for pkill, you'll discover a collection of options available for it. Most of them probably won't interest you, but a few of them might.

    Worth checking out.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Just Joined!
    Join Date
    Dec 2007
    Posts
    10

    Thanks all

    The "-s" option was the missing part of my puzzle.

    Things are working the way they should be.

Posting Permissions

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