Better Software Updates with End User Interaction

mottertektura
Contributor

Greetings!

I've been using this script that I found here that @kitzy kindly shared (thanks!) to display a prompt to close apps before installing updates. And so far it's been working pretty well, however, I have discovered that in some cases I'm getting false positives. I've found that if I hardcode the variables into the script instead of passing parameters from the JSS I get more accurate results. Any ideas what I could be doing wrong?

#!/bin/bash

# Path for JAMF Helper - this should not need to be changed
jamfHelper='/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper'

## Assign variables from JSS
applicationTitle="$4"
processNames="$5"
customTrigger="$6"

IFS="," # Set internal field separator to comma to saparate process names

####################################################################
######## Use the below variables to change the alert window ########
####################################################################

# Title of the alert window
title="PRAHS Information Technology Alert!"

# Icon displayed in the alert window
icon="/Library/User Pictures/PRAHS/PRA_Health_Sciences_256x256.png"

# Bold heading of the alert window
heading1="Update ${applicationTitle}"

# Main text of the alert window
description1="needs to quit before ${applicationTitle} can be updated. 

Click OK to close the application and continue."

###########################
######## Functions ########
###########################

function promptUser()
{
# This is the main prompt function. It relies on the heading, description and process name
# passed to it as parameters 1, 2 and 3, respectively. Even though we're only using this once
# by default, this part was written as a function to simplify the code if the script is expanded
# to provide more prompts for the user.

    promptResult=""

    promptResult=`"${jamfHelper}" -lockHUD -windowType utility -icon "$icon" 
    -title "$title" 
    -heading "$1" 
    -alignHeading left 
    -description "$3 $2" 
    -button1 "OK" -button2 "Cancel" 
    -defaultButton "1"`
}

####################################
######## Begin main program ########
####################################

for process in $processNames
do

PID="" # Clear PID to elimnate false positives

PID=`pgrep "$process"` # Get application PID

if [ ! -z "$PID" ] # Detect if application is running
    then
        # Prompt user to quit the running application
        echo "$process is running, prompting user to quit"
        promptUser "$heading1" "$description1" "$process"
        if [[ $promptResult = 0 ]] # 0 indicates user clicked button 1
            then
                echo "User clicked OK"
                # Ask application to quit
                osascript -e "tell application "$process" to quit"
        elif [[ $promptResult = 2 ]] # 2 indicates user clicked button 2
            then
                # Echo for the log, then exit
                echo "User clicked Cancel"
                exit 0
        elif [[ $promptResult = 1 ]] # 1 indicates jamfHelper was unable to launch
            then
                echo "ERROR: jamfHelper returned status 1: unable to launch"
                exit 1
            else
                # If jamfHelper returns anything other than 0, 1 or 2,
                # report an error to the JSS and exit
                echo "ERROR: an unknown error occurred"
                exit 2
        fi
    else
        echo "$process not running, moving on"
fi

done

    # Call the install policy via custom trigger without prompting user
    jamf policy -event $customTrigger
    exit 0

Thanks!

3 REPLIES 3

flyboy
Contributor

When you are having these "false positives," have you done any debugging to see what your variables are actually being set to? I would be mindful of parameter expansion issues if you are passing in values for $4, $5, & $6 that contain spaces or special characters. Consider modifying

## Assign variables from JSS
applicationTitle="$4"
processNames="$5"
customTrigger="$6"

to

## Assign variables from JSS
applicationTitle="${4}"
processNames="${5}"
customTrigger="${6}"

Also, be careful entering multiple process names. The way your script is written, it looks like you must enter process names as "word,powerpoint,excel,etc." If you have spaces after the commas, you may get weird results.

Good luck!

mottertektura
Contributor

@Berrier Thanks for your suggestion, looks like that might have done the trick! What's the best way to go about checking what the variables are actually being set to just to make sure? Thanks again!

flyboy
Contributor

@mottertektura, glad it has helped! I typically drop this logging function in to the top of my scripts:

#---  FUNCTION  ----------------------------------------------------------------
#   NAME:  logAction
#   DESCRIPTION:  Use OS X's logger function to log activity
#   PARAMETERS:  Message
#   RETURNS:  
#-------------------------------------------------------------------------------


logAction ()
{
    SCPTNAME=$(basename $0)              ## Get the filename of this script and use it as a flag in the system log
        logger -t $SCPTNAME "${1}"
}

Then whenever I need to log something like an action, the results of a function, a message, etc. I call it like so:

## Set a variable and log its value
applicationTitle="${4}"
logAction "Variable applicationTitle set to "${applicationTitle}""       ##  Escape out the double quotes so we get a quoted string in the system log.

## log return value from a function
funcExitStatus=$(myFunction)
logAction "Function myFunction returned "${funcExitStatus}""

The "logAction" function writes a statement to the system log something along the lines of:

Nov 17 13:55:55 MyMacBookPro logstuff.sh [7655]: Variable applicationTitle set to "Excel"
Nov 17 13:55:55 MyMacBookPro logstuff.sh [7655]: Function myFunction returned "0"

Cheers!