LINUX: Controllare se un servizio è in esecuzione

()

Script che controlla se un determinato servizio (in questo caso apache) è in esecuzione e se non lo è lo riavvia.

#!/bin/bash
if $(ps ax | grep apache2 | grep -v grep > /dev/null ) ;
then
exit ;
else
/etc/init.d/apache2 restart ;
fi
Un altro modo per fare la stessa cosa:
#!/bin/bash
TAG=”MOTION”
CONTROL=$(ps ax | grep motion | grep -v grep | awk ‘{print $5}’)
sleep 5 ;
if [ -n “$CONTROL” ]
then
echo “Motion già in esecuzione” ;
exit 0 ;
else
echo “Motion non attivo, RIAVVIO motion!”
motion ;
logger riavvio servizio non attivo -t $TAG ;
exit 1 ;
fi

 

 

Questo script prende l’output opportunamente filtrato da grep e se la stringa NON è vuota (-n “$CONTROL”) esce perché motion è già in esecuzione,
altrimenti riavvia il servizio e riporta con logger un eventuale avvio.
ATTENZIONE: il nome dello script non deve contenere “traccia” del nome del servizio da cercare, grep altrimenti troverà come servizio corrispondente lo
stesso script e non funzionerà come ci si aspetterebbe: in questo caso ad esempio sarebbe un errore nominarlo “riavvio_motion”.
/ 5
Grazie per aver votato!

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?