Find the answer to your Linux question:
Results 1 to 5 of 5
I would like to run a command when a user logins in and another one when they logout. I saw in another thread that this can be done with the ...
  1. #1
    Just Joined!
    Join Date
    Jul 2008
    Posts
    17

    Trap Command Run On User Login or Logout?

    I would like to run a command when a user logins in and another one when they logout. I saw in another thread that this can be done with the trap command, I am wondering if I wanted to run the command "gimp" for example what would I write for the login and for the logout, also where would I place the trap code?

    Thanks for any help

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    You're talking about bash shells scripting, right?

    You can write a bash shell script which is automatically run every time you log in. You can also use the alias command to cause bash to run a different script every time you type the exit command.

    For more information about bash, and bash scripting, google this:
    Code:
    bash tutorial
    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    Jul 2008
    Posts
    17
    Ok thanks, yeah I wasn't sure if bash could do this I will look through the tutorials to learn more about this, I figure it can't be that difficult

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    To the best of my knowledge, trap only allows you to catch signals. I'm not sure how you would use it to execute commands on login or logout.

    However, it turns out that Bash has this ability. Any commands added to ~/.bashrc will be executed whenever a new shell is started, and any commands in ~/.bash_logout are executed when a user's shell exits.

    For instance, my .bashrc sets a lot of my environment variables and creates some aliases for me, while my .bash_logout clears the screen, so that anyone who logs on after me can't see anything I did.
    DISTRO=Arch
    Registered Linux User #388732

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Any commands added to ~/.bashrc will be executed whenever a new shell is started
    ... unless that shell is started because you're just logging in, according to the man page I read and the bash that I'm running.

    When you first log in, bash runs /etc/profile, if that file exists. Then it executes the first of these files that it finds in your home directory, if any:
    Code:
    .bash_profile
    .bash_login
    .profile
    To be technically accurate, "when you first log in" means "whenever you run bash with the first character of argument 0 being "-", and "whenever you run bash with the --login option". This is normally done for you when you log in the normal way.

    Then, if you type "bash" at the bash command line, you'll get a new shell. (There are other ways of getting a new shell also.) Those are typically not considered login shells, and they will execute .bashrc in your home directory, if it exists.

    Many people, to avoid confusion, have this line in their home directory's .bash_profile, .bash_login, or .profile file:
    Code:
    . ~/.bashrc
    or
    Code:
    source ~/.bashrc
    SIde note: I have just verified all of this by testing, except that bash doesn't seem to read ~/.profile for me, even if both ~/.bash_profile and ~/.bash_login are missing. Go figure.

    I hope I haven't confused anyone. :)
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...