Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
03-17-2004, 03:21 AM
|
#1
|
Member
Registered: Oct 2003
Distribution: redhat
Posts: 63
Rep:
|
timer in a shell script
can anyone tell me how to set a timer in shell script so that it does a particular action after a specified amount of time.
eg i want to echo something to screen after 5 min duration of waiting
|
|
|
03-17-2004, 03:27 AM
|
#2
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
This works:
sleep <seconds>
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).
Hope this helps.
|
|
|
03-17-2004, 04:15 AM
|
#3
|
Member
Registered: Oct 2003
Distribution: redhat
Posts: 63
Original Poster
Rep:
|
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
|
|
|
03-17-2004, 04:41 AM
|
#4
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
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
|
|
|
03-17-2004, 06:19 AM
|
#5
|
Member
Registered: Oct 2003
Distribution: redhat
Posts: 63
Original Poster
Rep:
|
thanks for your logic. but what if the command gets executed quickly. we will have to wait till the sleep ends can i prevent it in some way
|
|
|
03-17-2004, 08:06 AM
|
#6
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
Good point!
I told you it was *tricky*.
As this is non-trivial...
and I am at work.....
I'll leave the rest to you!
regards, billy
|
|
|
03-17-2004, 08:08 AM
|
#7
|
Member
Registered: Oct 2003
Distribution: redhat
Posts: 63
Original Poster
Rep:
|
thanks buddy
regards sanjith
|
|
|
03-17-2004, 08:32 AM
|
#8
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
I've done this sort of thing before.
This is where shell programming starts to reach it's limits.
This and really robust error handling.
regards, billy
|
|
|
04-15-2011, 10:32 PM
|
#9
|
LQ Newbie
Registered: Apr 2011
Posts: 1
Rep:
|
I resolved this with a logic
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
|
|
|
All times are GMT -5. The time now is 08:18 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|