(bash experts) how to combine time & timeout

I am wanting to profile different bash scripts for CPU usage. I want to use:

time timeout 30 ./test.sh

But then time gives the CPU usage of .timeout and not 'test.sh' as desired?

How can I fix?

put the timeout within the script ?

That's an option true (albeit not easy in my specific situation). But there is surely a way to make this work. If I run:

time ./test.sh

and hit ctrl-c it works.

If I run:

timeout -s TERM 1 time ./test.sh

I don't see the output of time.

time ./test.sh | timeout 30 ./test.sh
[root@mail ~]# time ./ping.scr | timeout 2 ./ping.scr
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=118 time=0.812 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=118 time=0.844 ms

real    0m2.010s
user    0m0.007s
sys     0m0.005s

2 Likes

Thanks. I will mark your idea as a solution, but in the end I used:

timeout -s INT 10 time ./test.sh

The issue was getting timeout to issue the correct signal (simulating the same effect as running manually with 'time ./test.sh' and pressing Ctrl-C.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.