Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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 have the following script collecting data about current logged users.

# Collecting users login statistics


eval `keychain --eval ~/.ssh/id_rsa`

for STATION in `cat $STATIONS`; do
    TEMP_VAR=`ssh user@$STATION who | cut -d " " -f1 | sort -u | wc -l`


Run periodically with cronjob:

 * * * * * /path_to_script/

When the server reboots the script can't ssh to remote stations and I need to manually run the script for the first time.

What can I do to make the script work after reboot without manually running it the first time?

share|improve this question
Your problem is probably related to ssh authentication. I'd recommend a real monitoring solution, like munin, nagios or similar. – Alex Holst Jan 23 '11 at 10:26

When you manually run the script the first time, what does it do differently? Try the manual first-time run with bash -x and see if there's anything useful in there.

What directory is $CURRENT_ALL_USERS written to?

Oh this is probably your problem, from the keychain(1) manpage:

When keychain is run, it checks for a running ssh-agent, otherwise it starts one. It saves the ssh-agent environment variables to ~/.keychain/${ HOSTNAME }-sh, so that subsequent logins and non-interactive shells such as cron jobs can source the file and make passwordless ssh connections. In addition, when keychain runs, it verifies that the key files specified on the command-line are known to ssh-agent, otherwise it loads them, prompting you for a password if necessary.

keychain does soem special processing the first time it runs, in particular it starts ssh-agent. So ssh-agent must be failing when run from cron, but working when the script is run manually.

I suggest that you consider setting up a special user to run this check and/or a special set of private/public keys. Don't put a password on that keypair. Then you can get rid of the whole keychain thing and just use those keys directly in the script without having to worry about loading keys with keychain, ssh-agent, ssh-add, etc.

Also as another random aside, a neat refinement of this script would be to use a bash array to collect all the logged-in user info, instead of appending to a string like you are doing now.

share|improve this answer

export HOME=/root

You are propably missing the HOME which is used by ssh and referred by ~

eval `keychain --eval ~/.ssh/id_rsa`
share|improve this answer
d'oh. If it's run in user cron then HOME is propably set. Check out also environment with set >> /tmp/log – anttiR Jan 23 '11 at 9:58

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.