LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   30 Minute Timer in Bash (https://www.linuxquestions.org/questions/programming-9/30-minute-timer-in-bash-885216/)

Vincanis 06-08-2011 09:31 AM

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!

MS3FGX 06-08-2011 09:51 AM

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

Reuti 06-08-2011 09:58 AM

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


rahul_dubeyin 06-08-2011 10:01 AM

you can get the pid of sleep command and then

Code:

while ps $PID

Reuti 06-08-2011 10:04 AM

Yeah: PID=$!

Vincanis 06-08-2011 10:05 AM

Quote:

Originally Posted by Reuti (Post 4379972)
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;


MTK358 06-08-2011 11:27 AM

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.

Vincanis 06-08-2011 11:41 AM

So how is it possible to make a loop that runs a set of commands for 30 minutes?

Reuti 06-08-2011 11:46 AM

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.


All times are GMT -5. The time now is 03:10 PM.