Script to kill/restart any open browser after specific amount of idle time

New Contributor III

Would anyone be able to share a script or other method(s) to accomplish this task? I've been asked to make our kiosk machines kill and restart any browser after the computer is idle for a certain period of time. This will protect any user data that may have been left open, sensitive documents, etc.

Thanks in advance for any and all replies. Cheers!


Legendary Contributor II

There is a way in the shell to get the idle time of the system. Here is a script that can accomplish that:
It reports in seconds, so you can have a LaunchAgent run periodically to check the idle time of the machine, and once it reaches your desired threshold or greater in seconds, do the kill or quit procedure on the open browsers.

Note that Launchd jobs cannot run any more frequently than every 10 seconds (the OS will throttle it if you try to have it run more frequently), so you may not be able to get the kill commands to run at exactly the idle time threshold you want, but you can typically get to it within a few seconds. I would probably not have the LaunchAgent run the script any more frequently than every 30 seconds myself.

New Contributor III

Ok, this is helpful for sure, and having it happen at an exact amount of time is not vital. I have found several of these idle timeout scripts but, I have not yet been able to find the best way to tell it to kill a specific set of apps (e.g. Google Chrome and Mozilla Firefox) and then restart Firefox to essentially "reset" the kiosk. I'm certain this is relatively simple for someone who knows scripting better than I do but just need to be pointed in the right direction.

New Contributor III

Would something like this work? (Thanks, @el2493 )

exec 2>&1

# Get MacOSX idletime. Shamelessly stolen from
idleTime=$(/usr/sbin/ioreg -c IOHIDSystem | /usr/bin/awk '/HIDIdleTime/ {print int($NF/1000000000); exit}')

echo Idle Time is $idleTime seconds

if (( idleTime > 300 )); then
# Kill Firefox, Google Chrome, and Safari
sudo killall Firefox
sudo pkill "Google Chrome"
sudo pkill "Safari" 

#Launch Firefox browser
sudo open -a firefox
else exit 0

exit 0

Legendary Contributor II

Sure. Here's a simple example; something modified and adapted from a larger script I have.


"Google Chrome"

while read PROCESS; do
    ## Check the processlist output to see if that process is active
    PID_CHECK=$(/usr/bin/pgrep -x "${PROCESS}")
    if [ -n "$PID_CHECK" ]; then
        echo "Found PID for $PROCESS: $PID_CHECK"
        echo "Killing $PID_CHECK"
        /usr/bin/pkill "${PROCESS}"
        echo "PID for $PROCESS not found"
done < <(printf '%s
' "${PROCESS_LIST[@]}")

The idea is, you place the process names into that top array PROCESS_LIST and the script loops over each one, checking with pgrep to see if it finds the process ID for that app, and if it does, kills it.
There is of course a more graceful way to do this than using kill or pkill, such as using an osascript call to tell the applications to quit, but you'll face 2 issues with that. One, if using 10.14 or up you will need to make sure you have a proper PPPC profile in place to allow the AppleScript call to do anything without being stopped by the OS, and second, if the app has any unsaved data (possible in browsers with an open webform filled in) it will abort the quit asking the user if they want to close and lose the unsaved work. Since this is intended to run when the machine has been idle for a while, I assume there is no-one at the Mac to confirm such a prompt, so it will foil the script's attempt to close those browsers unattended in those cases.

The other potential problem you may face - when apps get forcefully quit, they sometimes come up with those same open windows/pages on next launch, or will ask if the previous pages should be restored. I know Firefox can do this. Not 100% certain on either Safari or Chrome, but they may as well. I don't think that's what you're looking for there, so you might have to experiment to see how such a script would fare for you.

New Contributor III

@mm2270 this is great, thank you! How would you integrate this into the script I posted above? I like what you've got here but am unsure how to get the "idle time" section, your addition, and the "open firefox" to work together as a singular script. Using what I posted above I get an "Script exit code: 0" and "Script result: Idle Time is 1966 seconds. No matching processes were found". It's like it doesn't see anything after the idle time is retrieved so it just exits. Any ideas?