Script to see if iTunes is running

dpertschi
Valued Contributor

I simply want to check and see if iTunes is running, if it is -- do nothing, if it isn't -- custom trigger to update it.

This seems to be working for everything I throw at it except iTunes. Looks like ps is picking up iTunesHelper even when iTunes is quit. Any suggestions here?

process="iTunes"
processrunning=$( ps axc | grep "${process}" )
if [ "$processrunning" != "" ]; then
    /bin/echo "$process IS running, we do nothing now"
else
    /bin/echo "$process IS NOT running - run custom trigger here to update"
fi
1 ACCEPTED SOLUTION

mm2270
Legendary Contributor III

Adjust the line to look like this:

processrunning=$( ps axc | grep "${process}$" )

View solution in original post

4 REPLIES 4

mm2270
Legendary Contributor III

Adjust the line to look like this:

processrunning=$( ps axc | grep "${process}$" )

sean
Valued Contributor

As suggested, putting a dollar at the end will help prevent false positives. You could be more specific with:

osascript -e 'tell app "System Events" to count processes whose name is "iTunes"'

Alternatively, you may wish to consider doing updates as part of a logout process instead.

dpertschi
Valued Contributor

@mm2270 - bingo, that'll do it thank you very much.

sean -in this case yes, logout would be better technically. And at login I'm finding iTunes frequently launching before the update runs to completion, causing problems. I have varying 'social' problems running updates at both login and logout.

sepiemoini
Contributor III
Contributor III

Howdy, all! I have a custom script here that leverages this basic concept into a policy that can be used in Self Service. It effectively just checks to see if a specified process/application is running and then executes the installation if it is not running. If it is running, the user is prompted with a JAMF Helper window which times out after 30 seconds.

Parameters are fun :)

#!/bin/sh

####################################################################################################
#
# DEFINE VARIABLES & READ IN PARAMETERS
#
####################################################################################################
#
# A HARDCODED VALUE FOR "processName," "jamfPolicyType" and "eventorIDName" CAN BE SET BELOW.
#
# A list of accepted process name values can be verified in Activity Monitor.
#
# Delete the double quotes and replace with the desired process name and/or event name, e.g. 
# processName=Safari.app or eventorIDName=runSafariPolicy. For jamfPolicyType, pass "-id" or "-event."
# 
# If this script is to be deployed via policy using the JSS leave the next line as is.
#
####################################################################################################

processName=""
jamfPolicyType=""
eventorIDName=""

####################################################################################################
#
# SCRIPT CONTENTS - DO NOT MODIFY BELOW THIS LINE
#
####################################################################################################

# CHECK TO SEE IF A VALUE WAS PASSED IN PARAMETER 4 AND, IF SO, ASSIGN TO "processName"

if [[ "$4" != "" ]] && [[ "$processName" == "" ]]
then
    processName=$4
fi

# CHECK TO SEE IF A VALUE WAS PASSED IN PARAMETER 5 AND, IF SO, ASSIGN TO "jamfPolicyType"

if [[ "$5" != "" ]] && [[ "$jamfPolicyType" == "" ]]
then
    jamfPolicyType=$5
fi

# CHECK TO SEE IF A VALUE WAS PASSED IN PARAMETER 6 AND, IF SO, ASSIGN TO "eventorIDName"

if [[ "$6" != "" ]] && [[ "$eventorIDName" == "" ]]
then
    eventorIDName=$6
fi

####################################################################################################
#
# JAMF HELPER CONTENTS - DO NOT MODIFY BELOW THIS LINE
#
####################################################################################################

/usr/bin/curl -s -o /tmp/iconName.png <pathToURLorDirectory>
icon="/tmp/iconName.png"

jamfHelper="/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper"
windowType="hud"
description="Please close the $processName application and retry."
button1="OK"
title="Warning: Application Running"
alignDescription="left" 
defaultButton="1"
cancelButton="1"
timeout="30"

####################################################################################################
#
# CHECK TO SEE IF THE SPECIFIED PROCESS IS RUNNING, THEN ECHO STATEMENT DEPENDING ON CONDITION
#
####################################################################################################

processRunning=$(ps axc | grep "${processName}$")
jamf="/usr/local/bin/jamf"

if [[ $processRunning != "" ]]
then
     echo "$processName is running. Prompt user to close the application and re-run policy from Self Service."
     #JAMF Helper
     userChoice=$("$jamfHelper" 
-windowType "$windowType" 
-lockHUD 
-title "$title" 
-timeout "$timeout" 
-defaultButton "$defaultButton" 
-cancelButton "$cancelButton" 
-icon "$icon" 
-description "$description" 
-alignDescription "$alignDescription" 
-button1 "$button1")
else
     echo "$processName is NOT running. Search for $jamfPolicyType triggered by $eventorIDName and install $processName."
     $jamf policy $jamfPolicyType $eventorIDName
fi