Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A simple priority-changing AppleScript System
This Change Priority AppleScript allows you to easily set the priority of any running application via a simple GUI. I built it based on the many previous priority-related hints, and would appreciate any suggestions.

I use
ps -axww | /usr/bin/grep '[/]APPNAME'| awk '{print $1}' | head -1
to get the pid. Is there a better way?

[Editor's note: I downloaded and tested this script, and it does exactly what it states - you get a list of all running processes, click the one you wish to change, set the priority level you want, and say OK.]
    •    
  • Currently 3.67 / 5
  You rated: 2 / 5 (3 votes cast)
 
[5,913 views]  

A simple priority-changing AppleScript | 18 comments | Create New Account
Click here to return to the 'A simple priority-changing AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
works great!
Authored by: Anonymous on Jan 19, '02 01:38:23PM

It works great and seems to do exactly what it should.

However, two suggestions.

Go ahead and save it as an application.

Use a slider bar to set priority. The scale is -20 to 20, right?



[ Reply to This | # ]
works great!
Authored by: MikeS on Jan 21, '02 04:22:46AM

Before this topic started, I had commenced writing an AppleScript Studio application to perform the same task. I have now put all (most) of the code from this thread into this application.

The only thing I have not done, but will do eventually is test for an administrator before the settings are changed, and perhaps change the time of when the process list is updated.

The application as is, can be found at the following address, as AppleScript Studio source files and a compiled application.

http://homepage.mac.com/schled/FileSharing1.html



[ Reply to This | # ]
Great Idea!
Authored by: ExecutiveEditor on Jan 19, '02 04:33:54PM

I've been using Nicer, which is a great program, but I'd rather not launch an app whenever I want to give TuxRacer a higher priority. Now, I can use this script in the Script Menu (available from Apple's Applescript page).

The script has one little problem, however: It keeps running if you choose to cancel rather than pick a process to renice. Here's my mod, which cancels the program if you hit the "Cancel" button:

tell application "Finder" to set theApps to name of every process
set theApp to (choose from list theApps OK button name "Edit Priority")
if theApp is not false then
set thePID to do shell script "ps -axww | /usr/bin/grep '[/]" & theApp & "'| awk '{print $1}' | head -1"
set newPriority to text returned of (display dialog "New priority for '" & theApp & "':" default answer "0")
Renice(newPriority, thePID, theApp)
end if
on Renice(newPriority, thePID, theApp)
try
set theConfirmation to (do shell script "renice " & newPriority & " -p " & thePID)
on error
set theConfirmation to (do shell script "renice " & newPriority & " -p " & thePID with administrator privileges)
end try
display dialog ("Changed priority of '" & (theApp as string) & "':" & return & theConfirmation) buttons {"OK"} giving up after 5
end Renice

Copy this text, paste it in Script Editor, and save.



[ Reply to This | # ]
Great Idea!
Authored by: strummer on Jan 20, '02 12:10:53AM

okay, sounds good. I tried it, I picked APP, set Priority, then got this message:

sudo: renice: command not found

any help?
cheers,
brian



[ Reply to This | # ]
where do I find renice?
Authored by: strummer on Jan 20, '02 09:58:55AM

i get error that says renice not found
any help?
cheers,
brian



[ Reply to This | # ]
where do I find renice?
Authored by: ExecutiveEditor on Jan 20, '02 01:04:28PM

Which version of OS X are you using? I think I remember something here on Mac OS X Hints that indicated nice/renice wasn't available before 10.1.



[ Reply to This | # ]
where do I find renice?
Authored by: strummer on Jan 20, '02 04:13:43PM

I have the current 10.1.2
is it part of the developer's tools?
because I haven't loaded that yet.
thanks
cheers,
brian



[ Reply to This | # ]
where do I find renice?
Authored by: ExecutiveEditor on Jan 20, '02 09:08:17PM

I wouldn't think so, but I'm not sure. I have the Developer Tools installed on both my Mac at work and the one at home, so I can't check. Sorry.



[ Reply to This | # ]
where do I find renice?
Authored by: babbage on Jan 21, '02 01:53:22PM
If you need to find where a command is, always give "which" a try first. This will give you the instance of the program that shows up first in your path (thus if you have a bunch of custom stuff in /usr/local/bin and some of it overrides default stuff in /usr/bin or /bin, then you'll get whichever version shows up first in your path:
% which renice
/usr/bin/renice
% env | grep '^PATH'
PATH=/usr/X11R6/bin:/sw/bin:/usr/local/bin:/usr/local/sbin:/usr/sbin:/sw/sbin:/Users/chris/bin/powerpc-apple-darwin:/Users/chris/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/X11R6/bin:/usr/X11R6/bin
% 

If I remember correctly, the nice & renice commands were installed as far back as the Public Beta, but the Darwin kernel didn't acknowledge run level change requests until 10.1. Before that you could run the command, but it didn't really do anything.

[ Reply to This | # ]

where do I find renice?
Authored by: barrysharp on Jan 21, '02 04:37:19PM

...also if one is using the Shell /bin/sh then one can use the Shell's (/bin/sh) built-in command 'type'.

Thus

> type renice
renice is: /usr/bin/renice

> type type
type is an exported alias for 'whence -v'

> whence -v renice
renice is a tracked alias for /usr/bin/renice

> whence -v type
type is a shell builtin


Mind you, type ONLY works if '/usr/bin' is defined in the PATH enviroment variable. If '/usr/bin' not defined in PATH then what you would see is

> type renice
renice not found

Hope this helps explains some additional background.

Regards... Barry Sharp



[ Reply to This | # ]
where do I find renice?
Authored by: barrysharp on Jan 20, '02 10:04:36PM

stummer:

I'm running X v10.1.2 and it's located at /usr/bin/renice.

Regards... Barry Sharp



[ Reply to This | # ]
Beware of
Authored by: barrysharp on Jan 20, '02 12:49:07AM

All UNIX systems employ the nice value associated with processes to provide the proper fast & timely attention by the CPU so that they can do their assigned work.

If this nice value assignment policy is disturbed by renice-ing some inappropriate processes just to speed them along it will be at your peril and could/may/will cause very undesirable side affects.

Be careful what you renice IMHO.

Regards... Barry Sharp



[ Reply to This | # ]
PID
Authored by: Mithrandir on Jan 20, '02 08:13:18AM

In answer to your question: to get the pid. Is there a better way?

If there is a better way I've never seen it (not that I am all seeing.) I have seen other methods such as with Perl but I don't think either is better. Your method should be solid.

M



[ Reply to This | # ]
another way to get the pid
Authored by: garbanzito on Jan 24, '02 07:34:55PM
if you're going to have the user choose from a list, why not use the list that ps provides in the first place, then you don't have to grep to find the pid. i put a crude example of this quick & dirty renice. it could be improved, especially if the filename were extracted from the command path. but i think Nicer is pretty nice. it does a couple of other things too.

[ Reply to This | # ]
Is there a better way ?
Authored by: barrysharp on Jan 20, '02 10:08:38AM

Well I believe you can eliminate the 'head -1' and save having to launch the 'head' command. I'm assuming the 'head -1' is simply there to pick off the first line if multiple Apps were found by 'grep'.

/bin/ps -axww | /usr/bin/grep '[/]APPNAME'| awk '{print $1;exit}'

This will accomplish the same without using the trailing 'head -1'.

Also, since you're using absolute path name for 'grep' then why not for the other commands such as /bin/ps and /usr/bin/awk.

Thus...

/bin/ps -axww | /usr/bin/grep '[/]APPNAME'| /usr/bin/awk '{print $1;exit}'

would be the finished product.

For what it's worth... Remember that /bin/ps isn't guaranteed to always return ALL the process information ALL the time. The /bin/ps command makes the best attempt to produce an accurate process report. The kernel proc table is typically in a state of flux and is maintained by the kernel as a linked list. This aspect will at times mean that when /bin/ps requests process information from the kernel the kernel cannot always return a 'clean' proc table to /bin/ps. I've seen at times over the years hanging /bin/ps processes because they've been delivered a corrupt or incomplete proc table that has circular link pointers.

Regards... Barry Sharp



[ Reply to This | # ]
Is there a better way ?
Authored by: barrysharp on Jan 20, '02 10:36:42AM

...also it's always good practice to sanity check a value such as PID to see if it conforms. In this case the PID must be numeric. A /bin/sh function for doing this is as follows.

function isnumber
{
# Return TRUE status = 0 if argument contains ONLY digits
[[ $1 = +([0-9]) ]]
}

Thus one could script the following to sanity check the PID value

if ! isnumber `/bin/ps -axww | /usr/bin/grep '[/]APPNAME'| /usr/bin/awk '{print $1;exit}'`; then
echo PID is not numeric
else
echo PID is numeric
fi

This technique might be useful to make the "Nicer" feature more robust and maybe to avoid it doing stupid things. For example let's say that PID for some strange reason were to be the string "`/bin/rm *`". Now executing /usr/bin/renice $PID would be rather nasty. ;-)

Sanity check anything and everything that is done -- especially if it's done under the root account.

Regards... Barry Sharp



[ Reply to This | # ]
Higher/Higher/etc.
Authored by: loren_ryter on Jan 20, '02 07:06:33PM

I'm neither a UNIX wonk nor do I know a lot about renice... But if my memory is correct that the range is -20 (highest priority) to 20 (lowest priority), this makes the script a bit more user-friendly:

------

property priorities : {"Highest (-20)", "Higher (-15)", "High (-10)", "Normal (0)", "Low (10)", "Lower (15)", "Lowest (20)"}
tell application "Finder" to set theApps to name of every process
activate
set theApp to (choose from list theApps OK button name "Edit Priority") as string
log theApp
if theApp is not false then
set thePID to do shell script "ps -axww | /usr/bin/grep '[/]" & theApp & "'| awk '{print $1}' | head -1"
set the_priority to (choose from list priorities OK button name "Set Priority") as string

if the_priority contains "Highest" then set newPriority to "-20"
if the_priority contains "Higher" then set newPriority to "-15"
if the_priority contains "High (" then set newPriority to "-10"
if the_priority contains "Normal" then set newPriority to "0"
if the_priority contains "Low (" then set newPriority to "10"
if the_priority contains "Lower" then set newPriority to "15"
if the_priority contains "Lowest" then set newPriority to "20"

log newPriority
Renice(newPriority, thePID, theApp)
end if
on Renice(newPriority, thePID, theApp)
try
set theConfirmation to (do shell script "renice " & newPriority & " -p " & thePID)
on error
set theConfirmation to (do shell script "renice " & newPriority & " -p " & thePID with administrator privileges)
end try
display dialog ("Changed priority of '" & (theApp as string) & "':" & return & theConfirmation) buttons {"OK"} giving up after 5
end Renice



[ Reply to This | # ]
A simple priority-changing AppleScript
Authored by: b1naryth0ughts on May 27, '03 02:07:50AM

Things that I have found...

1. The "-p" in the renice in is optional, I suggest you remove this...
2. Instead of taking the tiny bit of ram to hold the Process ID, you could combine both scripts...

---
It's only a matter of time before I take over the world with my army of bots.



[ Reply to This | # ]