Bash: Count Minutes and Seconds since Start of a Process

Starting long process you may want to watch how much time is it going. This helps you to guess how much time remains.

Usually you run some process and then watch on it in another terminal window in a loop `while [ 1 ]`.

For example, need to count minutes since the process start. Algorithm and simple bash script are here.

 

1. Store the start time in seconds in a variable

start=$(date +%s)

This stores start point as POSIX time in seconds like: 1660042758

 

2. Compute the difference between two date points in seconds

d=$(($(date +%s) - $start))

This counts seconds passed from start point, like: 489.

If your process is fast and you need seconds, so print seconds in a loop.

 

3. Compute minutes and seconds since start of a process

m=$(($d/60))

s=$(($d%60))

This counts minutes as integer number, and it is 8 in my case, also this counts seconds by taking rest from division with operator %.

 

4. Print time since the start of a process

echo `date +'%Y-%M-%d %H:%M:%S'` Process works $m min $s sec since start at $(date +'%m/%d/%Y %H:%M:%S' -d"@$start")
2022-08-09 14:08:56 Process works 8 min 9 sec since start at 08/09/2022 13:59:18

As you may see, our log contains current date and time at the beginning, then the message with time since start and also date and time of a start. I used different date formats, because logs usually have format year-month-date.

 

5. Example

For example, start to search beautiful address on Solana, starting with youni:

solana-keygen grind --starts-with youni:20

Next open another terminal window and let's watch on this process:

pgrep solana-keygen
2416

start=$(date +%s); while [ $(pgrep solana-keygen) ]; do d=$(($(date +%s) - $start)) m=$(($d/60)) s=$(($d%60)); echo `date +'%Y-%M-%d %H:%M:%S'` Process works $m min $s sec since start at $(date +'%m/%d/%Y %H:%M:%S' -d"@$start"); sleep 15; done; echo Finished at $(date)

2022-35-09 14:35:54 Process works 0 min 0 sec since start at 08/09/2022 14:35:54
2022-36-09 14:36:09 Process works 0 min 15 sec since start at 08/09/2022 14:35:54
2022-36-09 14:36:24 Process works 0 min 30 sec since start at 08/09/2022 14:35:54
2022-36-09 14:36:39 Process works 0 min 45 sec since start at 08/09/2022 14:35:54
2022-36-09 14:36:54 Process works 1 min 0 sec since start at 08/09/2022 14:35:54
2022-37-09 14:37:09 Process works 1 min 15 sec since start at 08/09/2022 14:35:54
2022-37-09 14:37:24 Process works 1 min 30 sec since start at 08/09/2022 14:35:54
..
2022-40-09 14:40:25 Process works 4 min 31 sec since start at 08/09/2022 14:35:54
Finished at Tue 09 Aug 2022 02:40:40 PM UTC

 

Extra: Count time since start of a process with ddiff in Linux

There is also utility ddiff from dateutils package, that you can find this way in apt:

apt-file search ddiff
dateutils: /usr/bin/dateutils.ddiff

apt install dateutils

ddiff can count time between to dates if you provide dates in any format and define also the format of input:

dateutils.ddiff -i '%Y%m%d%H%M%S' 20220809135918 20220809140727
489s

But it is easy to count time since process start with own hands.

Section
Category