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

Run shell scripts at login or logout UNIX
Faced with having to manually delete two lock files created in /tmp by Apple's X11 that should be deleted when x11.app is quit, I needed to put remove commands into a script that would only be run when logging into or out of the console. This couldn't happen for individual shells (so using /etc/csh.login or /etc/csh.logout wouldn't work). I could not find the answer in OS X Unleashed or other good books.

Gary Karbaugh pointed out to me that you can add a -LogoutHook and/or a -LoginHook switch to the console line of the /etc/ttys file. The switch takes an argument that is a full path to whatever script you want to run when you logout. Apple documents this here with an example. In my case the script was to delete /tmp/.X0-lock and /tmp/.X11-unix/, both of which were preventing me from making available X11.app for more than one user, a mysterious bug that plagues some but not others.

I found I had to restart to get it to work, though I imagine there is a more elegant way.
    •    
  • Currently 2.33 / 5
  You rated: 2 / 5 (3 votes cast)
 
[15,213 views]  

Run shell scripts at login or logout | 6 comments | Create New Account
Click here to return to the 'Run shell scripts at login or logout' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Runs As Root
Authored by: escowles on Jan 16, '03 04:52:11PM

Too bad this runs as root, not as the user who's logging in/out. I even setup a script to see if the ownership for console was changed (it's not) or if there's anything in the environment that tells you which user it is (there isn't).

Does anybody know how to tell which user is logging in/out?

-Esme



[ Reply to This | # ]
Runs As Root
Authored by: pmccann on Jan 16, '03 05:54:11PM

Hmm, not sure what the state of play is when this script is run (ie, is the user still logged in and about to be logged out?), but if he or she is still listed as logged in you should be able to use something as simple as

who | grep console | cut -f 1 -d ' '

If the user is gone then all bets are off. Looking at the developer page linked to the original story I'm unfortunately going to have to bet on the latter ("-LogoutHook The full path to a script or tool to run when a user successfully logs out.") There: not *completely* useless am I? Errr...

Cheers,
Paul



[ Reply to This | # ]
Runs As Root
Authored by: bjjessup on Jan 16, '03 09:43:44PM

You can run a command as the user who logged in using the bash variable $1.

for instance chown $1 /Users/$1

will change the ownership of the logged in users home directory to themselves.



[ Reply to This | # ]
Runs As Root
Authored by: escowles on Jan 17, '03 10:37:33AM

Thanks for pointing this out. Once you've got the username, it's pretty easy to setup what I'd really like: getting a shell script to run at login. You can make an AppleScript to do this, but I would rather that it happen silently and for all users who create the appropriate script in their home dir.

The following is a Perl script that will run $USER/bin/LoginScript when they login -- it will run the script as that user, not as root:

---------- CUT HERE ----------
#!/usr/bin/perl

$user = $ARGV[0];
@UINFO = getpwnam( $user );
$home = $UINFO[7];
$script = "$home/bin/LoginScript";

if ( -x $script )
{
exec( "su - $user -c $script" );
}
---------- CUT HERE ----------



[ Reply to This | # ]
Runs As Root
Authored by: inode on Mar 08, '03 06:03:54AM

echo $LOGNAME



[ Reply to This | # ]
You can also just use .login
Authored by: wilkens on Mar 16, '03 08:56:27PM
Seems like the following should work - I use it to run a simple script when I log in and it works for me:

  1. In your home directory, create a text file named .login
  2. Begin the file like any shell script: #!/bin/sh
  3. Put the rest of your script in this file, or call other scripts from it
  4. Make .login executable by typing chmod u+x .login from the command line
That's it. The .login script is executed every time you log in (either from the console or over the network via ssh), but not when you start up another terminal or a new shell. And it runs as the user logging in, not as root.

Hope that helps!

[ Reply to This | # ]