Results 1 to 3 of 3

Thread: script to search for process and pause it

  1. #1
    Join Date
    May 2012
    Beans
    12

    script to search for process and pause it

    hi guys,

    i am looking to create a script which looks for starting kvm/qemu processes (which are virtual machines), and immediately to pause that process.

    ps aux | grep kvm

    is able to produce a list of the running processes of kvm and their pids, and i can use:

    kill -s CONT <pid> to pause the process.

    however i need to extract the pids from the ps aux command output. i'm not experienced with scripting in any language, and am unsure how i would do that. any help please, or point me in the direction of a tutorial which can help?

  2. #2
    Join Date
    Jun 2009
    Location
    Earth
    Beans
    57
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: script to search for process and pause it

    I think the glue you're looking for is 'awk'. For example:


    Code:
    bick-ubtu3:~$ ps aux | grep sleep 
    root     12057  0.0  0.0   4300   352 ?        SN   07:35   0:00 sleep 1744
    root     14411  0.0  0.0  11368   608 ?        SN   07:57   0:00 sleep 10
    fbicknel 14417  0.0  0.0  13596   924 pts/3    S+   07:57   0:00 grep --color=auto sleep
    bick-ubtu3:~$ ps aux | grep sleep  | awk '{print $2}'
    12057
    14424
    14437
    bick-ubtu3:~$
    You can refine this a bit by using a r.e. in your grep pattern:
    Code:
    bick-ubtu3:~$ ps aux | grep [s]leep
    root     12057  0.0  0.0   4300   352 ?        SN   07:35   0:00 sleep 1744
    root     14628  0.0  0.0  11364   600 ?        SN   07:59   0:00 sleep 10
    bick-ubtu3:~$ ps aux | grep [s]leep  | awk '{print $2}'
    12057
    14628
    bick-ubtu3:~$
    That removes the 'grep sleep' process so you're only left with the real sleep processes.
    - Frank

  3. #3
    Join Date
    May 2012
    Beans
    12

    Re: script to search for process and pause it

    that looks like exactly what i was looking for. i really need to learn some more about linux commands. thanks!

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
  •