Sometimes when you're deploying software updates and upgrades, you need to identify if the application in question that is being updated and/or upgraded is running and if it is, sometimes you need to quit it.
Sometimes, some applications that you are trying to update and/or upgrade are just being a little too persnickety and the "gentle" method of "quitting cleanly" either isn't an option, or it's just too much of a hassle.
With that, allow me to introduce my latest pre-flight script I affectionately call the "App Process Killer".
#!/bin/bash
##############################################################################
#
# SCRIPT FILENAME:
# AppProcessKiller.sh
#
#
# SCRIPT SYNTAX AND EXAMPLE:
# Syntax: sudo AppProcessKiller.sh [AppName]
#
# Examples
# sudo AppProcessKiller.sh "Microsoft Office"
# sudo AppProcessKiller.sh zoom.us
# sudo AppProcessKiller.sh calculator
#
#
# DESCRIPTION:
# Use for killing an app or process with extreme prejudice.
# Works on command line via $1 or within Jamf Pro via $4.
#
# Jamf Variables
# https://www.jamf.com/jamf-nation/articles/146/script-parameters
# $1 - Predefined Variable - Mount point of the target drive
# $2 - Predefined Variable - Computer name
# $3 - Predefined Variable - Username
# $4 - Custom Variable - Okta Device Trust Version Number
#
# USE CASE:
# Use as a pre-flight script prior to software updates and upgrades, etc.
#
#
# CHANGE LOG:
# v1.0 - 2019-07-09
# Written by Caine Hörr.
# * Initial Script Creation
#
##############################################################################
##############################################################################
#
# CHOOSE YOUR RUN MODE
appName="$(/bin/echo ${1})" # Run Mode: Command Line
# appName="$(/bin/echo ${4})" # Run Mode: Jamf Pro
#
##############################################################################
##############################################################################
#
# THERE ARE NO USER SERVICEABLE PARTS HERE...
# DO NOT EDIT BELOW THIS LINE!
#
##############################################################################
# Confirm the existence of the command line argument
if [ -z "${appName}" ]; then
echo ""
echo "ERROR: Missing Argument"
echo ""
echo "SYNTAX: sudo ${0} [appName]"
echo ""
echo " EXAMPLES:"
echo " sudo ${0} "Microsoft Office""
echo " sudo ${0} zoom.us"
echo ""
exit 1
fi
main(){
Run_as_Root
Process_Killer
}
Run_as_Root(){
# Check for admin/root permissions
if [ "$(id -u)" != "0" ]; then
echo ""
echo "ERROR: Script must be run as root or with sudo."
echo ""
exit 1
fi
}
Process_Killer(){
processCheck=$(pgrep -i ${appName})
if [ "${processCheck}" = "" ]; then
echo "${appName} is not running..."
else
echo "${appName} is running..."
echo "${appName} Process ID: ${processCheck}"
echo "Quitting ${appName}..."
sudo kill -9 ${processCheck}
fi
}
main
exit
As you can see, the script is fairly straight forward.
The syntax is as simple as this...
sudo .AppProcessKiller.sh AppName
You can run it on the command line or from within Jamf. Just edit the "run mode" portion of the script and you're good to go.
When running from command line, the script reads in $1 - That's the app or process name you want to kill.
When running from Jamf, you need to set up your $4 variable in Settings > Computer Management > Scripts.
In your policy, you identify the name of the app or process name your want to kill.
Set your script to run "Before" and then your policy can install the update/upgrade you require.
Here's some sample output of the script in action:
$ sudo ./AppProcessKiller.sh Calculator
Password:
Calculator is running...
Calculator Process ID: 3369
Quitting Calculator...
Let's see that again, but this time, Calculator isn't running...
$ sudo ./AppProcessKiller.sh Calculator
Calculator is not running...
If the script is run without the correct syntax, you see the following:
$ ./AppProcessKiller.sh Calculator
ERROR: Script must be run as root or with sudo.
OR
$ sudo ./AppProcessKiller.sh
ERROR: Missing Argument
SYNTAX: sudo ./AppProcessKiller.sh [appName]
EXAMPLES:
sudo ./AppProcessKiller.sh "Microsoft Office"
sudo ./AppProcessKiller.sh zoom.us
These messages will also be reflected in your Jamf policy logs so as to assist you in troubleshooting any problems you may have.
Anyway - This script comes in handy for some of my needs - hopefully it will serve you well too!
Cheers!