Skip to main content
Question

How to run policies when no one is logged in

  • June 6, 2019
  • 4 replies
  • 23 views

kevin_v
Forum|alt.badge.img+10

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

4 replies

Forum|alt.badge.img+18
  • Contributor
  • June 7, 2019

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.


Forum|alt.badge.img+6

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


Forum|alt.badge.img+12
  • Contributor
  • October 17, 2019

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
Forum|alt.badge.img+8
  • Valued Contributor
  • February 19, 2020

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