I could not find a way of executing a script for the Guest account during login which executes every minute. They say using cron daemon is deprecated so it looks like I will be using launchd with .plist files.
Scenario:
I have a public iMac I want to allow the general public to use the guest account and have it force log out every half-hour. I wrote a ruby script to check the login time and figure out time remaining. I can have it display a banner notification every 10 minutes using osascript and then make it log off my account. Problem is when I try to implement it for the Guest account it doesn't work.
The problem is when I am placing the .plist file inside /Library/LaunchDaemons since it runs after login and also runs as root. Running as root is important since I can have the privilege to shut down processes when time runs out. I need it to execute once every minute. This it the current plist file that does work when I login as my own username "owner" but not guest. Using org.user.plist
My original .plist file looked something like this
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.user</string>
<key>Program</key>
<string>/usr/local/bin/notify-custom</string>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
Update 1 (Not a solution yet) .plist file which runs every 10 seconds for both Guest and my username
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.user</string>
<key>ProgramArguments</key>
<string>/usr/local/bin/notify-custom</string>
<key>WatchPaths</key>
<array>
<string>/Users/Guest/Library</string>
<string>/Users/owner/Library</string>
<array>
</integer>
</dict>
</plist>
As a test to make sure the osascript notification banner pops up I have this code inside /usr/local/bin/notify-custom
#/bin/bash
#Using whoami would have shown me logged in as root under LaunchDaemon .plist
loggedinUser=`finger | awk 'NR==3{print $1}'`
#I need to manually run terminal and type sudo as guest for nextline to work
sudo -u $loggedinUser /usr/bin/osascript -e 'display notification "Test" with title "Banner Notification"'
Solution is below.
Best Answer
In my opinion the following should work - it does in my VM! - launched as /Library/LaunchDaemons/org.user.plist:
As example task I use
/usr/bin/touch -f /Users/Guest/Desktop/test.txt
.The trick here is that the complete content of the Guest folder is deleted after a log out. After a new Guest logs in the whole content is recreated from scratch. As soon as the folder /Users/Guest/Library is created, the example task (
touch ...
) is started due to the WatchPaths key.Since the task/script/app has to be executed as guest you can't use launch agents because the path /Users/Guest/Library/LaunchAgents/ simply doesn't exist.
Use a launch daemon instead and run it as Guest/_guest. Your ruby script /usr/local/bin/notify-custom has to be world readable/executable? of course.
I also tried to run the task every 60 seconds - which works properly but throws some errors after the guest's log out. Probably it's better to implement the whole thing in the ruby script. Depending on your script your mileage may vary though.
If you have two different tasks to execute (e.g show a banner every 10 minutes with Ruby and a timer to force log out after 30 minutes) it's probably better to create two different launch daemons.