Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Log out background users after time limit System
Fast User Switching has a lot of benefits, but one of its downsides is that it is easy to end up with multiple users in the background wasting system resources and needlessly degrading performance. This hint shows how to implement a system to log out users automatically if they have been sent to the background for a specified period of time.

In order to keep the benefits of Fast User Switching while avoiding the situation where multiple users remain logged in unnecessarily, it is necessary to have a way to cause users to log out automatically after being in the background for a period of time. Unfortunately, Mac OS X does not provide a way to do this. (There is a setting in the Security preference pane for logging out automatically after a certain amount of idle time, but it logs out all users and only does so when no one uses the computer for the specified period of time; so long as anyone is using the computer, all background users stay logged in.)

My solution is to use launchd to make each user keep track of whether they are in the background or not, and to log out if they have been in the background for a while. To do this, save the following as a file named com.yourname.backgroundUserLogout.plist in the LaunchAgents folder of each user's Library folder:
<?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>com.yourname.backgroundUserLogout</string>
 <key>ProgramArguments</key>
  <array>
    <string>/bin/bash</string>
    <string>-c</string>
    <string>if [ $(stat -f %u /dev/console) == $UID ]; then if [ -e /tmp/backgroundUserLogout.$UID ]; then rm /tmp/backgroundUserLogout.$UID; fi; elif [ ! -e /tmp/backgroundUserLogout.$UID ]; then touch /tmp/backgroundUserLogout.$UID; elif [ $(( `date +%s` - `stat -f %m /tmp/backgroundUserLogout.$UID || printf 0` )) -ge $(( 60 * 30 )) ]; then rm /tmp/backgroundUserLogout.$UID; osascript -e 'tell application "System Events" to log out'; fi</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>StartInterval</key>
  <integer>60</integer>
</dict>
</plist>
This launch agent first checks to see whether the user owns the console device (that is, whether it has control of the GUI), and if not it keeps track of how long this has been the case by using a temporary file. Once the user has been in the background for 30 minutes (in the example code above), the launch agent will automatically send the signal to log the user out. Of course, if the user has unsaved documents, the log out will be aborted.

To use a different number of minutes to wait, simply find the part in the launch agent's code that says $(( 60 * 30 )) and replace the 30 with another number of your choosing. If you have any users that should never be automatically logged out (for example, a user whose job is to serve up the communal iTunes library), simply don't create this launch agent in their Library.

[crarko adds: I haven't tested this one.]
    •    
  • Currently 3.55 / 5
  You rated: 1 / 5 (22 votes cast)
 
[13,729 views]  

Log out background users after time limit | 19 comments | Create New Account
Click here to return to the 'Log out background users after time limit' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Log out background users after time limit
Authored by: mkuron on Jun 07, '10 08:15:33AM
To do this, save the following as a file named com.yourname.backgroundUserLogout.plist in the

LauchAgents folder of each user's Library folder

You should be able to just place it in the root-level LaunchAgents folder (/Library/LaunchAgents). LaunchAgents always get run inside a user's session, upon login (in contrast, root-level LaunchDaemons by default run in the loginwindow context).

I haven't tested this specificially for your hint, but in my other work with LaunchAgents, I haven't noticed any difference between it being placed in the user-level LaunchAgents folder and the all-users LaunchAgents folder.



[ Reply to This | # ]
Log out background users after time limit
Authored by: Sesquipedalian on Jun 07, '10 08:22:13AM

Absolutely true. But I needed different settings for different users, so I had to do it this way instead.



[ Reply to This | # ]
Log out background users after time limit
Authored by: mkuron on Jun 07, '10 08:35:21AM

Ok. Still I think it's worth mentioning: having a global LaunchAgent would probably be very useful for people managing Macs with large numbers of (network) users so that copying one to every individual user's folder would simply be impractical. In such a scenario, you could still individualize the script based on group membership - e.g. if you want to have individual logout times:

if [ $(id $UID | grep "80(admin)") = "" ]; then logouttime=60; else logouttime=1440; fi;

Adding this to the beginning of your third ProgramArguments string and replacing $(( 60 * 30 )) with $logouttime would give you a 1-hour logout wait time for non-admins and a 24-hour wait time for admin users.



[ Reply to This | # ]
Log out background users after time limit
Authored by: Sesquipedalian on Jun 07, '10 08:55:30AM

Very nice!

(Although you'd want to use 3600 and 86400 instead of 60 and 1440.)

Edited on Jun 07, '10 09:01:50AM by Sesquipedalian



[ Reply to This | # ]
Log out background users after time limit
Authored by: Azur Logix on Aug 30, '10 03:24:06PM

I put this in /Library/LaunchAgents but I'm guessing I must not have properly integrated your code into the above hints code.

<?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>com.global.backgroundUserLogout</string>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>-c</string>
<string>if [ $(id $UID | grep "80(admin)") = "" ]; then logouttime=60; else logouttime=1440; fi; if [ $(stat -f %u /dev/console) == $UID ]; then if [ -e /tmp/backgroundUserLogout.$UID ]; then rm /tmp/backgroundUserLogout.$UID; fi; elif [ ! -e /tmp/backgroundUserLogout.$UID ]; then touch /tmp/backgroundUserLogout.$UID; elif [ $(( <code>date +%s</code> - <code>stat -f %m /tmp/backgroundUserLogout.$UID || printf 0</code> )) -ge $logouttime ]; then rm /tmp/backgroundUserLogout.$UID; osascript -e ‘tell application “System Events” to log out’; fi</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StartInterval</key>
<integer>60</integer>
</dict>
</plist>



[ Reply to This | # ]
Log out background users after time limit
Authored by: Fairly on Jun 13, '10 01:57:33PM

Yes, but then it's not user-specific, and he specifically wanted to make it user-specific.



[ Reply to This | # ]
Log out background users after time limit
Authored by: horhey23 on Nov 21, '10 05:25:09PM

What would one name the plist file if using your suggestion to set this up for all users in the root folder? Just remove the "username" part?



[ Reply to This | # ]
Log out background users after time limit
Authored by: Sesquipedalian on Jun 07, '10 08:18:43AM
It looks like GeekLog inserted some unnecessary spaces into the XML posted above. These should be removed, or else it probably will not work.

For example,

<key>Label </key>
should be
<key>Label</key>
. It looks like this has happened before every
</***>
bit in the XML.

[ Reply to This | # ]
Log out background users after time limit
Authored by: crarko on Jun 07, '10 08:35:17AM

Thanks! I'll fix that.

~Craig



[ Reply to This | # ]
Log out background users after time limit
Authored by: SeanAhern on Jun 07, '10 05:20:53PM

One more typo. LauchAgents should be LaunchAgents.



[ Reply to This | # ]
Log out background users after time limit
Authored by: crarko on Jun 07, '10 06:08:08PM

Fixed. Thank you!



[ Reply to This | # ]
Log out background users after time limit
Authored by: poenn on Jun 08, '10 12:29:05AM

As a German I had to laugh about the LauchAgent: http://de.wikipedia.org/wiki/Lauch
:-)



[ Reply to This | # ]
Log out background users after time limit
Authored by: conundrum on Jun 08, '10 06:15:06AM

Thanks for that, it was very funny.



[ Reply to This | # ]
Log out background users after time limit
Authored by: scott997 on Jun 12, '10 04:16:11AM

This does not work for me. I've put the file in username/Library/LaunchAgents. I changed "yourname" in both the filename and in the file to the user's name. The user does not have any windows open, but might have a few applications running. The user is not an admin.

How do I even trouble shoot this?

Thanks.

Scott



[ Reply to This | # ]
Log out background users after time limit
Authored by: Sesquipedalian on Jun 13, '10 08:13:28AM

Make sure the permissions are correct on the .plist file. Launch agents in ~/Library/LaunchAgents must be owned by the user who owns ~, or they will be skipped as a potential security problem. If you used your administrator account to create the file in the non-admin account, the file will be owned by the admin rather than the non-admin.

The easiest way to solve this problem is to log in as the user in question, make a duplicate copy of the file, and then throw the original away.



[ Reply to This | # ]
Log out background users after time limit
Authored by: scott997 on Jun 22, '10 07:19:21PM

Thanks for the fix, it works now. But it seems to cause a problem. When I am logged in, the LaunchAgent will log out the other user after 30 minutes. But when I try to log out I get a blue screen with a spinning line. The computer never recovers. Any ideas?

Thanks



[ Reply to This | # ]
Log out background users after time limit
Authored by: Bakari on Jul 29, '10 10:04:40AM

I want to try this, but when I try to save the script in TextEdit, it says that I can’t save it just as a plist file. Rtf has to be added to it. Is there something I’m doing wrong?



[ Reply to This | # ]
Log out background users after time limit
Authored by: JH on Aug 22, '11 08:00:32AM

This worked for me. But now I have a problem! I wanted to undo this action. I deleted the plist file, but the behavior still exists. Then I realized I should have issued the launchctl remote... command first and then deleter the plist file. But too late now.

The system STILL logs me off after 10 mins of inactivity - aaagh!

Could someone please advise on how I can unload this ghost??

Thanks




[ Reply to This | # ]
Log out background users after time limit
Authored by: Ade_Hints on Dec 15, '11 10:00:10PM

I cannot find the LaunchAgents folder for the kids' user logons



[ Reply to This | # ]