how to kill a process/task?

hi there,

i’m very new to AS and terminal scripts, so have mercy with me . :slight_smile:
i already figured out, how to start a nice bandwith optimizing app (throttle, see http://wincent.org/article/articleview/161/1/25/ with

do shell script "sudo /usr/local/sbin/throttled-startup" password "password here" with administrator privileges

and it works fine so far (thanks to this forum, already). i created an executable on my desktop which does the job, but i’d like to have another script which terminates this thread/process/task. this is where i need you help, i have no idea at all .

help appreciated, thank you.

Model: TiPB DVI 867 MHz, 768 MB
AppleScript: AppleScript D1-1.9.3
Browser: Safari 125.12
Operating System: Mac OS X (10.3.8)

kill_pid=`ps ax | grep iTunes | grep -v grep | awk '{ print $1 }'`; kill $kill_pid

instead of iTunes, kill whatever you want. This definitely works on the command line. Note: you’ll have to do some work escaping everything like quotes and such.


You can define… I can’t remember what they are called, you can read the bash manpage if you are on a mission… lets call it a macro.

¢ a macro is defined inside `` marks.
¢ the pipe symbol (|) means send the output of the first command into the input for the second command.
¢ a semicolon (:wink: marks the end of one command (&& means continue if the first command executed properly while || means logical OR)
¢ a variable is always marked with a $ before it

¢ ps will list the running processes
¢ grep will search for a specific string on a line ( the -v means to invert the search)
¢ awk will output an item(s) in a specific position (here the first position).
¢ kill… will kill

thank you anaxamander,

but when i execute your code and have, to check it out, iTunes running for testing purposes, i get this as a result:

sounds as if were very close here but i can’t help myself finding an answer . still learning.

Ah, you changed your default shell to tcsh.

You should be aware that the syntax expected inside Applescript’s “do shell script” is that of /bin/sh (which is basically a special version of bash). tcsh syntax won’t work (well, it might if you jump through hoops):

set kill_pid=`ps ax | grep iTunes | grep -v grep | awk '{ print $1 }'`; kill $kill_pid

when you are testing out something really complicated, you should save yourself some time by opening a Terminal window, typing “/bin/sh” and doing testing there. Type “exit” or just close the window to exit out of the sh shell. There is even some bash syntax that isn’t allowed in sh syntax - so it will save you some hair on you head at the end of the day.

And in a /bin/sh shell, this new command won’t work.

ok “ i checked the code in terminal and you’re right, it won’t work there just like that. whereas

do shell script "kill_pid=`ps ax | grep iTunes | grep -v grep | awk '{ print $1 }'`; kill $kill_pid"

works like it should. in my case, i want to kill a task called throttled (http://wincent.org/article/articleview/161/1/25/) “ which limits upstream bandwith for genuine reasons “ but the code you provide will quit iTunes but not throttled. i use the same process name as shown in activity monitor but i get an error saying

. why is that?

You didn’t start throttled as a regular user did you? Look at how you started throttled - with admin privs. You have to kill it the same way. I hate to tell you, but lowly old “pat” can’t kill LOTS of things.

This example does kill root owned processes:

on clicked theObject
	do shell script "kill_pid=`ps ax | grep crashreporterd | grep -v grep | awk '{ print $1 }'`; kill $kill_pid" password "something" with administrator privileges
end clicked 

“You can click… and you can kill!”

well, i tried to add that password too but forgot to add the admin privs in the syntax . told you i’m new to this, didn’t i. if i can’t kill or not depends largely on you this time, because your code

do shell script "kill_pid=`ps ax | grep throttled | grep -v grep | awk '{ print $1 }'`; kill $kill_pid" password "whatever" with administrator privileges

looks great, and reports this error

sometimes i decide to start throttled as a startup item, where killing it requires admin privs. sometimes i start it myself, when i suddenly decide to need it.

Well the last “on clicked” handler I posted I tested, so it worked on 10.4. But I know how that error comes about - you can see it yourself on the command line if you do “sudo kill_pid…”. I believe it comes about because you are trying to do more than one command under “sudo xxx” (the trick is to JUST “sudo kill $kill_pid”). I guess Apple fixed this in 10.4 or I would have hit it like you did. Try this:

on clicked theObject
    set kill_pid to do shell script "ps ax | grep crashreporterd | grep -v grep | awk '{ print $1 }'"
    do shell script ("kill" & space & kill_pid) password "cheeky_monkey" with administrator privileges
end clicked 

As you gain more experience, you can write some more logic to see if you really NEED the admin password. You can test to see of throttled (or whatever) is a process owned by root ps -ajx | grep crashreporter | grep -v grep | awk '{ print $1 " " $2 }'
and see if the response has “root”, and if it does, get the admin password. Parse on the space, and kill that pid.

As it states in TN2065 (a very informative document):

So, try:

set the_pid to (do shell script "/usr/local/sbin/throttled-startup &> /dev/null & echo $!" password "password here" with administrator privileges)
--do some check to see if it's time to kill the thread
do shell script "kill " & the_pid password "password here" with administrator privileges

Jon

did everyone overlook killall or is there a reason for not using it ?

killall kills processes by their name as im aware so really all that “ps” and “grep” is irrelivant, and since the process was started as root, the sudo is needed anyway(killall needs to be run as root to kill any process).


do shell script "sudo killall" & the_pid password "password here" with administrator privileges

:smiley:
works as advertised, thanks a bunch anaxamander!
i also tried the solutions provided by jonn8 and bigkm, but failed to be successful.

another thing that i can’t explain is the other way around, that is, starting throttled. i removed the startup Æ’ from myHD/library/startup items/throttledStartup so i can start it when needed. i COULD start it before with this script

do shell script "sudo /usr/local/sbin/throttled-startup" password "password here" with administrator privileges

but now stalls, when i try to execute it. but i can still start it via terminal and

aha?! where is the difference in executing this script that leads AS to stall? and how can i accomplish to get a verbose output like what gets echoed when executing the line in the terminal?

Model: TiPB DVI 867 MHz, 768 MB
AppleScript: AppleScript D1-1.9.3
Browser: Safari 125.12
Operating System: Mac OS X (10.3.8)

I think bigkm’s script would work for you if you did:

do shell script "sudo killall throttled" "password here" with administrator privileges

and the reason I use that complicated grep/awk stuff is an adaptation of stuff that is in my bash .profile file :

[code]function command () {
ps -axwwo command | grep $1 | grep -v grep
}

function Azkill () {
Az_pid=ps ax | grep Azureus | grep -v grep | awk '{ print $1 }'
kill $Az_pid
}[/code]

jonn8’s routine will work if you start and kill using Applescript. You need to keep this script running (or save the pid somewhere as a text file or as a user default), but with sending output to /dev/null you loose the output that you are currently looking for. You can use his code after you find out the problem.

If you look at jonn8’s post here, in his first line he provides a link - you will find the answer you are looking for there (and apparently how to use tsch terminology easily). Hint: you want a “bunch of output”.

i can start throttled with

do shell script "sudo /usr/local/sbin/throttled-startup" password "password here" with administrator privileges

but the problem is, that the script editor or the script stalls and i have to cmd+option+Q to get out of it again. and the script worked, until i removed the throttled startup Æ’. so far, i couldn’t solve this by reading the man pages, which were helpful anyway.

If you’re running Mac OS X 10.4+, don’t use “sudo” and “with administrator privileges” unless you use some sort of bounce back command (again, see TN2065 or search for threads like this one).

Jon

i read that too, but i’m still on 10.3.8 .

use try
it might help you with debuging.

try
do shell script "sudo /usr/local/sbin/throttled-startup" password "password here" with administrator privileges
on error 
display dialog "No go !!!"
end try