I want to repeatedly run a program for a maximum of 5 seconds.
I know that timeout executes a command for the amount of time specified, e.g.:
timeout 5 ./a.out
But I want to keep executing the program until 5 seconds have passed so I can tell how
many times it was executed.
I figured that I need something like this:
timeout 5 `while true; do ./a.out; done`
but this is not working. I've already tried to create a shell script that calculates
the elapsed time of every loop iteration and subtracts it from the start time,
but that is inefficient.
Any help would be appreciated.
If you want to use timeout:
timeout 5s ./a.out
You can write a short script and easily set an end time with date -d "date string" +%s to get a future time in seconds. Then just compare current time to end time and break on true. This allows you to capture additional data during your execution time period. For example, the following code sets the end time 5 seconds in the future and then loops until current time equals end.
#!/bin/bash
end=$(date -d "+ 5 seconds" +%s) # set end time with "+ 5 seconds"
declare -i count=0
while [ $(date +%s) -lt $end ]; do # compare current time to end until true
((count++))
printf "working... %s\n" "$count" # do stuff
sleep .5
done
output:
$ bash timeexec.sh
working... 1
working... 2
working... 3
working... 4
working... 5
working... 6
working... 7
working... 8
working... 9
In your case you would do something like
./a.out & # start your application in background
apid=$(pidof a.out) # save PID of a.out
while [ $(date +%s) -lt $end ]; do
# do stuff, count, etc.
sleep .5 # something to prevent continual looping
done
kill $apid # kill process after time test true
Related
In general, we see programs running and showing from how long it running in realtime
example
[#] Scanning is running from 1min 30sec
The value will keep change dynamically untill the script complete.
How can we do it in bash !
By using printf with a carriage return but no newline
count=0
SECONDS=0
while ((count < 10)); do
# computation code goes here
# just use a sleep for demonstration
sleep 1
printf "\rRunning for %d seconds" $SECONDS
((count++))
done
echo # just to add a newline in the output
SECONDS is a special variable in bash
I have a little question and I hope someone can help me because I can not find a proper solution.
I want to resolve a hostname; while waiting for the result, I'd like to print a notification if it takes more than 30 seconds with shell script commands, preferably built-ins or ubiquitous system commands.
I have a background process that sleeps and then prints a message; while sleeping, the process runs ping, but I can't figure out how to kill the background process after the ping finishes and the message keeps printing even if the ping ends prior to the 30 second time limit since this is part of a bigger script that takes some time to run.
Here's the code that I've been using:
((sleep 30; echo "Querying the DNS server takes more than 30 seconds.") & ping -q -c 1 localhost >/dev/null)
I would greatly appreciate any and all help. Other solutions are welcome too; I just want to tell the user that the DNS is too slow and this will affect the further execution. I have tried ping -w or -W but this is not measuring the resolution time. I have tried to trap the result from the ping. I have tried to kill all processes with the same GPID but it is killing the console also. I am not the best with scripts, maybe this is the reason why this takes me so much time. Thank you in advance.
I hope this approach helps you. I think everything is pretty much portable, except for "bc" maybe. I can give you a "bc-less" version if you need it. Good luck!
#!/bin/bash
timeout=10; ## This is how long to wait before doing some batshit!
printed=1; ## this is how many times you want the message displayed (For #instance, you might want a message EVERY X seconds)
starttime="$( date +%F ) $( date +%T.%3N )"
################### HERE GOES YOUR BACKGROUND PROCESS
sleep 30 &
#######################################################
processId=$! ## And here we got the procees Id
#######################################################
while [ ! -z "$( ps -ef | grep $processId | grep -v grep )" ]
do
endtime="$( date +%F ) $( date +%T.%3N )";
timeelapsed=$( echo " $(date -d "$endtime" "+%s" ) - $(date -d "$starttime" "+%s" ) " | bc );
if [[ ($timeelapsed -gt $timeout) && ($printed -ne 0) ]]
then
echo "This is taking more than $timeout seconds";
printed=$(( printed - 1 ));
starttime="$( date +%F ) $( date +%T.%3N )"
fi
done
### Do something once everything finished
echo "The background process ended!!"
I would like to know how I could run a script that will run a command on my PC if the time is equal to x. I have heard of the at command and some others but I'm trying to make mine a bit unique.
The problem is when I try my script, when my computer is on and I run it, it checks the hour but if I turn my PC on at x:36 or something, then my refresh will occur at an hour after x but with 36 minutes, advice please!
My script
time="$(date +%H)"
echo $time
if [ $time != "22" ]; then
sleep 1h
else
zenity --title="asdf" --text="asdf" --info
fi
You don't want to sleep 1 hour, you want to sleep until 2200. With GNU date, you can use
now=$(date +%s)
t=$(date +%s --date 2200)
sleep "$now"
zenity --title="asdf" --text="asdf" --info
If you want to run the command at 22:00 only, output and compare both hours and minutes:
time=$(date +%H:%M)
if [ $time != 22:00 ] ; then
sleep 58
...
Is there any way I could run several echo statements one after the other with a delay?
For example:
The first statement will be:
echo Hello1
after 1/2 second, run the Second echo statement:
echo Hello2
Likewise, is it possible to run several statements one after the other with a time delay without printing all echoes at once?
Perhaps you would like to use sleep <number of seconds>
Like sleep 60 to wait for a minute.
eg. run from commandline
$ echo 'hello1'; sleep 2; echo 'hello2'
or in a bash script file (myscript.sh)
#!/bin/bash
echo 'hello1'
sleep 2
echo 'hello2 after 2 seconds'
sleep 2
echo 'hello3 after 2 seconds'
echo Hello1
usleep 500000 # sleep 500,000 microseconds
echo Hello2
The usleep(1) command is part of the initscripts package on Fedora.
for i in `echo "hello1 hello2 hello3"`; do echo $i; sleep 2; done
I have an interesting situation I am trying to script. I have a program that outputs 26,000 lines after 10 seconds when it starts successfully. Otherwise I have to kill it and start it again. I tried doing something like this:
test $(./long_program | wc -l) -eq 26000 && echo "Started successfully"
but that only works if the program finishes running. Is there a clever way to watch the output stream of a command and make decisions accordingly? I'm at a loss, not quite sure even how to start searching for this. Thanks!
What about
./long_program > mylogfile &
pid=$!
sleep 10
# then test on mylogfile length and kill $pid if needed
count=0
until [ $count -eq 26000 ]; do
killall ./longrun
#start in background
./longrun >output.$$ &
sleep 10
count=$(wc -l output.$$ |awk '{print $1}')
done
echo "done"
#disown so it continues after current login quits
disown -h