Simple script, silly problem...

michaelsanford

Translator, Web Developer
I made a small one-liner to renice to +1 whatever application name I pass to the script. It uses grep to look through the output of ps -aux for the string I use as an argument.

The output:
Code:
[gwailo:~/bin]% ./dumb MSN
Renicing MSN to 1
1818: old priority 0, new priority 1
1807: old priority 0, new priority 1
1541: old priority 1, new priority 1
Done
[gwailo:~/bin]% ps -aux | grep MSN | grep -v grep
gwailo  1541   0.7  1.4   100872  11024  ??  SN    10:09.45 /Applications/MSN Messenger/MS

The code:
Code:
echo "Renicing $1 to 1"
renice 1 `ps -aux | grep $1 | grep -v grep | awk '{print $2}'`
echo "Done"

So why is it doing it three times? After the script terminated I tried (ps -aux | grep 1818 | grep -v grep) and (ps -aux | grep 1807 | grep -v grep) and they both cae up empty, which leads me to believe that they are instances of grep. But I used the -v flag with grep, so why would they show up at all?

PS This is the second shell script I've ever written...
 
I love the name of your script! :D

Sorry - I have no advice to help, I've written a few more shell scripts but much, much much more simpler than yours! :p
 
Here's a hint from macosxhints.com:
alias prioritize "top -u -l 1 | head -20 | tail -12 | grep -v kernel | awk ' { print $1 } ' | xargs -n1 renice -10"
It could easily be adapted to your particular application.
 
The answer is very simple...

First, what is the command that you're running, it's "dumb MSN". In that command you're searching for all processes with the word "MSN" in it. You're thinking that it's just going to find the MSN Messenger process, but it also finds itself, as "dumb MSN" is now in the process list, cause, well it's running. Change your command to replace the 'grep -v grep' with grep -v "grep\|$0 $1" and that will take out any instance of the command you sent.

Brian
 
If you change the flags slightly on grep, you can simplify it somewhat (and get the benefit of case insensitivity -- making dumb MSN or dumb msn equally acceptable).

#!/bin/sh
echo "Renicing $1 to 1"
renice 1 `ps aux | grep -i $1 | grep -v "grep\|$0 $1" | awk '{print $2}'`
echo "Done"

Of course, you could just make it an alias in your .*shrc file:
alias dumb "renice 1 `ps aux | grep -i $1 | grep -v "grep\|$0 $1" | awk '{print $2}'`"
renice returns output anyway, so the echo lines are relatively unnecessary when a one-line alias will suffice.
 
Back
Top