Offset doesn't seem to trigger scripts in 10.13 on logoff
Deployed offset and my offset scripts to a 10.13 client just like I had previously done with 10.11 and 10.12. Doesn't seem to work. /var/log/offset just says the following on each logoff:
02/08/2018 02:50:55 PM - INFO: Skipping logout scripts, because login state is loggedIn and should be loggedOut or Restart
Offset scripts work when run directly or through a custom loginwindow launch agent by themselves.
Yeah, just noticing this myself. Hadn't had a chance to test this before. I'll have to do some more digging...
I suspect it's this logic that somehow broke in 10.13: https://github.com/aysiu/offset/blob/master/offset#L200-L210
I thought it might have to do with the output of the last command, but they seem identical to me. Hm.
Here's an example from 10.12:
USERNAME1 ttys000 192.168.0.1 Mon Mar 26 08:34 still logged in
reboot ~ Mon Mar 26 08:33
shutdown ~ Mon Mar 26 08:33
And here's an example from 10.13:
USERNAME1 ttys000 192.168.0.2 Mon Mar 26 08:29 still logged in
reboot ~ Mon Mar 26 06:00
shutdown ~ Sun Mar 25 22:00
Okay, so I did a little testing, and it's working for a straight reboot, a shutdown and startup, and a logout and immediatley reboot... which is all to say it will work at the login screen after a reboot.
The only scenario it's not working on right now is for 10.13 for a regular logout.
It did seem able to parse out the last | head -n3 output okay. I'll have to examine it more closely to see if I can figure out what it's not seeing:
[['username', 'console', 'Mon', 'Mar', '26', '09:52', '-', '09:53', '(00:00)'], ['reboot', '~', 'Mon', 'Mar', '26', '09:52'], ['shutdown', '~', 'Mon', 'Mar', '26', '09:51'], []]
last output wasn't where it was at. I should have looked more closely at the logic of my own script. The problem is that when you log out of 10.13, the last user (from com.apple.loginwindow.plist) is still showed as loggedIn.
Turns out it's a timing thing. In 10.13 the status doesn't change from loggedIn to loggedOut immediately when you log out. It takes anywhere between 1 second and 2 seconds. I'll see if I can find the shortest amount of time to wait.
1.7 seconds seems to be the "magic" length of time to wait before running the script. It's a super ugly hack. Yes, in real life it'd be very rare for a user to log out and then log back in in less than 1.7 seconds, but it's still a possible scenario, and so this wouldn't work in that scenario.
Seems the wait is variable. Sometimes 1.7 is enough. Sometimes you need 1.9 seconds. Sometimes it's even slightly over 2 seconds. Yikes!
Fixed in the latest release. Unfortunately, 1.7 is not the magic length of time. The time seems to be arbitrarily variable. Went with 2.5 seconds, which seemed to cover a lot of scenarios. https://github.com/aysiu/offset/releases/tag/1.3.2
Doing some more testing, it seems the time delay may even have to be as high as 4 seconds. Yikes.
4 seconds: https://github.com/aysiu/offset/releases/tag/1.3.3
I'm sorry but this still seems to be an issue. I will say it works sporadically now, but not enough to count on it. Still getting this in the log when it doesn't work: 04/13/2018 05:19:01 PM - INFO: Skipping logout scripts, because login state is loggedIn and should be loggedOut or Restart
Computer I tested with didn't have offset on it prior. Downloaded and installed version 1.3.3 and dropped a tester script in the logout-every folder. Initially it worked, but after about two works days of keeping an eye on it it doesn't work more often then not. offset-share.log
Well, it doesn't seem there's a definite time delay that will account for all instances. In order to be effective, this might have to be turned into login-screen scripts instead of logout scripts.
I'd love to hear people's input on a possible restructuring, which would slightly change functionality and expected behavior but would provide more consistently predictable behavior based on Apple's changes.
Since I initially had this problem thats what I've been doing to keep things running in 10.13. Just a LaunchAgent which runs everything /usr/local/offset/logout-every/*.sh. Not as elegant as offset and it causes the items to get run more then they are needed, but it still does the job.
I've done some testing, and the output of defaults read /Library/Preferences/com.apple.loginwindow lastUser seems to be correct immediately at logout for 10.13. Not sure why it doesn't register.
Since I got no input from people on how to fix this, I've put in my own dirty workaround for 10.13: https://github.com/aysiu/offset/releases/tag/1.4.0
Finally got around to testing it on 10.14, and it has the same problem as 10.13, so I may have to modify this so that instead of looking for 10.13 as an exception, it looks for 10.13 or greater.
Apparently, there's a better way to determine whether you're at the login screen or not (thanks, @poundbanghash). I'm not sure when I'll have the bandwidth to test and implement. If someone wants to test and make a pull request, I'd be super appreciative. Otherwise, I'll get to it when I get to it.
'''Get the current logged in user'''
import subprocess
from SystemConfiguration import SCDynamicStoreCopyConsoleUser
import sys
def fact():
'''Get the current logged in user'''
try:
username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]
username = [username,""][username in [u"loginwindow", None, u""]]
except (IOError, OSError):
pass
if username == '':
return {'isUserLoggedIn': False}
else:
return {'isUserLoggedIn': True}
I have to give this a bit more thought. The issue isn't really determining that the user isn't logged in. The whole point is that the launch agent triggers Offset at the login screen. It's that the old way of determining whether it was a logout or a reboot, I think the prevent Offset from double-running if you logged out and then shut down. I have to go back through my notes and see what would be the best way to deal with the breakage that happened in 10.13. and 10.14. The workaround will have to work in the meantime.