Posted on 11-04-2013 11:19 AM
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.
Solved! Go to Solution.
Posted on 11-04-2013 11:38 AM
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.
Posted on 11-04-2013 11:50 AM
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.
Posted on 11-04-2013 11:28 AM
Self service runs things as root, you might need to run your kill command at an elevated permissions level such as with Sudo.
Posted on 11-04-2013 11:30 AM
The entire script should be running as root, no? When I ran the script locally as root it works fine.
Posted on 11-04-2013 11:38 AM
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.
Posted on 11-04-2013 11:50 AM
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.
Posted on 11-04-2013 12:17 PM
Ahhh beauty! I knew that I was doing something dumb! That did the trick! Thanks!
Posted on 07-23-2015 07:16 AM
How would this be done for multiple processes at a time? @stevewood @nortonpc
Posted on 07-23-2015 07:27 AM
@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.
Posted on 07-23-2015 08:00 AM
@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...
Posted on 07-23-2015 08:38 AM
Strange, I've used killall in several policy scripts and it's always worked fine for me.
Posted on 07-23-2015 01:10 PM
this is great @mm2270 Thank You!
Posted on 07-23-2015 03:56 PM
Check out pkill.
Posted on 12-16-2015 10:47 AM
+1 kudos to @mm2270 for the perfect preinstall template.