LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   shell shell to execute commands for pgrep (http://www.linuxquestions.org/questions/programming-9/shell-shell-to-execute-commands-for-pgrep-4175425563/)

linux833 09-04-2012 07:40 AM

shell shell to execute commands for pgrep
 
Hello All,
could any one help me to get shell script to execute action.

i have application that take %100 cpu, each time i run it i need to
get the PID and use cpulimit.
this application run pid base on how many instance(s).

i need script to run the pgrep and execute the cpulimit commands:

1- get the pid
pgrep xapp
22764
22978

2- execute cpulimit for boxh pid:
sudo cpulimit -p 22764 -l 40 && sudo cpulimit -p 22978 -l 40

any help will be appreciated.

linux833.

pan64 09-04-2012 08:21 AM

what about xargs, something like this:
pgrep app | xargs sudo cpulimit -l 40 -p '{}'
(not tested)

linux833 09-04-2012 11:12 AM

Quote:

Originally Posted by pan64 (Post 4772287)
what about xargs, something like this:
pgrep app | xargs sudo cpulimit -l 40 -p '{}'
(not tested)



Hello pan64,
thanks alot for your extended support,

this script does not work, and give me this error:
[1]+ Stopped ./limit.sh

to make sure of output i replace it with:
pgrep qemu | xargs > process.txt

then i got these result on the redirected file:
25457 25476

but still unable to make the action for both process base on the script.

any help please.


linux833.

pan64 09-04-2012 12:04 PM

ok, another solution:
Code:

for p in `pgrep qemu`
do
      sudo cpulimit -l 40 -p $p &
done

not tested again

linux833 09-04-2012 12:56 PM

Quote:

Originally Posted by pan64 (Post 4772461)
ok, another solution:
Code:

for p in `pgrep qemu`
do
      sudo cpulimit -l 40 -p $p &
done

not tested again

Hello pan64,

Thank alot for your solution, it's working :-),

could you please explain what you are referencing for this : ($p).
is it predefine variable??

linux83.

pan64 09-04-2012 02:14 PM

pgrep qemu will return the pids of the processes
for p in `pgrep qemu` will construct a loop and p will be set to every pid one by one. see the man page of bash, how it works
$p is the value of the variable p - inside the loop.

If you really want to say thanks, press Yes on the bottom right corner....

linux833 09-04-2012 03:39 PM

thanks pan64 for your help and support, i rate this for you.

thanks again.

linux833.

David the H. 09-04-2012 11:28 PM

I would make one minor modification, and use a while loop instead.

Code:

pgrep qemu | while read procname ; do
      sudo cpulimit -l 40 -p "$procname" &
done

Reading Lines With For is not a good habit to get into, even if the input here is safe to use with it.

It's also a good idea to use more explicit variable names.

linux833 09-05-2012 02:17 AM

Quote:

Originally Posted by David the H. (Post 4772869)
I would make one minor modification, and use a while loop instead.

Code:

pgrep qemu | while read procname ; do
      sudo cpulimit -l 40 -p "$procname" &
done

Reading Lines With For is not a good habit to get into, even if the input here is safe to use with it.

It's also a good idea to use more explicit variable names.


Hello David,

Thanks alot for your help, your solution is working as well.
your advice is appreciated.

thanks again.

linux833

konsolebox 09-05-2012 02:29 AM

I'm not sure if executing files with sudo in background (&) is a good thing. What if it's something that would ask for a password? Might as well just create or run an sbin script.
Code:

sudo [bash] script.sh
chmod 700 /usr/local/sbin/script
/usr/local/sbin/script


konsolebox 09-05-2012 02:40 AM

Quote:

Originally Posted by David the H. (Post 4772869)
I would make one minor modification, and use a while loop instead.

Code:

pgrep qemu | while read procname ; do
      sudo cpulimit -l 40 -p "$procname" &
done

Reading Lines With For is not a good habit to get into, even if the input here is safe to use with it.

It's also a good idea to use more explicit variable names.

Except that values of variables within the loop are lost after it ends. It would rather be best to use named pipes with bash instead:
Code:

while read procname ; do
      sudo cpulimit -l 40 -p "$procname" &
done < <(exec pgrep qemu)

Too bad it's not available in any shell except bash :)
Wonder what would people who dislike bash think about it.

Add: Using file descriptors and read -u as well would be useful.

David the H. 09-07-2012 12:01 AM

Quote:

Originally Posted by konsolebox (Post 4772983)
Except that values of variables within the loop are lost after it ends. It would rather be best to use named pipes with bash instead:

Absolutely correct. Actually, I went with the piped form because I wasn't sure which shell the OP was using, and because the command inside the loop was stand-alone, so the variable didn't appear to be needed elsewhere. I was feeling a bit lazy and didn't feel like posting both versions, so I chose to stick with the portable one. :)


All times are GMT -5. The time now is 09:20 PM.