I need script that to start the stopped service when its stopped.
This script always controls status of service. For example nagios service stopped script have to start nagios service. And It can send an email for notification when stopped.
What OS is this? Some OSes - like Solaris - have special commands for controlling services.
I think it be some cross platform way
#!/usr/bin/perl
use strict;
use warnings;
use Proc::Daemon;
Proc::Daemon::Init;
my $continue = 1;
$SIG{TERM} = sub { $continue = 0 };
while ($continue) {
#do stuff
}
I need this script for redhat. But I can use for Solaris. Everybody can use this script.
What is the special command for controlling the service for Solaris.
How can I use below script for ntop service.
# pgrep ntop
17551
#
#!/usr/bin/perl
use strict;
use warnings;
use Proc::Daemon;
Proc::Daemon::Init;
my $continue = 1;
$SIG{TERM} = sub { $continue = 0 };
while ($continue) {
#do stuff
}
Special command Solaris: svcadm
Once you add the service to known services, then svcadm enable will run, and keep restarting the service automatically. Forever.
svcadm is not autostart the service when stopped.
Finally, I did my script for nagios and ntop services.
FOR NAGIOS
***** crontab *****
[root@GateWay /]# crontab -l
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
@reboot ./autostart.sh > /tmp/autostart.log 2>&1
[root@GateWay /]#
***** autostart.sh ****** root(/)
[root@GateWay /]# cat autostart.sh
#!/usr/bin/env bash
#export ARGELA_RUN_HOME=/
nagios_kontrol()
{
# hOldPID=0
while :; do
hPID=0
hPID=`pgrep -f '/usr/bin/nagios -d /etc/nagios/nagios.cf'`
if [ -z "$hPID" ] ; then
if [ -f $ARGELA_RUN_HOME/sbin/service ]; then
$ARGELA_RUN_HOME/sbin/service nagios start
else
echo "############################################"
echo "### Can not find nagios service ###"
echo "### Check ARGELA_RUN_HOME setting ###"
echo "### Execution failed... ###"
echo "############################################"
break
fi
else
sleep 10
fi
done
}
nagios_kontrol &
[root@GateWay /]#
*********root***************
.autostart.sh
/tmp/autostart.log // for logging
FOR NTOP
***** crontab *****
[root@argela /]# crontab -l
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
@reboot ./autostart.sh > /tmp/autostart.log 2>&1
[root@argela /]#
***** autostart.sh ****** root(/)
[root@argela /]# cat autostart.sh
#!/usr/bin/env bash
#export ARGELA_RUN_HOME=/
ntop_kontrol()
{
# hOldPID=0
while :; do
hPID=0
hPID=`pgrep -f 'ntop'`
if [ -z "$hPID" ] ; then
if [ -f $ARGELA_RUN_HOME/./autostart_ntop.sh ]; then
$ARGELA_RUN_HOME/./autostart_ntop.sh
else
echo "############################################"
echo "### Can not find ntop for executable ###"
echo "### Check NTOP please ###"
echo "### Execution failed... ###"
echo "############################################"
break
fi
else
sleep 10
fi
done
}
ntop_kontrol &
[root@argela /]#
***** autostart_ntop.sh ****** root(/)
[root@argela /]# cat autostart_ntop.sh
/usr/local/bin/ntop -d -i "eth0" -L -P /usr/local/var/ntop -u ntop --skip-version-check --use-syslog=daemon > /tmp/autostart_ntop.log 2>&1
[root@argela /]#
*********root***************
autostart.sh and autostart_ntop.sh
/tmp/autostart_ntop.log // for logging