How to run script using cron or launchd for Guest user account in El Capitan 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

&lt;?xml version="1.0" encoding="UTF-8"?&gt; 
&lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt; 
&lt;plist version="1.0"&gt; 
&lt;dict&gt; 
    &lt;key&gt;Label&lt;/key&gt; 
        &lt;string&gt;org.user&lt;/string&gt; 
    &lt;key&gt;Program&lt;/key&gt; 
        &lt;string&gt;/usr/local/bin/notify-custom&lt;/string&gt; 
    &lt;key&gt;RunAtLoad&lt;/key&gt; 
        &lt;true/&gt; 
&lt;/dict&gt; 
&lt;/plist&gt;


Update 1 (Not a solution yet) .plist file which runs every 10 seconds for both Guest and my username

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
&lt;plist version="1.0"&gt;
&lt;dict&gt;
    &lt;key&gt;Label&lt;/key&gt;
        &lt;string&gt;org.user&lt;/string&gt;
    &lt;key&gt;ProgramArguments&lt;/key&gt;
        &lt;string&gt;/usr/local/bin/notify-custom&lt;/string&gt;
    &lt;key&gt;WatchPaths&lt;/key&gt;
        &lt;array&gt;        
            &lt;string&gt;/Users/Guest/Library&lt;/string&gt;
            &lt;string&gt;/Users/owner/Library&lt;/string&gt;
        &lt;array&gt;
    &lt;/integer&gt;
&lt;/dict&gt;
&lt;/plist&gt;


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.
In my opinion the following should work - it does in my VM! -  launched as /Library/LaunchDaemons/org.user.plist:

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
&lt;plist version="1.0"&gt;
&lt;dict&gt;
    &lt;key&gt;Label&lt;/key&gt;
    &lt;string&gt;org.user&lt;/string&gt;
    &lt;key&gt;ProgramArguments&lt;/key&gt;
    &lt;array&gt;
        &lt;string&gt;/usr/bin/touch&lt;/string&gt;
        &lt;string&gt;-f&lt;/string&gt;
        &lt;string&gt;/Users/Guest/Desktop/test.txt&lt;/string&gt;
    &lt;/array&gt;
    &lt;key&gt;UserName&lt;/key&gt;
    &lt;string&gt;Guest&lt;/string&gt;
    &lt;key&gt;GroupName&lt;/key&gt;
    &lt;string&gt;_guest&lt;/string&gt;
    &lt;key&gt;InitGroups&lt;/key&gt;
    &lt;true/&gt;
    &lt;key&gt;WatchPaths&lt;/key&gt;
    &lt;array&gt;
        &lt;string&gt;/Users/Guest/Library&lt;/string&gt;
    &lt;/array&gt;
&lt;/dict&gt;
&lt;/plist&gt;


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.