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 unix.stackexchange.com/q/58539/140633top. But I still can't do it elegantly.pidofmight also be a good tool instead ofpgrep.