LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 03-17-2004, 03:21 AM   #1
sanjith11
Member
 
Registered: Oct 2003
Distribution: redhat
Posts: 63

Rep: Reputation: 15
Red face 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
 
Old 03-17-2004, 03:27 AM   #2
anon237
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 03-17-2004, 04:15 AM   #3
sanjith11
Member
 
Registered: Oct 2003
Distribution: redhat
Posts: 63

Original Poster
Rep: Reputation: 15
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
 
Old 03-17-2004, 04:41 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516

Rep: Reputation: 240Reputation: 240Reputation: 240
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
 
Old 03-17-2004, 06:19 AM   #5
sanjith11
Member
 
Registered: Oct 2003
Distribution: redhat
Posts: 63

Original Poster
Rep: Reputation: 15
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
 
Old 03-17-2004, 08:06 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516

Rep: Reputation: 240Reputation: 240Reputation: 240
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
 
Old 03-17-2004, 08:08 AM   #7
sanjith11
Member
 
Registered: Oct 2003
Distribution: redhat
Posts: 63

Original Poster
Rep: Reputation: 15
thanks buddy


regards sanjith
 
Old 03-17-2004, 08:32 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516

Rep: Reputation: 240Reputation: 240Reputation: 240
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
 
Old 04-15-2011, 10:32 PM   #9
yogeeshsagar
LQ Newbie
 
Registered: Apr 2011
Posts: 1

Rep: Reputation: 0
Thumbs up 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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script inside shell script treotan Linux - General 4 02-19-2009 06:34 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
[SHELL SCRIPT] Write at the right of the shell window Creak Linux - General 2 04-02-2004 03:00 PM
Bitchix timer script zeky Linux - Software 0 11-16-2002 10:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration