I am on macOS. I want to write a bash script which pauses for 0.5 seconds (i.e. sleeps) while a certain process (which I know by command name only) uses more than, say 5% CPU. I could
pgrep command_name
and then
ps -p PID -o %cpu | tail -1 | cut -c 3-5
to get the CPU usage and use this number in a while loop. Can this be done more elegantly (ideally in one line of code)?
ps
? I.e.man ps | less +/'NOTES'
and https://unix.stackexchange.com/q/58539/140633 – ibuprofen Jul 05 '21 at 14:49top
. But I still can't do it elegantly. – 220284 Jul 05 '21 at 15:01pidof
might also be a good tool instead ofpgrep
. – Marcus Müller Jul 05 '21 at 16:37