Posted on 10-05-2016 08:10 AM
I am in the process of writing a short script and need your help. The script is pretty simple. The aim is to first search and see if a specified process (processName=$4) is running. If it is, echo a string that states that it is running. If the specified process is not running, the script will execute /usr/local/bin/jamf policy -$5 $6 where jamfPolicyType=$5 and eventorIDName=$6.
#!/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 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="Safari"
jamfPolicyType="ID"
eventorIDName="1"
####################################################################################################
#
# 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
# CHECK TO SEE IF THE SPECIFIED PROCESS IS RUNNING, THEN ECHO STATEMENT DEPENDING ON CONDITION
processRunning=$(pgrep -i $processName)
if [[ $processRunning != "" ]]
then
echo "$processName is running. Run Inventory Scan and run script again at next check-in."
else
echo "$processName is NOT running. Search for $jamfPolicyType triggered by $eventorIDName."
/usr/local/bin/jamf policy -$5 $6
fi
exit 0
Regardless of whether or not the specified process is running, the same echo string is returned.
"$processName is running. Run Inventory Scan and run script again at next check-in."
If it's not too much to ask, can someone please sift through the provided code and let me know where I've taken a wrong turn?
Solved! Go to Solution.
Posted on 10-05-2016 11:41 AM
My guess is the -$5
is not being recognized and its simply running /usr/local/bin/jamf policy
which invokes the regular Check-in trigger. The error message you're seeing is because the check-in trigger has already been called by the LaunchDaemon with the random delay being applied to stagger it. So its in a wait mode, waiting to finally execute, and you can't call it again.
I would try doing the $5 variable in the script as jamfPolicyType="-id"
meaning include the dash in the string up at top, not outside the variable. Or if you're entering a string in the variable assignment for the policy, add it in as -id
or whatever it needs to be.
Also, I don't know if its case sensitive, but I believe its -id, not -ID, the latter of which is what's in your script.
Posted on 10-05-2016 12:16 PM
Thanks, @mm2270! I didn't think that was an issue :)
Here's the wining script!
#!/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
# CHECK TO SEE IF THE SPECIFIED PROCESS IS RUNNING, THEN ECHO STATEMENT DEPENDING ON CONDITION
processRunning=$(ps aux | grep -i $processName | grep -v grep | grep -v JAMF)
jamf="/usr/local/bin/jamf"
if [[ $processRunning != "" ]]
then
echo "$processName is running. Run Inventory Scan and run script again at next check-in."
else
echo "$processName is NOT running. Search for $jamfPolicyType triggered by $eventorIDName."
$jamf policy $5 $6
fi
Posted on 10-05-2016 08:26 AM
At first glance, nothing looks wrong in it to me.
I would probably add an echo line between the line that grabs the processRunning variable and the if/then block, like so:
processRunning=$(pgrep -i $processName)
echo "$processRunning"
if [[ $processRunning != "" ]]
Maybe somehow its not turning up empty and is grabbing something, which would be revealed with the echo line. Though really it shouldn't be picking anything up if the app isn't running as far as I can see.
Posted on 10-05-2016 08:35 AM
As a thought, you don't need to do the string equivalence check. You can inspect pgrep's exit code directly:
if pgrep -q "$processName"
then
echo "$processName is running"
else
echo "$processName is NOT running. Search for ..."
# /usr/local/bin/jamf policy ...
fi
Posted on 10-05-2016 09:58 AM
@mm2270 @NTmatter Here's what I have now:
#!/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
# CHECK TO SEE IF THE SPECIFIED PROCESS IS RUNNING, THEN ECHO STATEMENT DEPENDING ON CONDITION
processRunning=$(ps aux | grep -i $processName | grep -v grep | grep -v JAMF)
jamf="/usr/local/bin/jamf"
if [[ $processRunning != "" ]]
then
echo "$processName is running. Run Inventory Scan and run script again at next check-in."
else
echo "$processName is NOT running. Search for $jamfPolicyType triggered by $eventorIDName."
$jamf policy -$5 $6
fi
Everything looks great except the if-else statement, specifically the $jamf policy -$5 $6 command. When the defined process is not running and the aforementioned command should execute, I receive the following error:
This policy trigger is already being run: root 125 0.0 0.1 2507300 14156 s000 S+ 12:47PM 0:00.18 jamf policy -id <PolicyID>
Any ideas?
Posted on 10-05-2016 11:41 AM
My guess is the -$5
is not being recognized and its simply running /usr/local/bin/jamf policy
which invokes the regular Check-in trigger. The error message you're seeing is because the check-in trigger has already been called by the LaunchDaemon with the random delay being applied to stagger it. So its in a wait mode, waiting to finally execute, and you can't call it again.
I would try doing the $5 variable in the script as jamfPolicyType="-id"
meaning include the dash in the string up at top, not outside the variable. Or if you're entering a string in the variable assignment for the policy, add it in as -id
or whatever it needs to be.
Also, I don't know if its case sensitive, but I believe its -id, not -ID, the latter of which is what's in your script.
Posted on 10-05-2016 12:16 PM
Thanks, @mm2270! I didn't think that was an issue :)
Here's the wining script!
#!/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
# CHECK TO SEE IF THE SPECIFIED PROCESS IS RUNNING, THEN ECHO STATEMENT DEPENDING ON CONDITION
processRunning=$(ps aux | grep -i $processName | grep -v grep | grep -v JAMF)
jamf="/usr/local/bin/jamf"
if [[ $processRunning != "" ]]
then
echo "$processName is running. Run Inventory Scan and run script again at next check-in."
else
echo "$processName is NOT running. Search for $jamfPolicyType triggered by $eventorIDName."
$jamf policy $5 $6
fi
Posted on 10-05-2016 12:49 PM
Good! Glad that helped fix it. Just one comment. You can use ps axc
to get the running process without needing to use the whole grep -v grep
stuff. Like this:
processRunning=$(ps acx | grep -i "$processName")
Assuming you are entering in pretty basic process names, things like "Safari" "Microsoft Word" etc. that should work fine. Try it out if you want. I would also put the process name variable in double quotes in case its something with two or more words, like in the "Microsoft Word" example.