Linux Bash Timer

Linux bash Timer

#!/bin/bash
SECS=5
while [[ 0 -ne $SECS ]]; do
echo "$SECS.."
sleep 1
SECS=$[$SECS-1]
done
echo "Time is up, clown."

Timer Event in Bash

I think you tried out wrong if...then condition.
Here is my shot at it ... and it should send the "special command" only once at the required time.

#!/bin/bash

# Set the "next time" when special command should be sent
((start=SECONDS+300))

while read LINE <&3; do
echo "do some stuff"

# Did we reach the "next time" ... if yes
if ((SECONDS>=start)); then
echo "special command"
# And now set the new "next time"
((start=SECONDS+300))
fi
done

How to include a timer in Bash Scripting?

Blunt version

while sleep 60; do
if ! check_internet; then
if is_wifi; then
set_wired
else
set_wifi
fi
fi
done

Using the sleep itself as loop condition allows you to break out of the loop by killing the sleep (i.e. if it's a foreground process, ctrl-c will do).

If we're talking minutes or hours intervals, cron will probably do a better job, as Montecristo pointed out.

how to set timer for each script that runs

for calculating seconds you can use

SECONDS=0 ; 
your_bash_script ;
echo $SECONDS

for more sensitive calculation

start=$(date +'%s%N') 
your_shell_script.sh
echo "It took $((($(date +'%s%N') - $start)/100000)) miliseconds"

for internal time function

time your_shell_script.sh

Edit: example provided for OP

for i in $script_name
do
echo running the script - $i
start=$(date +'%s%N')
/tmp/scripts/$i
echo "It took $((($(date +'%s%N') - $start)/100000)) miliseconds"
done

for i in $script_name
do
echo running the script - $i
time /tmp/scripts/$i
done

Timer in a loop - Bash

I Solved this problem creating a check response with status code.

response=$(curl -H 'authorization: '${token_type}' '${bearer_token}'' --write-out '%{http_code}' --silent --output /dev/null https://api.foo.bar/devices/entities/devices/v1?ids='0000000000000')

if [ ${response} -eq 200 ]; then
do something
else
token
fi


Related Topics



Leave a reply



Submit