it would be really great to be able to tell after what number of seconds (or milliseconds) a specific sub-command started running
Uma possibilidade é usar essa variável bash
:
SECONDS
This variable expands to the number of seconds since the shell was started. Assignment to this variable resets the count to the value assigned, and the expanded value becomes the value assigned plus the number of seconds since the assignment.
Para combinar isso com um fluxo de saída, considere este script dedicado que adiciona um registro de data e hora a cada linha de entrada:
$ cat timestamp-output
#!/bin/bash
while read line
do
printf "%05d %s\n" $SECONDS "$line"
done
E envie a saída do seu script para esse script.
$ your-script | timestamp-output
Exemplo:
$ cat your-script
#!/bin/bash
for i in 1 2 3 4
do
echo "step $i started";
sleep $(($RANDOM%10));
done
$ your-script | timestamp-output
Saída:
00000 step 1 started
00002 step 2 started
00010 step 3 started
00015 step 4 started
Isso pode ser refinado para registrar a data e hora somente nas linhas de saída que sinalizam o início de um subcomando, supondo que elas possam ser exibidas:
#!/bin/bash
regex="^start of command"
while read line
do
if [[ "$line" =~ $regex ]]; then
printf "%05d %s\n" $SECONDS "$line"
else
echo "$line"
fi
done
Exemplo de script:
#!/bin/bash
for i in 1 2 3 4
do
echo "start of command: $i";
echo some output
echo other output
sleep $(($RANDOM%10));
done
Exemplo de saída:
00000 start of command: 1 some output other output 00002 start of command: 2 some output other output 00006 start of command: 3 some output other output 00008 start of command: 4 some output other output