Skip to main content
Solved

Detect if process is running only occasionally works?


Forum|alt.badge.img+7
  • Valued Contributor
  • 50 replies

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".

View original
Did this topic help you find an answer to your question?

2 replies

Forum|alt.badge.img+15
  • Esteemed Contributor
  • 719 replies
  • 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
  • 50 replies
  • January 25, 2021

That did the trick @cbrewer , thank you!


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings