Go Back   The macosxhints Forums > OS X Help Requests > UNIX - General



Reply
 
Thread Tools Rating: Thread Rating: 7 votes, 5.00 average. Display Modes
Old 01-21-2002, 09:37 PM   #1
stephenfleming
Prospect
 
Join Date: Jan 2002
Location: Atlanta, Georgia
Posts: 13
Unhappy 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
stephenfleming is offline   Reply With Quote
Old 01-22-2002, 12:17 AM   #2
ginoledesma
Prospect
 
Join Date: Jan 2002
Location: Quezon City, Philippines
Posts: 3
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.
ginoledesma is offline   Reply With Quote
Old 12-18-2007, 12:07 PM   #3
Tuxford
Triple-A Player
 
Join Date: Mar 2006
Posts: 97
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.
Tuxford is offline   Reply With Quote
Old 12-19-2007, 09:08 AM   #4
anika123
All Star
 
Join Date: Sep 2006
Posts: 860
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.
anika123 is offline   Reply With Quote
Old 12-19-2007, 09:38 AM   #5
cwtnospam
League Commissioner
 
Join Date: Jan 2005
Posts: 8,475
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.
cwtnospam is offline   Reply With Quote
Old 12-19-2007, 09:49 AM   #6
anika123
All Star
 
Join Date: Sep 2006
Posts: 860
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.
anika123 is offline   Reply With Quote
Old 12-19-2007, 10:06 AM   #7
cwtnospam
League Commissioner
 
Join Date: Jan 2005
Posts: 8,475
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.
cwtnospam is offline   Reply With Quote
Old 12-19-2007, 11:15 AM   #8
anika123
All Star
 
Join Date: Sep 2006
Posts: 860
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?
anika123 is offline   Reply With Quote
Old 12-19-2007, 11:41 AM   #9
cwtnospam
League Commissioner
 
Join Date: Jan 2005
Posts: 8,475
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.
cwtnospam is offline   Reply With Quote
Old 12-19-2007, 01:34 PM   #10
Hal Itosis
Hall of Famer
 
Join Date: Apr 2002
Posts: 3,315
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-
Hal Itosis is offline   Reply With Quote
Old 12-19-2007, 02:45 PM   #11
anika123
All Star
 
Join Date: Sep 2006
Posts: 860
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.
anika123 is offline   Reply With Quote
Old 12-19-2007, 02:48 PM   #12
Tuxford
Triple-A Player
 
Join Date: Mar 2006
Posts: 97
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.
Tuxford is offline   Reply With Quote
Old 12-19-2007, 03:15 PM   #13
cwtnospam
League Commissioner
 
Join Date: Jan 2005
Posts: 8,475
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
cwtnospam is offline   Reply With Quote
Old 12-19-2007, 03:42 PM   #14
anika123
All Star
 
Join Date: Sep 2006
Posts: 860
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.

Last edited by anika123; 12-19-2007 at 03:46 PM.
anika123 is offline   Reply With Quote
Old 12-19-2007, 05:20 PM   #15
Tuxford
Triple-A Player
 
Join Date: Mar 2006
Posts: 97
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?
Tuxford is offline   Reply With Quote
Old 12-19-2007, 05:43 PM   #16
cwtnospam
League Commissioner
 
Join Date: Jan 2005
Posts: 8,475
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.

Last edited by cwtnospam; 12-19-2007 at 05:48 PM.
cwtnospam is offline   Reply With Quote
Old 12-19-2007, 06:04 PM   #17
Tuxford
Triple-A Player
 
Join Date: Mar 2006
Posts: 97
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.

Last edited by Tuxford; 12-19-2007 at 06:06 PM.
Tuxford is offline   Reply With Quote
Old 12-19-2007, 06:13 PM   #18
cwtnospam
League Commissioner
 
Join Date: Jan 2005
Posts: 8,475
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.
cwtnospam is offline   Reply With Quote
Old 12-19-2007, 06:32 PM   #19
Tuxford
Triple-A Player
 
Join Date: Mar 2006
Posts: 97
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.
Tuxford is offline   Reply With Quote
Old 12-19-2007, 06:35 PM   #20
cwtnospam
League Commissioner
 
Join Date: Jan 2005
Posts: 8,475
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
cwtnospam is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 02:23 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.