ps aux + grep + nice + while

Hi again, well does anyone knows how can i grep a process that right know the only part of the process name that i know is "backup" then renice it if the cpu consumption is more then 90% ...

for now i have :

a=$(ps aux | grep -c backup )

while  $a > 2 #pseudo code
do 
    if [ $cpulevel gt 95 ]; then # grab the cpu consumption for the task ?? lol TOP maybe


    renice ... # this is were i really dont know how im gonna grab the pid maybe awk
                  # and lower the priority of the backup task
    
    fi

done

IMHO It is not a good idea... For you may end with backups never completing...
With time passing all processes loose importance ( automatically the privileges are lowered ) after a well quite some time if your backup is still running, its new privilege will avoid the backup to complete itself... I already saw that on 2 sites so far... ( without anybody noticing... I happened to be called for an issue on a weekend and when tried to restore, I looked at 70 tapes...)

yeh i understand but that is on server that i need to keep track of logs, the backup task ( c0ded by the product team ) is single core so doesnt try to balance itself in the server resources ... that was the only solution i thought

What kind of backup tool are you dealing with? Why is the server so loaded at backup time?

IDS - Large stream of data coming in , at in a certain period of time in the night the task backup starts and i receive a spike cpu load alert that makes me always check for possible DDoS on our servers etc

But what kind of backup? What is your backup tool?
an IDS should not run during backup, meaning to be safe you should have two net interfaces configured one for appl etc... the other for technical purpose (maintenance, backup) and when backup runs disable the open (appl ) network interface ( you use now the private technical...) and so you can put to sleep during backup your IDS

well the tool is integrated on the ids tools is Backup script that in a basic resume logs in zips and send it by ssh to the backup server from the main server ( the one that receives the traffic and analyze it ) .

Ah... So its not a true system backup then... So I suppose the peak you get is when it is actually zipping when is to be sent...
Maybe all you have to do is to monitor the phenomenon, if it is that, then write a script to point out the process from your alert

that is exactly what i'm doing vbe :slight_smile: a .sh to make the process go down in priority a bit not too much and relase some load on the task ( lower the cpu load ) and prevent the alert on my email from the CPU Health monitor :slight_smile:

Will this work for you?

if [ $( ps -ef | grep -v grep | grep -c backup ) -eq 1 ]
then
      UNIX95= ps -ef -o pid,pcpu,args | grep backup | grep -v grep | read _PID _CPU _ARGS
      if [ ${_CPU} -gt 95 ]
      then
            # renice priority ${_PID}
      fi
fi
1 Like

hmmm why, not a while loop to keep track if the process is on or not.

well bipinajith i have your script on my crontab.hourly :slight_smile: small question why you have the the read parameter on the UNIXPROC ... variable and why you dont refer to the unixproc to find the values on the if -gt 95 ?

This one does work on my linux/bash system; not sure it will on yours as you don't mention details:

while read PID CPU REST < <(ps -eo pid,pcpu,args|grep ackup)
      do echo $CPU
         sleep 2
      done

This one will print the backup process' CPU percentage every other second. Replace echo with what you need, e.g. if [ "$CPU" -ge 95 ] . If the process does not exist or disappear, so does the loop. Make sure you only grep the one backup process of interest, eventually you have to narrow down the grep pattern.

1 Like
while read PID CPU REST < <(ps -eo pid,pcpu,args|grep ackup)
      do 
         if [ "$CPU" -ge 95 ]
         then
           renice -1 $PID
         fi   
         sleep 2
      done

drd0spt, UNIX95 is no ordinary variable, it changes the behavior of the ps command to use the XPG4 environment instead of the HPUX environment.

You can read more about it here

If that works, great! It might be wise to consider renicing back if the process consumes less than a sensible CPU percentage for a while...

i thought on that but i experimented seems he goes up when he runs out of the loop in normal fashion after the cpu reaches levels below the 95