Ubuntu: My loop is stopping but cant figure out why



Question:

Here is the situation ... I have a program that I'm running in wine. This program will sometimes hang.. I wanted to create a 'monitoring script' to watch it and kill and restart it if it hangs. I have gotten it to find the process and kill it if it hangs but the script stops at that point without finishing .. the finishing would be to sleep for 30 seconds then launch the program again .. but instead .. the script just stops

First of all I will say that Im very bad at writing stuff like this ... this is from googling stuff and slapping it into a file and trying to get it to do what I want :D

Here is what I have .. its a bit sloppy and probably can be cleaned up some ...

#!/bin/bash    CPU_USAGE_THRESHOLD="120"  TOPPROCESS="0"  i="1"  re='^[0-9]+([.][0-9]+)?$'    while [ $i -eq 1 ]; do           MYPROCESS=$(ps aux --sort=-pcpu | grep -v %CPU | head -n 1 | cut -d"." -f1 | cut -d" " -f7)           MYPROCESS2=$(ps aux --sort=-pcpu | grep -v %CPU | head -n 1 | cut -d"." -f1 | cut -d" " -f6)  if ! [[ $MYPROCESS2 =~ $re ]] ; then            >&2;       else       if [ "$MYPROCESS2" -gt "1" ]; then          TOPPROCESS="$MYPROCESS2"          echo "the calculated top process2 is $TOPPROCESS"           echo "the usage threshold is $CPU_USAGE_THRESHOLD";       fi  fi  if ! [[ $MYPROCESS =~ $re ]] ; then            >&2;       else       if [ "$MYPROCESS" -gt "1" ]; then          TOPPROCESS="$MYPROCESS"          echo "the calculated top process is $TOPPROCESS"           echo "the usage threshold is $CPU_USAGE_THRESHOLD";        fi  fi   if [ "$TOPPROCESS" -gt "$CPU_USAGE_THRESHOLD" ]; then       exec pkill SAMBC.exe;      echo "process killed"          sleep 30           exec wine C:\\SAMBC\\SAMBC.exe          sleep 60          i="1"   else      sleep 60      i="1"      fi  done  

The result of this seems like it stops at killing the process... I get this as a result if i run this from a command prompt.

{~}:$ monitorsam  the calculated top process2 is 121  the usage threshold is 120  {~}:$  

It seems to stop after the kill because it doesn't echo "process killed" The reason I have process and process2 is for some reason the value I need will show up on one or the other .. I think it depends on what its PID is.

Like I say, I'm very bad at this stuff .. I don't really fully understand the code I just copied it off google searches and found the errors and corrected them using google .. but .. still really don't understand fully .. just a minor understanding ... it is probably some semicolon thing or something I'm missing but I tried all I can think of.

It kills the process when it hangs .. but doesn't start it again and doesn't continue to run the monitorsam script ... I want it to restart the program and continue to monitor it so it can kill it and restart it again if it hangs again.

lol sorry for the book but I wanted to make sure I gave all the information I could :D

any help would be appreciated.


Solution:1

A few things:

  • i is always 1 in your script, AFAICT. So there's no point in using it, and it's simpler to do:

    while true; do  ...  done  
  • You're execing pkill and wine. exec replaces the current shell with the command to be executed, so your script effectively ends when it exec's anything. Just do pkill SAMBC.exe and wine C:\\SAMBC\\SAMBC.exe. The wine command may need to be sent to the background: wine C:\\SAMBC\\SAMBC.exe &.

  • If you want to get the CPU usage of a specific process, it's easier if you use pgrep to get the PID and ps to get just the CPU usage:

    process_usage=$(ps -o pcpu= $(pgrep SAMBC.exe) | sed 's/\..*//')  

    The sed command strips away trailing decimals.

Combining and simplifying:

#!/bin/bash    CPU_USAGE_THRESHOLD="120"    while true; do      proc_usage=$(ps -o pcpu= $(pgrep SAMBC.exe) | sed 's/\..*//')        if (( proc_usage > CPU_USAGE_THRESHOLD )); then           pkill SAMBC.exe;          echo "process killed"          sleep 30           wine C:\\SAMBC\\SAMBC.exe &      fi      sleep 60  done  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »