Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have the following script collecting data about current logged users.

#!/bin/bash
# Collecting users login statistics

CURRENT_ALL_USERS="path_to_log_file"
ADMIN="path_to_admin_dir"
STATIONS="$ADMIN/stations_list"
ALL_USERS_IN_STATIONS=0

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`
    USERS_IN_STATION=$TEMP_VAR
    let ALL_USERS_IN_STATIONS="$ALL_USERS_IN_STATIONS+$USERS_IN_STATION"
done

echo $ALL_USERS_IN_STATIONS > $CURRENT_ALL_USERS

Run periodically with cronjob:

 * * * * * /path_to_script/script.sh

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

2 Answers

When you manually run the script the first time, what does it do differently? Try the manual first-time run with bash -x script.sh 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
add comment

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
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.