The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   UNIX - General (http://hintsforums.macworld.com/forumdisplay.php?f=16)
-   -   Automatic Logout (http://hintsforums.macworld.com/showthread.php?t=112)

stephenfleming 01-21-2002 10:37 PM

Automatic Logout
 
I'm trying to craft a Unix command line that I can add to crontab to execute a graceful Log Out of the Macintosh environment. Basically, I want to be able to walk away from my machine at night (running a backup or something), but be sure that everything gets tidied up and is ready for me to login the next morning.

This should be incredibly simple. I can't figure out how to do it either in AppleScript or from the command line. Any suggestions? Thanks!

Stephen Fleming

ginoledesma 01-22-2002 01:17 AM

Off the top of my head I'd use kill, but that isn't necessarily gracious. If the application gets a SIGTERM or SIGQUIT, it should try to quit gracefully.

With regards to an AppleScript doing a QUIT, the issue I can think of is applications that ask for a "confirmation" when quitting, such as browsers asking for a confirmation while downloads are in progress, or documents that need saving. The Finder logout eventually times out when no response is given.

Tuxford 12-18-2007 01:07 PM

Has this issue been solved in recent OSX releases? Set my server to login as public user. I manually login as an admin user. Want to ensure that when I walk away, the admin user will eventually automatically logout.

anika123 12-19-2007 10:08 AM

I am no expert but you should be able to write in your scripts to modify a certain file, say a text file or something. Then set up launchd to monitor this file for changes. When it detects a change it will call the applescipt you have written that uses "system events" to logout or shutdown or whatever you want. So long as it is in the system events dictionary.
This could all be done (the backup and stuff) from a single applescript.

cwtnospam 12-19-2007 10:38 AM

If you're going to walk away, and you want it to log out, why not schedule it to shutdown in the Energy Saver preference pane?

If you want to walk away leaving the computer running but require a password to get back in, then set your screen saver to require one. While you're at it, make your screen saver something useful like folding@home.

anika123 12-19-2007 10:49 AM

Quote:

If you're going to walk away, and you want it to log out, why not schedule it to shutdown in the Energy Saver preference pane?
This would work just fine but I think what stephenfleming wants to do is run a backup script that may not be done when the scheduler decides to shutdown. Or, he wants to save the most amount of energy possible and wants his computer to shutdown promptly after the backup.

cwtnospam 12-19-2007 11:06 AM

Well, if he wants it to shutdown, the Terminal command is:

shutdown -h now

Simply logging out is another story, in which case I think it would be much more productive to use a distributed computing screen saver and set it to require a password. ;)

anika123 12-19-2007 12:15 PM

Quote:

shutdown -h now
That actually seemed to work but when I restarted; the session from the previous shutdown seemed to be active. Also, Camino acted like it had been force quit. Are you sure this works for a clean osx shutdown?

cwtnospam 12-19-2007 12:41 PM

The man page is dated December 11, 1998 so developers should be aware of it. I did see the same problem with Camino though.

Of course, if you're going to put this in a script, it would be best to close all applications before using the command.

Oh, just in case some one has a legitimate need to logout instead of shutdown — although I can't think of one — the command key for logout is command-shift-Q. If you want it to take affect immediately, it's command-option-shift-Q. That should be easy enough to GUI script in Applescript.

Hal Itosis 12-19-2007 02:34 PM

I don't remember the syntax offhand, but in AppleScript there is something like:

shutdown without saving

[I forget exactly... but it is slightly more "graceful".]

-HI-

anika123 12-19-2007 03:45 PM

Wow, was this thread really started in 2002? I doubt stephenfleming even cares anymore. But the info is still good for newcomers and interested parties.

Tuxford 12-19-2007 03:48 PM

Since the machine is server, I want it always running. Since it runs some server applications, I have it login automatically as a standard user.

To get to my private shared folders, I login as a second user sometimes, and then walk away. I want this second user to automatically logout after a period of time. Can someone consolidate these ideas into a strategy for me? Thanks.

cwtnospam 12-19-2007 04:15 PM

You could save this as a stay open application and put it in your startup items. After about 10 seconds of inactivity, the 2:00 minute warning for logout will popup. You can cancel the logout if you're there, and it will popup after the next inactive period.

Code:

on idle
        delay 10
        tell application "System Events"
                key down shift
                key down command
                keystroke "q"
                key up shift
                key up command
        end tell
end idle


anika123 12-19-2007 04:42 PM

I think what you want is a logout script. This is way beyond my means but this might be a relevant link http://managingosx.wordpress.com/200...aunchd-gotcha/. Anyway a logout hook is apparently what you need. I would hope that applescript could do this. Where is tw when you need him. Anyone one else with logout hook experience?

The solution by cwtnospam looks like a good solution as well.

Tuxford 12-19-2007 06:20 PM

Great. Got the script working. Would be nice if I could also then automatically log back into the other standard user. This script just gets me to the login name screen. The other user is typically running in the background anyway. I wonder if it's energy saver settings would kick in and dim the screen at this login window?

cwtnospam 12-19-2007 06:43 PM

Ok, then based on the code from post #9 in this thread, let's do this:

Code:

on idle
        delay 10
        display dialog "About to switch users..." giving up after 10
        do shell script "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -switchToUserID 502"
        delay 2
        tell application "System Events"
                keystroke "their password here"
                delay 1
                keystroke return
        end tell
end idle

You need to enter the correct userID number. If your standard account ID isn't 502, change it to the correct one!

Edit: Note that providing the password in the script isn't secure. Since it's a standard account and you auto login anyway, it shouldn't be a huge problem. ;)

Tuxford 12-19-2007 07:04 PM

This script gets stuck after the password entry, on Return. Ends up looping back at the About to switch warning. Need to click the login button. Also wonder if need to disable this script when user is in the background after switching.

cwtnospam 12-19-2007 07:13 PM

Two things that could cause that problem:
1) The user id isn't correct.
2) The password isn't correct.

I don't think the script needs to be disabled when in the background. When I tried it everything seemed ok.

Tuxford 12-19-2007 07:32 PM

After the script runs and gets stuck in the loop, I simply click the login button, and it switches user. So the user id and password are correct.

Seems to switch to the Finder (or maybe to the looped script) before the Return is recognized.

cwtnospam 12-19-2007 07:35 PM

Ok, I was able to duplicate the problem. I think this should take care of it:

Code:

on idle
        delay 10
        tell application "Finder"
                activate
                display dialog "About to switch users..." giving up after 10
        end tell
        do shell script "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -switchToUserID 502"
        delay 3
        tell application "System Events"
                keystroke "password"
                delay 1
                keystroke return
                keystroke return
        end tell
end idle



All times are GMT -5. The time now is 05:42 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.