How to run policies when no one is logged in

kevin_v
Contributor

Is there a way to specify that a policy be run only when no one is logged in?

4 REPLIES 4

ryan_ball
Valued Contributor

You could do two policies, one with a script as follows:

#!/bin/bash

loggedInUser=$(/usr/bin/python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");')

if [[ -z "$loggedInUser" ]] || [[ "$loggedInUser" == "root" ]]; then
     jamf policy -event "trigger_of_policy_you_want_to_run"
fi

exit 0

The other policy would have a custom trigger of "trigger_of_policy_you_want_to_run". So the first policy with script will check for a logged in user, if there is not one, it will execute the other policy.

ChicagoGuy1984
New Contributor III

Hello All,

I was wondering if anybody has setup a "REUSABLE" policy trigger for computers that dont have anybody logged in.

Example a computer lab. I would love to automate the Google Chrome updates with a script, but only Run that policy when no user is logged on.

I was trying to modify the Script above to create something of a "CUSTOM" trigger. that i could reuse for this Chrome update script or for several others.

I guess I am dreaming of a predefined policy trigger That RUNS when computer is not logged on or "IDLE" this way software would be updated in timely manner when the 'computers in the far corner' is not used or rebooted in a long time.

Thank you for all your help,

Marek

marklamont
Contributor III

Simply set the trigger to use the $4 input variable then you can set it in the Jamf policy to trigger whatever policy you want. Totally reusable

FutureFacinLuke
Contributor II

Alternatively if you have a policy you want to run, say a weekly update check and reboot and the update will not impact the User

#!/bin/sh
currentUser=$(stat -f %Su "/dev/console")
if [[ "$currentUser" = "root" ]]; then
     jamf policy -event "trigger_of_policy_you_want_to_run_if_no_user"
else
     jamf policy -event "trigger_of_policy_you_want_to_run_if_a_user_is_logged_in"
fi

exit 0