From the question here, the OP wants to repeatedly poll the pid of a process using
pidof in a shell script. Of course this is inefficient as a new process must be started for the
pidof program multiple times per second (I don't know that this is the cause of the CPU spikes in the question, but it seems likely).
Usually the way around this kind of thing in a shell script is to work with a single program that outputs the data you need on
stdout and then doing some text processing if necessary. While this involves more programs to be running concurrently, it is likely to be less CPU intensive since new processes are not being continually created to for polling purposes.
So for the above question, one solution might be to have some program which outputs the names and pids of processes as they are created. Then you could do something like:
pids-names | grep some_program | cut -f 2 | while read pid; do process-pid "$pid" done
The problem with this is that it raises a more fundamental question, how can pids and process names be printed as they are created?
I have found a program called
ps-watcher, though the problem with this is that it is just a
perl script which repeatedly runs
ps so it doesn't really solve the problem. Another option is to use
auditd which could probably work if the log was processed directly via
tail -f. An ideal solution would be simpler and more portable than this, though I will accept an
auditd solution if it is the best option.