Scripting Question : Killing Processes

dgreening
Valued Contributor II

I am running in to a roadblock with a script which I have written to automate some of the steps in our Entourage to GMail conversion process. This script is being run via a policy in Self Service, which is when I started having issues killing off a process (BigHonkingText) which I am running to notify the user of the script's status.

When I ran the script locally as root with "killall BigHonkingText" to kill off an instance of BigHonkingText set to "-p 0" (as I want it to run for the entirety of the run of an AppleScript via osascript) it worked just fine. Once I put the script up in a policy and ran it via Self Service, the instance of BigHonkingText would not longer die when the "killall BigHonkingText" command was run.

I tried working around it by gathering the PID of BigHonkingText, and then "kill -9 "$THEPROC"", however, it still won't die.

Here is the snippet of the code which I am having trouble with (please note this is not the whole script - everything else works correctly):

THEPROC=pgrep BigHonkingText | awk '{print $1}'

/usr/local/bin/BigHonkingText -m -b blue -p 0 -w 90% -h 20% "RUNNING ENTOURAGE EXPORT - PLEASE DO NOT TOUCH YOUR COMPUTER" &

osascript /Users/$THEUSER/GMail Conversion Folder/Scripts/BPS_Export_Entourage_Mail_10212013.scptd

kill -9 "$THEPROC"

Any ideas are much appreciated.

2 ACCEPTED SOLUTIONS

nortonpc
Contributor

Sorry, I missed that part.

Here is a script, of which I use all the time for different apps to find and kill a process.

pid=$(ps axo pid,command | grep "[A]crobat" | awk '{print $1}')

echo "Pid is: "$pid

if [ "$pid" ]
    then
        echo "Acrobat is Running"
        echo "Killing Acrobat pid"
            kill $pid
        echo "Pid killed"
    else
        echo "Acrobat not running"
    fi

I really like using echos to test, that way I know where the script died, or if it even got to the kill command.

View solution in original post

stevewood
Honored Contributor II
Honored Contributor II

Looking at the snippet of code you put, it looks like you're grabbing the PID before you even run BigHonkingText. I would debug by throwing an echo statement in just before the killall to make sure you have the correct PID.

In my mind, this line:

THEPROC=`pgrep BigHonkingText | awk '{print $1}'`

Needs to be between the call to BigHonkingText and the osascript line.

View solution in original post

12 REPLIES 12

nortonpc
Contributor

Self service runs things as root, you might need to run your kill command at an elevated permissions level such as with Sudo.

dgreening
Valued Contributor II

The entire script should be running as root, no? When I ran the script locally as root it works fine.

nortonpc
Contributor

Sorry, I missed that part.

Here is a script, of which I use all the time for different apps to find and kill a process.

pid=$(ps axo pid,command | grep "[A]crobat" | awk '{print $1}')

echo "Pid is: "$pid

if [ "$pid" ]
    then
        echo "Acrobat is Running"
        echo "Killing Acrobat pid"
            kill $pid
        echo "Pid killed"
    else
        echo "Acrobat not running"
    fi

I really like using echos to test, that way I know where the script died, or if it even got to the kill command.

stevewood
Honored Contributor II
Honored Contributor II

Looking at the snippet of code you put, it looks like you're grabbing the PID before you even run BigHonkingText. I would debug by throwing an echo statement in just before the killall to make sure you have the correct PID.

In my mind, this line:

THEPROC=`pgrep BigHonkingText | awk '{print $1}'`

Needs to be between the call to BigHonkingText and the osascript line.

dgreening
Valued Contributor II

Ahhh beauty! I knew that I was doing something dumb! That did the trick! Thanks!

wmateo
Contributor

How would this be done for multiple processes at a time? @stevewood @nortonpc

stevewood
Honored Contributor II
Honored Contributor II

@wmateo I'm sure there is a neat fancy *NIX way to grab all of the PIDs that you need, but you could just add additional PIDs to the script that @nortonpc sent:

pid1=$(ps axo pid,command | grep "[A]crobat" | awk '{print $1}')
pid2=$(ps axo pid,command | grep "[A]crobat" | awk '{print $1}')
pid3=$(ps axo pid,command | grep "[A]crobat" | awk '{print $1}')
etc....

Obviously changing the grep to be whatever program you are looking for. Then setup multiple if then statements to shutdown the processes.

I know there is an easier/cleaner method to doing it, I'm just not sure off the top of my head.

mm2270
Legendary Contributor III

@wmateo Are you asking how you would find multiple running app processes (different applications) at kill them at once?

While there's no way of doing a single kill command to get them all in one shot, something like this would probably help.
Note: using ps axc avoids needing to enclose the process name's first character in brackets or doing grep -v grep

#!/bin/bash

procList=("Microsoft Word"
"Safari"
"Firefox"
"Preview")

for proc in "${procList[@]}"; do
    runningProc=$(ps axc | grep -i "$proc" | awk '{print $1}')
    if [[ $runningProc ]]; then
        echo "Found running process $proc with PID: ${runningProc}. Killing it..."
        kill $runningProc
    else
        echo "$proc not found running..."
    fi
done

Just change the items in the procList array to locate what you want. It should find them if running and kill them by their PID.

The output would look something like this-

Found running process Microsoft Word with PID: 58106. Killing it... Found running process Safari with PID: 1770. Killing it... Found running process Firefox with PID: 56085. Killing it... Preview not found running...

alexjdale
Valued Contributor III

Strange, I've used killall in several policy scripts and it's always worked fine for me.

wmateo
Contributor

this is great @mm2270 Thank You!

sean
Valued Contributor

Check out pkill.

donmontalvo
Esteemed Contributor III

+1 kudos to @mm2270 for the perfect preinstall template.

--
https://donmontalvo.com