• 5 odgovora
  • 4.734 pregleda
Bash script pomoc 01.05.2012. u 15:18 · pre 176 meseci #1
Gde gresim?

Code:

#!/bin/bash
EMAIL='[email protected]'

SERVICE='httpd'

if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
   echo "[OK] $SERVICE service running, everything is fine."
else
   echo "[!!] $SERVICE is not running."
   echo "---> Starting $SERVICE now."
   service $SERVICE start > /dev/null
fi
   EXITSTATUS=$?
   if [[ $EXITSTATUS != 0 ]] ; then
   echo "Service status for $SERVICE" >> /tmp/mail.err
   echo "**************************************" >> /tmp/mail.err
   echo "$SERVICE is down. I can't start it up!" >> /tmp/mail.err
   echo "Check log files for more details!" >> /tmp/mail.err
   mail -s "Service $SERVICE is down!" $EMAIL < /tmp/mail.err
   rm -fr /tmp/mail.err
else
   echo "Service status for $SERVICE" >> /tmp/mail.ok
   echo "**************************************" >> /tmp/mail.ok
   echo "Service $SERVICE was down! Now it's running so everything is fine." >> /tmp/mail.ok
   mail -s "Service $SERVICE was down!" $EMAIL < /tmp/mail.ok
   rm -fr /tmp/mail.ok
fi


Hvala.
Re: Bash script pomoc 01.05.2012. u 15:42 · pre 176 meseci #2
Za početak,

mislim da imaš jedno fi viška ovde ( trebalo bi ga premestiti na kraj skripte ):

Code:

else
   echo "[!!] $SERVICE is not running."
   echo "---> Starting $SERVICE now."
   service $SERVICE start > /dev/null
fi


Ali bilo bi dobro da nam kopiraš tačnu poruku o grešci koju dobiješ pri izvršavanju.
Potpis
Attempt all the problems. Those you can do, don't do. Do the ones you cannot.
Re: Bash script pomoc 01.05.2012. u 15:47 · pre 176 meseci #3
Greska je u tome sto ja pokusavam da proverim da li je servis stvarno pokrenut preko exit statusa i da na osnovu toga posaljem odgovarajuci email.

Code:

#!/bin/bash
EMAIL='[email protected]'

SERVICE='httpd'

if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
   echo "[OK] $SERVICE service running, everything is fine."
else
   echo "[!!] $SERVICE is not running."
   echo "---> Starting $SERVICE now."
   service $SERVICE start > /dev/null  <-- hocu da proverim da li je servis stvarno pokrenut ili ne
fi
   EXITSTATUS=$?
   if [[ $EXITSTATUS != 0 ]] ; then
   echo "Service status for $SERVICE" >> /tmp/mail.err
   echo "**************************************" >> /tmp/mail.err
   echo "$SERVICE is down. I can't start it up!" >> /tmp/mail.err
   echo "Check log files for more details!" >> /tmp/mail.err
   mail -s "Service $SERVICE is down!" $EMAIL < /tmp/mail.err
   rm -fr /tmp/mail.err
else
   echo "Service status for $SERVICE" >> /tmp/mail.ok
   echo "**************************************" >> /tmp/mail.ok
   echo "Service $SERVICE was down! Now it's running so everything is fine." >> /tmp/mail.ok
   mail -s "Service $SERVICE was down!" $EMAIL < /tmp/mail.ok
   rm -fr /tmp/mail.ok
fi


Ako izbacim fi posle service komande koji mi ne treba dobijam syntax error: unexpected end of file

Re: Bash script pomoc 01.05.2012. u 15:58 · pre 176 meseci #4
Verovatno nisam bio dovoljno jasan - poenta je da želiš da proveriš exit status service komande, a kako sad stvari stoje,
proveravaš exit status ili one echo komande iz if grane ili service komande iz else grane. Drugim rečima, ono što predlažem
je da sve ono iza prve else grane smestiš u blok unutar prve else grane:

Code:

#!/bin/bash
EMAIL='[email protected]'

SERVICE='httpd'

if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
   echo "[OK] $SERVICE service running, everything is fine."
else
   echo "[!!] $SERVICE is not running."
   echo "---> Starting $SERVICE now."
   service $SERVICE start > /dev/null  <-- hocu da proverim da li je servis stvarno pokrenut ili ne
   EXITSTATUS=$?
   if [[ $EXITSTATUS != 0 ]] ; then
      echo "Service status for $SERVICE" >> /tmp/mail.err
      echo "**************************************" >> /tmp/mail.err
      echo "$SERVICE is down. I can't start it up!" >> /tmp/mail.err
      echo "Check log files for more details!" >> /tmp/mail.err
      mail -s "Service $SERVICE is down!" $EMAIL < /tmp/mail.err
      rm -fr /tmp/mail.err
   else
      echo "Service status for $SERVICE" >> /tmp/mail.ok
      echo "**************************************" >> /tmp/mail.ok
      echo "Service $SERVICE was down! Now it's running so everything is fine." >> /tmp/mail.ok
      mail -s "Service $SERVICE was down!" $EMAIL < /tmp/mail.ok
      rm -fr /tmp/mail.ok
   fi
fi

Potpis
Attempt all the problems. Those you can do, don't do. Do the ones you cannot.
Re: Bash script pomoc 01.05.2012. u 16:04 · pre 176 meseci #5
Osim toga, deo sa ps i grep je možda nepotrebno zakomplikovan.

Možeš da koristiš direktno:


service httpd status


tj.



eval service "$SERVICE" status


ili ako želiš baš da grepuješ listu procesa, najbolje koristiti:

pgrep -f ili još bolje pregp -fx


Potpis
Attempt all the problems. Those you can do, don't do. Do the ones you cannot.
Re: Bash script pomoc 01.05.2012. u 16:40 · pre 176 meseci #6
Hvala puno :) To je to :)
Prijavite se da biste odgovorili u ovoj temi.PrijavaRegistracija

Premeštanje teme