LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-04-2012, 06:40 AM   #1
linux833
Member
 
Registered: Jul 2011
Posts: 40

Rep: Reputation: Disabled
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.
 
Old 09-04-2012, 07:21 AM   #2
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,929

Rep: Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306
what about xargs, something like this:
pgrep app | xargs sudo cpulimit -l 40 -p '{}'
(not tested)
 
Old 09-04-2012, 10:12 AM   #3
linux833
Member
 
Registered: Jul 2011
Posts: 40

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
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.
 
Old 09-04-2012, 11:04 AM   #4
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,929

Rep: Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306
ok, another solution:
Code:
for p in `pgrep qemu`
do
      sudo cpulimit -l 40 -p $p &
done
not tested again
 
Old 09-04-2012, 11:56 AM   #5
linux833
Member
 
Registered: Jul 2011
Posts: 40

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
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.
 
Old 09-04-2012, 01:14 PM   #6
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,929

Rep: Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306
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....
 
1 members found this post helpful.
Old 09-04-2012, 02:39 PM   #7
linux833
Member
 
Registered: Jul 2011
Posts: 40

Original Poster
Rep: Reputation: Disabled
thanks pan64 for your help and support, i rate this for you.

thanks again.

linux833.
 
Old 09-04-2012, 10:28 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949
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.
 
1 members found this post helpful.
Old 09-05-2012, 01:17 AM   #9
linux833
Member
 
Registered: Jul 2011
Posts: 40

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by David the H. View Post
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
 
Old 09-05-2012, 01:29 AM   #10
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
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
 
Old 09-05-2012, 01:40 AM   #11
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by David the H. View Post
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.

Last edited by konsolebox; 09-05-2012 at 01:43 AM.
 
Old 09-06-2012, 11:01 PM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949
Quote:
Originally Posted by konsolebox View Post
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.
 
  


Reply

Tags
grep


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute shell commands that require root password from PHP ? newbie_0404 Linux - Software 2 06-27-2010 08:16 AM
rsync ssh - how to execute shell commands on server banjer Linux - Newbie 4 11-04-2009 02:01 PM
wesite which i can open and execute shell commands online sharma.kashyap Linux - General 1 02-12-2007 10:48 PM
Shell won't execute commands.... frank320 Red Hat 6 03-17-2006 11:12 AM
make c-program that accepts shell commands and execute them boeroe Programming 4 02-18-2005 08:41 AM


All times are GMT -5. The time now is 03:00 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration