Detect if process is running only occasionally works?

jonlju
Contributor

Hi all,

I've copied and pasted together scripts I've found on here to set up automated patching. In it is a piece that checks if the process is running, and if it is, it should abort. This works with for example "Microsoft Excel" and "Google Chrome" but doesn't work for "Microsoft Teams" or "Firefox". Has anyone run into a similar problem? I can run "pdkill -f firefox" and that will kill the process locally on the test Mac but it doesn't work with the script below.

#!/bin/bash
#Automatically downloads latest Firefox package and installs
#Created 6-2-2020 by Shaquir Tannis
#Influenced by https://www.jamf.com/jamf-nation/discussions/35211/updates-to-google-chrome-deployment-for-macos#responseChild200000

process="$5"
processrunning=$( ps axc | grep "${process}$" )
if [ "$processrunning" != "" ]; then
    echo "$process IS running, we do nothing now"
    exit
else
    echo "$process is not running, proceeding to update"
fi

programDownloadUrl=$(curl "$4" -s -L -I -o /dev/null -w '%{url_effective}')
pkgName=$(printf "%s" "${programDownloadUrl[@]}" | sed 's@.*/@@' | sed 's/%20/-/g')
programPkgPath="/tmp/$pkgName"
logfile="/Library/Logs/ScriptInstaller.log"

/bin/echo "--" >> ${logfile}
/bin/echo "`date`: Downloading program." >> ${logfile}
#Downloads file
/usr/bin/curl -L -o "$programPkgPath" "$programDownloadUrl"
/bin/echo "`date`: Installing $pkgName..." >> ${logfile}
#Install PKG
cd /tmp
/usr/sbin/installer -pkg "$pkgName" -target /
/bin/sleep 10
/bin/echo "`date`: Deleting package installer." >> ${logfile}
#Remove package if it still exists
if test -f "$programPkgPath"; then
    /bin/rm "$programPkgPath"
else
    echo "$programPkgPath does not exist."
fi
1 ACCEPTED SOLUTION

cbrewer
Valued Contributor II

You could try pgrep -x.
Firefox would be pgrep -x "firefox".
Teams would be pgrep -x "Teams".

View solution in original post

2 REPLIES 2

cbrewer
Valued Contributor II

You could try pgrep -x.
Firefox would be pgrep -x "firefox".
Teams would be pgrep -x "Teams".

jonlju
Contributor

That did the trick @cbrewer , thank you!