Skip to main content
Solved

Scripting Question : Killing Processes


Forum|alt.badge.img+18

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.

Best answer by nortonpc

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 original
Did this topic help you find an answer to your question?

12 replies

Forum|alt.badge.img+15
  • Contributor
  • 37 replies
  • November 4, 2013

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


Forum|alt.badge.img+18
  • Author
  • Honored Contributor
  • 645 replies
  • November 4, 2013

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


Forum|alt.badge.img+15
  • Contributor
  • 37 replies
  • Answer
  • November 4, 2013

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
Forum|alt.badge.img+35
  • Employee
  • 1797 replies
  • November 4, 2013

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.


Forum|alt.badge.img+18
  • Author
  • Honored Contributor
  • 645 replies
  • November 4, 2013

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


Forum|alt.badge.img+12
  • Contributor
  • 194 replies
  • July 23, 2015

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


stevewood
Forum|alt.badge.img+35
  • Employee
  • 1797 replies
  • July 23, 2015

@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
Forum|alt.badge.img+16
  • Legendary Contributor
  • 7880 replies
  • July 23, 2015

@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...

Forum|alt.badge.img+17
  • Contributor
  • 881 replies
  • July 23, 2015

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


Forum|alt.badge.img+12
  • Contributor
  • 194 replies
  • July 23, 2015

this is great @mm2270 Thank You!


Forum|alt.badge.img+12
  • Contributor
  • 529 replies
  • July 23, 2015

Check out pkill.


donmontalvo
Forum|alt.badge.img+36
  • Legendary Contributor
  • 4293 replies
  • December 16, 2015

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


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings