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! |
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 &).
|
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 & |
you can get the pid of sleep command and then
Code:
while ps $PID |
Yeah: PID=$!
|
Quote:
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` ]; |
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` ] |
So how is it possible to make a loop that runs a set of commands for 30 minutes?
|
My example was unclear? As rahul_dubeyin mentioned, it can even be coded to check the pid:
Code:
$ sleep 30 & |
All times are GMT -5. The time now is 03:10 PM. |