Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I open multiple shell tabs when I start KDE, and I've just added keychain to my ~/.shellrc the problem is that all the tabs prompt for key passwords when I login. This is quite annoying to do this. Is there any good solution for this so that all the tabs simply start, and once I've logged into one tab, all of them have the keys loaded?

share|improve this question
up vote 2 down vote accepted

Here are two methods:

You can ensure that keychain only opens on one tab like this:

if mkdir /tmp/keychain.lock; then
  eval `keychain --eval --agents ssh id_dsa`
  rm -r /tmp/keychain.lock

But it may not be on the first tab you land on - you might have to hunt for it, which could be just as annoying. This works because mkdir is an atomic operation - only one script will succeed, and that one will display the prompt.

Another way will display the prompt on all the tabs, but will quit them once you respond on any one of them. You can poll a file or use inotify-tools like this:

touch $file
inotifywait -e delete_self $file |\
while read file event; do 
 if [ "$event" = "DELETE_SELF" ]; then
   pkill keychain
done &

rm $file

This one presents the prompt, but first it starts a watcher to see if a file is deleted. After the prompt is satisfied, the file is deleted, and the watcher will kill any other prompts that are waiting. inotifywait is from inotify-tools; inotify is a Linux API. There may be a similar API on other Unices, but if not, you only need a loop that polls to see if the file is deleted.

share|improve this answer
I changed your code a little you can see it here: github.com/xenoterracide/dot_etc/blob/master/rc.d/keychain . I don't think any of my changes would account for my problem... but... I occasionally get 2 prompts for passwords... does my code have a problem? or is there something else causing a race condition. – xenoterracide Dec 20 '10 at 3:11
Yes - There is time between the test -e $file and touch $file. This is why I used the mkdir in my example. You can read more about atomic operations here: unix.stackexchange.com/questions/70/… – Shawn J. Goff Dec 20 '10 at 20:50

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.