LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 06-08-2011, 09:31 AM   #1
Vincanis
LQ Newbie
 
Registered: Jun 2011
Posts: 10

Rep: Reputation: Disabled
30 Minute Timer in Bash


Hello everyone, I've recently started coding in bash and I'm making a diagnostic check CD for rackmounted computers to print out to an LCD screen.

So far it works but what i want it to do is, when it checks what ethernet ports are unplugged, it stays there for 30 minutes or so and proceeds through the script.

I've looked up cron and that seems to be useful for making timers on permanent systems but I just want the cd to boot, do the check and then eject it and let the real OS take over and I've also tried using:

while [ `sleep 30m` ];
do
<script>

with no luck. Is there any command I can use to do this? I've noticed a few examples where people have captured the time and then used a while less than/greater than statement but I'm not sure how to do this.

Is it possible to do a concept like:

while [ %current time -lt %current time+30 min ];
do
<script>

or is there an easier way?
Thanks in advance!
 
Old 06-08-2011, 09:51 AM   #2
MS3FGX
LQ Guru
 
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
If you want to wait for 30 minutes, you just need to put the "sleep" line you have in the script by itself. You don't need to do anything fancy with a do while, the script waits for each line to complete before moving on anyway (unless you push a line into the background with &).
 
Old 06-08-2011, 09:58 AM   #3
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Do you want to run the script endlessly during these 30 minutes? Besides checking the actual time against a set variable, you could also use a background process and wait for its completion:
Code:
$ sleep 15 &
[1] 23596
$ while [ $(jobs | wc -l) -gt 0 ]; do echo -n "."; sleep 1; done; echo
..............[1]+  Done                    sleep 15
 
Old 06-08-2011, 10:01 AM   #4
rahul_dubeyin
Member
 
Registered: Apr 2011
Posts: 34

Rep: Reputation: 12
you can get the pid of sleep command and then

Code:
while ps $PID
 
Old 06-08-2011, 10:04 AM   #5
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Yeah: PID=$!
 
Old 06-08-2011, 10:05 AM   #6
Vincanis
LQ Newbie
 
Registered: Jun 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Reuti View Post
Do you want to run the script endlessly during these 30 minutes?
Correct, what it does is check the lan ports and then spit out what one(s) is(are) unplugged/not functioning. I want it to stay here for testing.

IE: When you unplug the 1st ethernet port, the LCD displays LAN 1 DIS, plug it in and it goes away. With 1&2 unplugged it alternates between saying LAN 1 DIS and LAN 2 DIS, so I want it to keep doing this for a specified period of time and then continue.

EDIT: Here's my code I'm working with

Code:
while [ `sleep 30m` ];
	  do	

	   /mnt-system/KNOPPIX/lcdwriter.pl "";
	   BAD="";
		
	  for i in 0 1 2 3
	  do

   	  #LAN number to ETH number mapping
   	  case $i in
   	     0)
   	        j=1;
   	     ;;
   	     1)
   	        j=2;
   	     ;;
   	     2)
   	        j=4;
   	     ;;
   	     3)
   	        j=3;
   	     ;;
   	  esac

	
	     STATUS=`/usr/sbin/ethtool eth$i | grep "Link detected: no"`;
	
	     if [ "x$STATUS" != "x" ]
	     then
	        BAD=$BAD$j;
	        echo "LAN $BAD Disconnected";
	        /mnt-system/KNOPPIX/lcdwriter.pl "LAN $BAD   DIS";
		
		sleep 5;
	     fi
	     done;
           done;

Last edited by Vincanis; 06-08-2011 at 10:22 AM.
 
Old 06-08-2011, 11:27 AM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
while loops over its body over and over, and executes the condition command before each iteration. If the condition command succeeds, while runs its body again. Otherwise, it ends. The break command ends the loop immediately, no matter what. The continue command skips the rest of the body and goes right to the next iteration.

test (and its alias "[") aren't math-like expressions. They are actaully commands. See man test for more.

Backticks execute the command inside, and are substituted for what the command prints to stdout.

(Note that you really should use $(command) instead of backticks. They can't be confused with commas, they nest easily, and backslashes aren't interpreted differently inside).

Hopefully now it should be clear that this:

Code:
while [ `sleep 30m` ]
do
    # body
done
is actaully an infinite loop that runs its body every 30 minutes.

Last edited by MTK358; 06-08-2011 at 11:29 AM. Reason: Typo
 
Old 06-08-2011, 11:41 AM   #8
Vincanis
LQ Newbie
 
Registered: Jun 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
So how is it possible to make a loop that runs a set of commands for 30 minutes?
 
Old 06-08-2011, 11:46 AM   #9
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
My example was unclear? As rahul_dubeyin mentioned, it can even be coded to check the pid:
Code:
$ sleep 30 &
[1] 24007
$ PID=$!
$ while ps $PID > /dev/null; do echo -n "."; sleep 1; done; echo
..........................[1]+  Done                    sleep 30
The loop echoing the "." runs for 30 seconds here.
 
  


Reply



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
crontab: minute hour or hour minute anon091 Linux - Newbie 2 11-04-2009 03:09 PM
Need a simple bash timer trubar Programming 6 03-26-2009 05:27 AM
How can I get LAPIC timer to run instead of the PIT timer? sixerjman Linux - Kernel 1 10-16-2007 09:59 PM
Multimedia timer (SMP friendly timer) bigqueso Linux - Kernel 0 03-15-2007 03:49 PM
Bash : Copy a file every one minute macabre_sunsets Programming 15 09-16-2006 12:26 AM

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

All times are GMT -5. The time now is 09:35 AM.

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