I am utilising the very handy tool CPULimit to restrict the CPU usage of an import triggered in the cron of my Ubuntu server.
Successfully limiting the process is not a problem, I am also able to return the value back to no limit (100%) when the process ends, however when the import process terminates, I am left with CPULimit processes running. The list of CPULimit processes then gets appended to when the next cron job runs.
I can run a command like pkill -f cpulimit
to remove the processes, however I am assuming there is a command that will terminate the limit, it just isn't documented.
To try help you understand here is a bit of a flow:
- Cron triggers import.
- Import script sets a cpulimit and points to
> /dev/null
: sudo -u {USER} cpulimit --pid {PID} --limit {LIMIT} > /dev/null &
.
- A CPULimit process is added to the processes list when grep'ing
ps aux | grep cpulimit
.
- The import completes.
- The import script then returns the limit to 100%.
- A CPULimit process is added to the process list allowing 100% CPU.
~ This is what I thought would end the previous process, but it keeps the {LIMIT} processes running and the limit:100 process.
- The importer runs again.
- Two more CPULimit processes are added to the list in
ps aux | grep cpulimit
.
I can shell_exec(pkill -f cpulimit) which kills all the CPULimit processes other than the latest ones.
However, say I want to limit other processes and the timings don't work out perfectly, the limit on other restricted processes will also be killed.
Is there a way to initiate the cronjob, without freezing the importer when the CPULimit is applied (I got around this problem by sending it to > /dev/null), then when the importer ends, remove / delete / kill the limiter in this instance, as they currently just stack up?
** I am not requesting to kill the importer process if the limit is reached, I want to kill the CPULimit from the processes and remove the limit on the process it is limiting **