shell script:
#!/bin/sh
services=( httpd named proftpd mysqld dovecot postfix webmin)
for service in ${services[@]}
do
if ps ax | grep -v grep | grep $service > /dev/null
then
echo "$service service running, everything is fine"
else
echo "$service is not running"
service $service start
fi
done
file executable, running from root user
command:
bash /etc/mycron/checkServices.sh
tried sh and just /etc/mycron/checkServices.sh
doesn’t run
Works fine here… maybe you want to add after
#!/bin/shPATH="/bin:/sbin:/usr/bin:/usr/sbin:/opt/usr/bin:/opt/usr/sbin:/usr/local/bin:/usr/local/sbin"You could also do
chmod 775 /etc/mycron/checkServices.shto make it executable, which is needed for cron. Then you would also not need to callbash /etc/mycron/checkServices.shand can just call/etc/mycron/checkServices.shthe#!/bin/shtells the executable loader to load the file with/bin/shif you invokebash /etc/mycron/checkServices.shyou will start bash which on his turn would start/bin/shto finally execute your script.Since the for loop in bash / sh uses the IFS variable (
$IFS) as delimiter, you could also make the lineservices=(httpd named proftpd mysqld dovecot postfix webmin)asservices="httpd named proftpd mysqld dovecot postfix webmin"since this is more general