Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
add comment

1 Answer 1

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
fi

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:

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

keychain
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
1  
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
add comment

Your Answer

 
discard

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.