If you have cron access to your cloud, a very common way of doing what you want is to use a Watchdog .
It consists of checking from time to time whether a service or program is running and if it does not execute a boot command.
This is done using the scheduler (in Linux is Cron) with an acceptable time interval that the system to be monitored is out. Generally the average time the service takes to "stop + start" is used.
Below is an example script that serves many services.
Basically you tell the script which service to monitor, which command to start the service, email addresses for alerts and paths to some binaries
Suppose you save the script in your $ HOME with the name watchdog.sh, give permission to run with chmod +x $HOME/watchdog.sh"
, edit your crontable with contrab -e
and add a line like this
*/5 * * * * $HOME/watchdog.sh
Indicating that it will be checked every 5 minutes
Follow a Watchdog script
#!/bin/bash
NAME=tomcat
START='which $NAME'
STARTCMD="$START + _ARGUMENTOS_"
NOTIFY=EMAIL1
NOTIFYCC=EMAIL2
GREP='which grep'
PS='which ps'
NOP='which true'
DATE='which date'
MAIL='which mail' # ou sendmail dependendo da distro
RM='which rm'
# $PS -ax|$GREP -v grep|$GREP $NAME >/dev/null 2>&1 # Caso tenha problema com serviço que roda com um usuario que roda muitos serviços
$PS -efx|$GREP -v grep|$GREP $NAME >/dev/null 2>&1
case "$?" in
0)
# Em execução, não fazer nada.
$NOP
;;
1)
echo "$NAME NÃO ESTÁ EM EXECUÇÃO. INICIANDO $NAME E ENVIANDO AVISOS."
$STARTCMD 2>&1 >/dev/null &
mkdir -p "$HOME/tmp"
NOTICE="$HOME/tmp/watchdog.txt"
echo "$NAME não estava executando e foi iniciado em '$DATE'" > $NOTICE
$MAIL -n -s "Aviso Watchdog" -c $NOTIFYCC $NOTIFY < $NOTICE
$RM -f $NOTICE
;;
esac
exit