I was looking around on the internet to find if there was already a way to time a certain part of an executable/script to see how long that part takes, but all that I find is more for outside the file. So I want to input a line that starts a timer and at the end of the part stops the timer and shows me the elapsed time, not the whole thing but just a part.
example:

  #!/bin/sh
    IPTABLES="/sbin/iptables"
    BLOCKEDIPS_XS=/root/iptables/iptables/blockxs.zone
$IPTABLES -F
$IPTABLES -F INPUT
$IPTABLES -F OUTPUT
$IPTABLES --delete-chain
$IPTABLES -F FORWARD
$IPTABLES -F -t mangle
$IPTABLES -F -t nat
$IPTABLES -X
timer start
/bin/egrep -v "^#|^$" $BLOCKEDIPS_XS | while IFS= read -r ip
do
    # Append everything to droplist
    $IPTABLES -A droplist -i eth0 -s $ip -j LOG --log-prefix " Drop IP List blockxs "
    $IPTABLES -A droplist -i eth0 -s $ip -j DROP
done <"$BLOCKEDIPS_XS"
timer stops, shows elapsed time
  • Please do not add version tags unless the question is version specific. Nothing in this question is specific to any particular version of Ubuntu. – muru May 16 at 13:21
up vote 3 down vote accepted

A time that operates on command groups and the SECONDS variable are both bash features, not in sh. With sh, you can get the timestamps at the two points using the date command, and then get the difference. date +%s will give the time in seconds since the epoch.

# timer start
start=$(date +%s)
/bin/egrep -v "^#|^$" $BLOCKEDIPS_XS | while IFS= read -r ip
do
    # Append everything to droplist
    $IPTABLES -A droplist -i eth0 -s $ip -j LOG --log-prefix " Drop IP List blockxs "
    $IPTABLES -A droplist -i eth0 -s $ip -j DROP
done <"$BLOCKEDIPS_XS"
# timer stops, shows elapsed time
echo $(( $(date +%s) - start ))

You can time part of a bash script by using command grouping

#!/bin/bash

echo "foo"
sleep 1

time { 
sleep 2
echo "bar"
sleep 3
}

sleep 1
echo "baz"

Ex.

$ ./somescript.sh 
foo
bar

real    0m5.006s
user    0m0.005s
sys 0m0.001s
baz

Grouping with a subshell (i.e. parentheses in place of braces) should also work.

  • In this case, a group might not even be needed - the bash time keyword works on pipelines, and the part that OP wants to time is a single pipeline... But they are using sh, so the external time command will be used, making it more complicated. – muru May 16 at 13:42
  • @muru good catch - I missed that the OP was using /bin/sh – steeldriver May 16 at 15:07

The easiest way is to put the time command in front of the command you are running. For example:

$ time locate display-auto
/etc/cron.d/display-auto-brightness
/home/rick/Downloads/display-auto-brightness
/home/rick/Pictures/display-auto-brightness conky.png
/home/rick/Pictures/display-auto-brightness systray.png
/home/rick/Pictures/display-auto-brightness-config 1.png
/home/rick/Pictures/ps display-auto-brightness.png
/lib/systemd/system-sleep/display-auto-brightness
/mnt/e/etc/cron.d/display-auto-brightness
/mnt/e/lib/systemd/system-sleep/display-auto-brightness
/mnt/e/usr/local/bin/display-auto-brightness
/usr/local/bin/display-auto-brightness

real    0m0.826s
user    0m0.803s
sys     0m0.016s

We are using the locate command to find all the filenames with display-auto in the names. Simply insert the time command in front of the command.


Getting time for portion of script

You can use the $SECONDS variable inside your script to get the time for a portion of it. For example:

SECONDS=0
/bin/egrep -v "^#|^$" $BLOCKEDIPS_XS | while IFS= read -r ip
do
    # Append everything to droplist
    $IPTABLES -A droplist -i eth0 -s $ip -j LOG --log-prefix " Drop IP List blockxs "
    $IPTABLES -A droplist -i eth0 -s $ip -j DROP
done <"$BLOCKEDIPS_XS"
timer stops, shows elapsed time
BlockTime=$SECONDS
echo "Total time to block IPs: $BlockTime Seconds"

In this code the SECONDS is reset to zero and then obtained after a lengthy process occurs.

  • yes that is what i see mostly but now i need it in a file like if you were to use the comand nano (filename) so not to run with nano but put it in the file you are working on and once the file is execited with the ./(filename) – delfiler May 16 at 10:46
  • @delfiler I added the SECONDS variable to get time for portion of script only. – WinEunuuchs2Unix May 16 at 10:55
  • so if i am right i should put SECONDS=0 first then put rsyncTime=$SECONDS at the end, right? – delfiler May 16 at 11:02
  • @delfiler Correct! – WinEunuuchs2Unix May 16 at 11:05
  • sad to inform that isn't not working – delfiler May 16 at 11:28

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.