Join 3,424 readers in helping fund MetaFilter (Hide)


Bash, I command thee to always...
October 22, 2007 4:27 PM   Subscribe

How do I get bash to automagically execute a line or two of code at login?

I ssh into various web servers all day long. They're helpfully *cough* configured to log me off after a few minutes inactivity, so re-logging in happens a lot. Then the re-return to the correct working directory. Where do I put something like 'cd ~/foo/bar' to instruct bash to execute that step (and whatever other convenience steps I can think of) automatically at shell login? I tried just dropping that into ~/.bash_profile, but no dice. 'chmod +x ~/.bash_profile' didn't make a difference.
posted by nakedcodemonkey to Computers & Internet (25 answers total) 5 users marked this as a favorite
 
isn't it .bashrc?
posted by boo_radley at 4:32 PM on October 22, 2007


.bashrc it is

and you want to read up more about fun bash stuff to help you save time:

Terminal Productivity Tips
posted by mrzarquon at 4:35 PM on October 22, 2007


I background this little Perl script at login, and my sessions never get logged off:

#!/usr/bin/perl

$|++;
while (1) { print "\e[0n"; sleep 60; }


posted by nicwolff at 4:45 PM on October 22, 2007


Screen is a $diety-send for this kind of situation. Complete virtual consoles with nice detach and reattach abilities!

(You can also send a keepalive w/ ssh by adding -o TCPKeepAlive=yes to the ssh command or by editing ssh conf)
posted by Cat Pie Hurts at 4:47 PM on October 22, 2007 [1 favorite]


.bash_login ?

beware the interaction between .bash_login, .bashrc, and .bash_profile . See the man page.
posted by cmiller at 4:54 PM on October 22, 2007


Wait, it logs you off? is the "$TIMEOUT" variable set? What if you unset it?
posted by cmiller at 4:55 PM on October 22, 2007


Hmm. I got an empty response to 'print $TIMEOUT' on the account that's logged in now. Do hosting companies usually let the user alter their timeout? I just assumed it was an admin-dictated housekeeping measure.

Assigning each account a different color is bloody brilliant. Damn. Thanks!

Okay, tried putting it in .bash_login (then .bashrc) instead. No joy there either.
posted by nakedcodemonkey at 5:26 PM on October 22, 2007


This may not be a direct answer, but I second the GNU screen suggestion. It will change your life (or at least greatly enhance the way you deal with shells). When I started, I made a little screen cheatsheat and just pasted it next to the monitor.
posted by spiderskull at 5:38 PM on October 22, 2007


I hereby concur with the first responder, known as boo_radley. The file in your home directory, .bashrc will do your bidding.
posted by EastCoastBias at 5:46 PM on October 22, 2007


It is .profile on most unix systems. This can be configured by the sysadmin, so you might not even have the ability to alter your login stuff.

Also, it is usually $TMOUT not $TIMEOUT (in my experience, maybe some distros of linux, or BSD or something use $TIMEOUT). If someone is setting $TMOUT for you, they are probably setting it read-only, so you couldn't unset it if you wanted to. You could give a try though.
posted by synaesthetichaze at 5:47 PM on October 22, 2007


Oh, also (and forgive me if this sounds patronizing), are you logging out after you alter these files? Because it won't take effect otherwise (obviously). Just making sure...
posted by synaesthetichaze at 5:49 PM on October 22, 2007


The file in your home directory, .bashrc will do your bidding.

But it didn't. (Yep, logging out then back in.)
posted by nakedcodemonkey at 5:58 PM on October 22, 2007


What specific OS are you using? Bash startup is configured slightly differently on each system I've seen.

It could be .bashrc, .bash_profile, .profile


Dumb question.. are you sure you are using bash as the default shell?
posted by TravellingDen at 6:30 PM on October 22, 2007


~ echo $SHELL
/bin/bash

posted by nakedcodemonkey at 6:32 PM on October 22, 2007


Don't know the specific answer, but thirding screen.
posted by pompomtom at 6:36 PM on October 22, 2007


env doesn't seem to have an entry for OS. How do I get that?
posted by nakedcodemonkey at 6:37 PM on October 22, 2007


env doesn't seem to have an entry for OS. How do I get that?

uname -a
posted by esch at 7:03 PM on October 22, 2007


Victory! The answer was .profile, at least on this machine. I'll have to do some trial and error with the others too.

How do I determine OS for each, and is there a cheatsheet that would determine file to use for each?

Thanks all for the screen recs. I've been reading the tutorials and experimenting.
posted by nakedcodemonkey at 7:04 PM on October 22, 2007


Thanks, esch. Turns out that one's Linux 2.4.32. So, Linux > .profile. Duly noted.
posted by nakedcodemonkey at 7:12 PM on October 22, 2007


>>are you logging out after you alter these files? Because it won't take effect otherwise

You can make it take effect without logging out by using the "source" command, I believe.
source ~/.bashrc
for instance.
posted by AmbroseChapel at 7:42 PM on October 22, 2007


I background this little Perl script at login, and my sessions never get logged off:

The real answer is to talk to the adminstrator and ask for a timeout extension. Don't go behind the admin's back when it comes to security measures.
posted by Mikey-San at 8:22 PM on October 22, 2007


I was referring to the bit I quoted and any other methods of extending the timeout itself artificially. Screen is a really great option here if the admin can't be moved!
posted by Mikey-San at 8:23 PM on October 22, 2007


/etc/profile, ~/.bash_profile and ~/.profile are run once, when you log in. ~/.bashrc is run for every non-login shell, e.g. when you start an xterm. That's why changing .bashrc doesn't work if you log out and then back in.

This is so that you can have things like log-in messages in your .profile, that you don't need to see more than once in a session.

There are more details, and the sequence can be overriden with command-line arguments, read the man page for more.
posted by the number 17 at 11:06 PM on October 22, 2007


'TMOUT=0' has solved the forced logouts. All is right with the world. Gracias.
posted by nakedcodemonkey at 11:10 PM on October 22, 2007


A note for those of you suggesting screen: one of the Un*x servers I use will kill all your screens if you try to leave anything running and log off.
posted by oaf at 5:09 AM on October 23, 2007


« Older what fun, performance-related ...   |  How do I keep my dog active an... Newer »
This thread is closed to new comments.