Shell Script: FreeBSD ensure all services are running

The FreeBSD rc system makes it very simple to check and control services. With the addition of the service command you can easily list enabled services. This script ensures all services are running. It uses service -e to get enabled services, if rc status reports service is not running, start it.

I don’t know of any built in functionality to do this, however writing a script to do it is pretty simple. My script below uses the service -e command to get the list of enabled services. It then uses the status subcommand to check if the service is running, if it is not, the service will be started.

Not all rc scripts start a daemon, or by design will not run continuously. An example of this is background fsck. To stop these from being started and to give the user granulated control, the script supports an ignore list.

Check if services are running, if not, start them

#!/bin/sh
#
# Script to check if enabled rc scripts are running, if not start them
#
# Uses ``<rc.d script> status'' to check if service is running.
# Some rc scripts do not run a simple daemon, we also might not want
# to check them, add the rc script name to IGNORE_LIST.
#
# Jake Smith, <jake at xz.cx>
# http://mebsd.com

# Ignore list, services we don't want to start, e.g. bgfsck, newsyslog.
# Note space at ends of string and in test var, this ensures exact matches.
IGNORE_LIST=" bgfsck newsyslog ntpdate "

# Get list of enabled rc scripts
/usr/sbin/service -e | while read SERVICE
do
    # is rc script in ignore list?
    test "${IGNORE_LIST#* $(basename ${SERVICE}) }" != "${IGNORE_LIST}" && continue

    # check rc script supports status
    ${SERVICE} 2>&1 | /usr/bin/grep '|status|poll' >/dev/null
    if [ $? -eq 0 ]
    then
        # check status
        STATUS=$(${SERVICE} status)
        if [ $? -gt 0 ]
        then
            # service not running try to start
            echo ${STATUS}
            ${SERVICE} start
            ${SERVICE} status
        fi
    fi
done

You may also like...

  • Pingback: Check if service is running on FreeBSD 9.0 breakdown()

  • BSD

    That is superb! I have tested it by stopping my ntpd and spamd service and when I run your script both services were started. Thanks very much! This is very useful to run after updating many ports as sometimes the services aren’t restarted after updating is complete. This will quickly and easily restart all the ones that have been stopped during an update.

  • BSD

    That is superb! I have tested it by stopping my ntpd and spamd service and when I run your script both services were started. Thanks very much! This is very useful to run after updating many ports as sometimes the services aren’t restarted after updating is complete. This will quickly and easily restart all the ones that have been stopped during an update.

    • Andrea

      I’ve been looking for something to restart ports after updating them as well… thanks!

  • BSD

    I’ve been using your script since you posted it here and its great! The only thing is I can’t seem to get the ignore list to work. When I run your script everytime the following is logged in /var/log/messages:

    /usr/sbin/service: WARNING: $svnserve_enable is not set properly – see rc.conf(5).
    /usr/sbin/service: WARNING: $snmptrapd_enable is not set properly – see rc.conf(5).
    /usr/sbin/service: WARNING: $snmpd_enable is not set properly – see rc.conf(5).
    /usr/sbin/service: WARNING: $rrdcached_enable is not set properly – see rc.conf(5).
    /usr/sbin/service: WARNING: $dccd_enable is not set properly – see rc.conf(5).
    /usr/sbin/service: WARNING: $dccgrey_enable is not set properly – see rc.conf(5).
    /usr/sbin/service: WARNING: $dccifd_enable is not set properly – see rc.conf(5).
    /usr/sbin/service: WARNING: $dccm_enable is not set properly – see rc.conf(5).
    /usr/sbin/service: WARNING: $milteropendkim_enable is not set properly – see rc.conf(5)
    /usr/sbin/service: WARNING: $openssh_enable is not set properly – see rc.conf(5).
    /usr/sbin/service: WARNING: $htcacheclean_enable is not set properly – see rc.conf(5).
    /usr/sbin/service: WARNING: $courier_imap_pop3d_ssl_enable is not set properly – see rc
    /usr/sbin/service: WARNING: $courier_imap_pop3d_enable is not set properly – see rc.con
    /usr/sbin/service: WARNING: $netwait_enable is not set properly – see rc.conf(5).

    For the first one I have tried putting svnserver and svnserve_enable in the ignore list but it hasn’t helped. What am I doing wrong and what can I do to stop this from being logged everytime I run the script?

    Thank you ;-)

    • http://www.mebsd.com/ Jake Smith

      Hi, glad the script is working well for you!

      This warning is simply because there is no explicit entry in /etc/rc.conf for all your installed services, including the disabled services. To remove these warnings you will need to add a valid entry for each service into rc.conf. For example: svnserve_enable=”NO”, assuming it shouldn’t be running. You could use this command to make things a little quicker: ‘service svnserve rcvar | tee -a /etc/rc.conf’. Just make sure the service is correctly enabled/disabled as you need.

      The warnings themselves are coming from the ‘/usr/sbin/service -e’ command, /usr/sbin/service is a shell script that includes the file /etc/rc.subr for some generic functions. The checkyesno() function is producing the warning. This function will return a relevant exit code based on the value passed to it. If it doesn’t recognize the value or if it’s blank, such as this case, it will log the warning message you are seeing.

      Hope this helps :)

      • BSD

        Thanks for the explanation! I edited my /etc/rc.conf file to define these services and set them all to “NO” and when rerunning the script I now get no errors logged in /var/log/messages

        Thank you!

  • BSD

    I’ve been using your script since you posted it here and its great! The only thing is I can’t seem to get the ignore list to work. When I run your script everytime the following is logged in /var/log/messages:

    /usr/sbin/service: WARNING: $svnserve_enable is not set properly – see rc.conf(5).
    /usr/sbin/service: WARNING: $snmptrapd_enable is not set properly – see rc.conf(5).
    /usr/sbin/service: WARNING: $snmpd_enable is not set properly – see rc.conf(5).
    /usr/sbin/service: WARNING: $rrdcached_enable is not set properly – see rc.conf(5).
    /usr/sbin/service: WARNING: $dccd_enable is not set properly – see rc.conf(5).
    /usr/sbin/service: WARNING: $dccgrey_enable is not set properly – see rc.conf(5).
    /usr/sbin/service: WARNING: $dccifd_enable is not set properly – see rc.conf(5).
    /usr/sbin/service: WARNING: $dccm_enable is not set properly – see rc.conf(5).
    /usr/sbin/service: WARNING: $milteropendkim_enable is not set properly – see rc.conf(5)
    /usr/sbin/service: WARNING: $openssh_enable is not set properly – see rc.conf(5).
    /usr/sbin/service: WARNING: $htcacheclean_enable is not set properly – see rc.conf(5).
    /usr/sbin/service: WARNING: $courier_imap_pop3d_ssl_enable is not set properly – see rc
    /usr/sbin/service: WARNING: $courier_imap_pop3d_enable is not set properly – see rc.con
    /usr/sbin/service: WARNING: $netwait_enable is not set properly – see rc.conf(5).

    For the first one I have tried putting svnserver and svnserve_enable in the ignore list but it hasn’t helped. What am I doing wrong and what can I do to stop this from being logged everytime I run the script?

    Thank you ;-)

    • http://mebsd.com/ Jake Smith

      Hi, glad the script is working well for you!

      This warning is simply because there is no explicit entry in /etc/rc.conf for all your installed services, including the disabled services. To remove these warnings you will need to add a valid entry for each service into rc.conf. For example: svnserve_enable=”NO”, assuming it shouldn’t be running. You could use this command to make things a little quicker: ‘service svnserve rcvar | tee -a /etc/rc.conf’. Just make sure the service is correctly enabled/disabled as you need.

      The warnings themselves are coming from the ‘/usr/sbin/service -e’ command, /usr/sbin/service is a shell script that includes the file /etc/rc.subr for some generic functions. The checkyesno() function is producing the warning. This function will return a relevant exit code based on the value passed to it. If it doesn’t recognize the value or if it’s blank, such as this case, it will log the warning message you are seeing.

      Hope this helps :)

      • BSD

        Thanks for the explanation! I edited my /etc/rc.conf file to define these services and set them all to “NO” and when rerunning the script I now get no errors logged in /var/log/messages

        Thank you!