Skip to main content
Answer

Detect if process is running only occasionally works?

  • January 22, 2021
  • 2 replies
  • 36 views

Forum|alt.badge.img+7

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

Best answer by cbrewer

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

2 replies

Forum|alt.badge.img+15
  • Esteemed Contributor
  • Answer
  • January 22, 2021

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


Forum|alt.badge.img+7
  • Author
  • Valued Contributor
  • January 25, 2021

That did the trick @cbrewer , thank you!