#!/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;
Works well here ... maybe you want to add after #!/bin/sh PATH="/bin:/sbin:/usr/bin:/usr/sbin:/opt/usr/bin:/opt/usr/sbin:/usr/local/bin:/usr/local/sbin"
You can also make chmod 775 /etc/mycron/checkServices.sh to make it executable, which is necessary for cron. Then you will also not need to call bash /etc/mycron/checkServices.sh and can just call /etc/mycron/checkServices.sh #!/bin/sh tells the executable to load the file using /bin/sh if you call bash /etc/mycron/checkServices.sh , you run bash, which in turn starts /bin/sh to finally execute your script.
Since the for loop in bash / sh uses the IFS variable ( $IFS ) as a delimiter, you can also make the line services=(httpd named proftpd mysqld dovecot postfix webmin) as services="httpd named proftpd mysqld dovecot postfix webmin" , since this is more general
source share