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 ...
- 07-11-2008 #1Just 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
- 07-11-2008 #2
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:
Hope this helps.Code:bash tutorial
--
Bill
Old age and treachery will overcome youth and skill.
- 07-11-2008 #3Just 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
- 07-12-2008 #4
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
- 07-12-2008 #5... 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.Any commands added to ~/.bashrc will be executed whenever a new shell is started
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:
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.Code:.bash_profile .bash_login .profile
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:
orCode:. ~/.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.Code:source ~/.bashrc
I hope I haven't confused anyone. :)--
Bill
Old age and treachery will overcome youth and skill.


Reply With Quote