ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I don't know how precise you need to time this, but the bigger the amount of seconds you wait the bigger the deviation will be (might be a second or more if you do a sleep 300).
u see my problem is that i give a command and if there is no output for more than 3 minutes then iwant to skip that command and do something else . i think sleep dosent help here. u have any other idea
now you are talking!
this is very tricky to get right.
you could use 'expect' i suspect.
or try the 'wait' command.
something like, maybe? ( I haven't tested this!)
Code:
command > outfile & # save output
command_pid=$! # save PID
sleep $(( 5 * 60 )) & # sleep
sleep_pid=$!
wait $sleep_pid # this will wait till the sleep ends, (don't wait for caommand as it may not end!)
[ -s outfile ] || kill -TERM $command_pid # check outfile, and kill if no output
As I feel, nothing is impossible in programming, using few logics. I too came across similar requirement where in my script need to ssh to number of servers, but should not wait for more than 30 seconds. I know, there are few ssh switches/variables/parameters to serve this requirement; but one fine day I observed an issue with one of my target host, which was in hung status. Port 22 was open and was connecting; but ssh login use to hung without any response. So no ssh option helped to trap such errors. After using few logics, I was able to get the requirement accompalished. My requirement looks to be similar to you where in I would like to kill the ssh session after specified amount of time and continue with my next actions.
Below is the extract of my script. I know, this thread is very old. Still I hope this helps for any one, who are in search of similar requirement.
I am not the GURU in scripting; still can do anything using my logics and with the help fo Google.... I tried removing my personal/server infromation from the below code. While doing the same, their might be some typo in variable names or syntaxes. My intention is just to give you an idea on the logic I used.
# Test to ssh login without password
SSH_TIME_OUT=60
SEVERNAME= <Pick up from list/file in loop>
END_TIME=$(( $(date +%s) + $SSH_TIME_OUT )) # Till the time hits this count, ssh will continue
touch /tmp/temp_output_file
ssh -o PasswordAuthentication=no -o PreferredAuthentications=publickey root@$SERVERNAME 'ls /' > /tmp/temp_output_file 2>/dev/null &
SSH_PID=$!
until [ `ps -ef | grep -i $SSH_PID | grep -v "grep" | wc -l` -eq 0 ] ; do
if [ $(date +%s) -ge $END_TIME ] ; then
#We crossed SSH_TIME_OUT period waiting SSH to connect. Hopefully client is not responding to SSH. Clearing the SSH process
kill -9 $SSH_PID > /dev/null 2>&1
fi
done
#If ssh executed properly, output file size should be more than 0. So we continue only if the output file is bigger than zero
if [ -f /tmp/temp_output_file -a $(ls -l /tmp/temp_output_file | awk '{print $5}') -eq 0 ] ; then
echo "Not able to ssh into server $SERVERNAME - Probably server not responding for ssh login or not set with proper trusted ssh key" >> /tmp/errorlog.log
else
# ssh into server and collect the needful infromation
ssh -f -l root $SERVERNAME df -Phl -x tmpfs -x iso9660 >> /tmp/disk_usage.out 2>/dev/null
fi
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.