I’ve tried looking for this all over and found some partial solutions that don’t really work so I wanted to ask here.
I’ve been asked to set up several iMacs to auto-log off or reboot after 30 minutes of inactivity AND (this is the kicker) delete all files on Desktop, Downloads, and Documents.
I found the Configuration Profile Login Window setting to auto log-off but I’ve seen that if people left unsaved documents over it doesn’t work. It seems to me some scripting is needed here and that’s still a weak spot for me so I’m putting this out to this community in the hopes of some help.
lidClosed=\$(ioreg -r -k AppleClamshellState -d 4 | grep AppleClamshellState | head -1 | awk '{print \$NF}') if [[ "\$lidClosed" == "Yes" ]]; then writelog "Lid has been closed while still logged in; logging out." osascript -e 'tell application "loginwindow" to «event aevtrlgo»' rm -r /Users/Guest else writelog "Lid is not closed; exiting." fi
IdleTimeSecs=\$(expr \$(ioreg -c IOHIDSystem | awk '/HIDIdleTime/{print \$NF; exit}') / 1000000000) IdleLimit="3600" if [[ "\$IdleTimeSecs" -gt "\$IdleLimit" ]]; then writelog "Idle limit reached. Loging out user." osascript -e 'tell application "loginwindow" to «event aevtrlgo»' rm -r /Users/Guest fi
As @_Daley said you can use sudo shutdown -r now to force a reboot. MacOS will natively prevent reboots if there is unsaved work. However, also be aware macOS updates dont like to run if a user is not logged in so this could disrupt any overnight OS update workflows you have. As far as deleting the user files a simple script in a policy to loop through /Users and then loop through the contents of ~/Documents, ~/Desktop and ~/Downloads that runs on startup should accomplish what you are needing. Make sure to exempt your local admin account from this workflow as automating file deletion is very risky.
Yeah, look into Guest account. Used this in school labs in the past and it works.
There is the baked in Guest account - but you’ll want to test it. It doesn’t log out inactive users. We had to create a launchAgent to take care of that. We were using it in the capacity of “guest mode” for iPads, so naturally we wanted an inactivity log out.
lidClosed=\$(ioreg -r -k AppleClamshellState -d 4 | grep AppleClamshellState | head -1 | awk '{print \$NF}') if [[ "\$lidClosed" == "Yes" ]]; then writelog "Lid has been closed while still logged in; logging out." osascript -e 'tell application "loginwindow" to «event aevtrlgo»' rm -r /Users/Guest else writelog "Lid is not closed; exiting." fi
IdleTimeSecs=\$(expr \$(ioreg -c IOHIDSystem | awk '/HIDIdleTime/{print \$NF; exit}') / 1000000000) IdleLimit="3600" if [[ "\$IdleTimeSecs" -gt "\$IdleLimit" ]]; then writelog "Idle limit reached. Loging out user." osascript -e 'tell application "loginwindow" to «event aevtrlgo»' rm -r /Users/Guest fi
lidClosed=\$(ioreg -r -k AppleClamshellState -d 4 | grep AppleClamshellState | head -1 | awk '{print \$NF}') if [[ "\$lidClosed" == "Yes" ]]; then writelog "Lid has been closed while still logged in; logging out." osascript -e 'tell application "loginwindow" to «event aevtrlgo»' rm -r /Users/Guest else writelog "Lid is not closed; exiting." fi
IdleTimeSecs=\$(expr \$(ioreg -c IOHIDSystem | awk '/HIDIdleTime/{print \$NF; exit}') / 1000000000) IdleLimit="3600" if [[ "\$IdleTimeSecs" -gt "\$IdleLimit" ]]; then writelog "Idle limit reached. Loging out user." osascript -e 'tell application "loginwindow" to «event aevtrlgo»' rm -r /Users/Guest fi
Thank you for this! This is only used for iMacs so the LidClosed part won’t be needed. I can just remove that section and the code will still work, yes?
I don’t need to delete the entire User folder, just the contents of the Desktop, Downloads and Documents subfolders. If I replace the line “rm -r /Users/Guest” with “rm -rf /Users/Library/Desktop/*” and include additional lines for the Downloads and Documents folders, would that work?
lidClosed=\$(ioreg -r -k AppleClamshellState -d 4 | grep AppleClamshellState | head -1 | awk '{print \$NF}') if [[ "\$lidClosed" == "Yes" ]]; then writelog "Lid has been closed while still logged in; logging out." osascript -e 'tell application "loginwindow" to «event aevtrlgo»' rm -r /Users/Guest else writelog "Lid is not closed; exiting." fi
IdleTimeSecs=\$(expr \$(ioreg -c IOHIDSystem | awk '/HIDIdleTime/{print \$NF; exit}') / 1000000000) IdleLimit="3600" if [[ "\$IdleTimeSecs" -gt "\$IdleLimit" ]]; then writelog "Idle limit reached. Loging out user." osascript -e 'tell application "loginwindow" to «event aevtrlgo»' rm -r /Users/Guest fi
Thank you for this! This is only used for iMacs so the LidClosed part won’t be needed. I can just remove that section and the code will still work, yes?
I don’t need to delete the entire User folder, just the contents of the Desktop, Downloads and Documents subfolders. If I replace the line “rm -r /Users/Guest” with “rm -rf /Users/Library/Desktop/*” and include additional lines for the Downloads and Documents folders, would that work?
We wrote this to use on all devices - so it’s smart enough to know there isn’t a lid...and it gets rid of the entire profile - namely because you really should plan on removing the SSO tokens and stuff. Just remove the whole profile and be done.
lidClosed=\$(ioreg -r -k AppleClamshellState -d 4 | grep AppleClamshellState | head -1 | awk '{print \$NF}') if [[ "\$lidClosed" == "Yes" ]]; then writelog "Lid has been closed while still logged in; logging out." osascript -e 'tell application "loginwindow" to «event aevtrlgo»' rm -r /Users/Guest else writelog "Lid is not closed; exiting." fi
IdleTimeSecs=\$(expr \$(ioreg -c IOHIDSystem | awk '/HIDIdleTime/{print \$NF; exit}') / 1000000000) IdleLimit="3600" if [[ "\$IdleTimeSecs" -gt "\$IdleLimit" ]]; then writelog "Idle limit reached. Loging out user." osascript -e 'tell application "loginwindow" to «event aevtrlgo»' rm -r /Users/Guest fi
Thank you for this! This is only used for iMacs so the LidClosed part won’t be needed. I can just remove that section and the code will still work, yes?
I don’t need to delete the entire User folder, just the contents of the Desktop, Downloads and Documents subfolders. If I replace the line “rm -r /Users/Guest” with “rm -rf /Users/Library/Desktop/*” and include additional lines for the Downloads and Documents folders, would that work?
We wrote this to use on all devices - so it’s smart enough to know there isn’t a lid...and it gets rid of the entire profile - namely because you really should plan on removing the SSO tokens and stuff. Just remove the whole profile and be done.
Works for me! One last (possibly stupid) question: do we just deploy this script to the machines in question once or does this have to be deployed on a recurring basis? Thank you again!