Ensure that the service is running

    name
        The name of the init or rc script used to manage the service

    enable
        Set the service to be enabled at boot time, ``True`` sets the service
        to be enabled, ``False`` sets the named service to be disabled. The
        default is ``None``, which does not enable or disable anything.

    sig
        The string to search for when looking for the service process with ps

    init_delay
        Some services may not be truly available for a short period after their
        startup script indicates to the system that they are. Provide an
        'init_delay' to specify that this state should wait an additional given
        number of seconds after a service has started before returning. Useful
        for requisite states wherein a dependent state might assume a service
        has started but is not yet fully initialized.

    no_block : False
        **For systemd minions only.** Starts the service using ``--no-block``.

        .. versionadded:: 2017.7.0

    unmask : False
        **For systemd minions only.** Set to ``True`` to remove an indefinite
        mask before attempting to start the service.

        .. versionadded:: 2017.7.0
            In previous releases, Salt would simply unmask a service before
            making any changes. This behavior is no longer the default.

    unmask_runtime : False
        **For systemd minions only.** Set to ``True`` to remove a runtime mask
        before attempting to start the service.

        .. versionadded:: 2017.7.0
            In previous releases, Salt would simply unmask a service before
            making any changes. This behavior is no longer the default.

    .. note::
        ``watch`` can be used with service.running to restart a service when
         another state changes ( example: a file.managed state that creates the
         service's config file ). More details regarding ``watch`` can be found
         in the :ref:`Requisites <requisites>` documentation.

def running(name,
            enable=None,
            sig=None,
            init_delay=None,
            no_block=False,
            unmask=False,
            unmask_runtime=False,
            **kwargs):
    '''
    Ensure that the service is running

    name
        The name of the init or rc script used to manage the service

    enable
        Set the service to be enabled at boot time, ``True`` sets the service
        to be enabled, ``False`` sets the named service to be disabled. The
        default is ``None``, which does not enable or disable anything.

    sig
        The string to search for when looking for the service process with ps

    init_delay
        Some services may not be truly available for a short period after their
        startup script indicates to the system that they are. Provide an
        'init_delay' to specify that this state should wait an additional given
        number of seconds after a service has started before returning. Useful
        for requisite states wherein a dependent state might assume a service
        has started but is not yet fully initialized.

    no_block : False
        **For systemd minions only.** Starts the service using ``--no-block``.

        .. versionadded:: 2017.7.0

    unmask : False
        **For systemd minions only.** Set to ``True`` to remove an indefinite
        mask before attempting to start the service.

        .. versionadded:: 2017.7.0
            In previous releases, Salt would simply unmask a service before
            making any changes. This behavior is no longer the default.

    unmask_runtime : False
        **For systemd minions only.** Set to ``True`` to remove a runtime mask
        before attempting to start the service.

        .. versionadded:: 2017.7.0
            In previous releases, Salt would simply unmask a service before
            making any changes. This behavior is no longer the default.

    .. note::
        ``watch`` can be used with service.running to restart a service when
         another state changes ( example: a file.managed state that creates the
         service's config file ). More details regarding ``watch`` can be found
         in the :ref:`Requisites <requisites>` documentation.
    '''
    ret = {'name': name,
           'changes': {},
           'result': True,
           'comment': ''}

    # Check for common error: using enabled option instead of enable
    if 'enabled' in kwargs:
        return _enabled_used_error(ret)

    # Convert enable to boolean in case user passed a string value
    if isinstance(enable, six.string_types):
        enable = salt.utils.data.is_true(enable)

    # Check if the service is available
    try:
        if not _available(name, ret):
            if __opts__.get('test'):
                ret['result'] = None
                ret['comment'] = 'Service {0} not present; if created in this state run, it would have been started'.format(name)
            return ret
    except CommandExecutionError as exc:
        ret['result'] = False
        ret['comment'] = exc.strerror
        return ret

    # lot of custom init script won't or mis implement the status
    # command, so it is just an indicator but can not be fully trusted
    before_toggle_status = __salt__['service.status'](name, sig)
    if 'service.enabled' in __salt__:
        before_toggle_enable_status = __salt__['service.enabled'](name)
    else:
        before_toggle_enable_status = True

    unmask_ret = {'comment': ''}
    if unmask:
        unmask_ret = unmasked(name, unmask_runtime)

    # See if the service is already running
    if before_toggle_status:
        ret['comment'] = '\n'.join(
            [_f for _f in ['The service {0} is already running'.format(name),
                           unmask_ret['comment']] if _f]
        )
        if enable is True and not before_toggle_enable_status:
            ret.update(_enable(name, None, skip_verify=False, **kwargs))
        elif enable is False and before_toggle_enable_status:
            ret.update(_disable(name, None, skip_verify=False, **kwargs))
        return ret

    # Run the tests
    if __opts__['test']:
        ret['result'] = None
        ret['comment'] = '\n'.join(
            [_f for _f in ['Service {0} is set to start'.format(name),
                           unmask_ret['comment']] if _f])
        return ret

    # Conditionally add systemd-specific args to call to service.start
    start_kwargs, warnings = \
        _get_systemd_only(__salt__['service.start'], locals())
    if warnings:
        ret.setdefault('warnings', []).extend(warnings)

    if salt.utils.platform.is_windows():
        for arg in ['timeout', 'with_deps', 'with_parents']:
            if kwargs.get(arg, False):
                start_kwargs.update({arg: kwargs.get(arg)})

    try:
        func_ret = __salt__['service.start'](name, **start_kwargs)
    except CommandExecutionError as exc:
        ret['result'] = False
        ret['comment'] = exc.strerror
        return ret

    if not func_ret:
        ret['result'] = False
        ret['comment'] = 'Service {0} failed to start'.format(name)
        if enable is True:
            ret.update(_enable(name, False, result=False, skip_verify=False, **kwargs))
        elif enable is False:
            ret.update(_disable(name, False, result=False, skip_verify=False, **kwargs))
        return ret

    if init_delay:
        time.sleep(init_delay)

    # only force a change state if we have explicitly detected them
    after_toggle_status = __salt__['service.status'](name, sig)
    if 'service.enabled' in __salt__:
        after_toggle_enable_status = __salt__['service.enabled'](name)
    else:
        after_toggle_enable_status = True
    if (
            (before_toggle_enable_status != after_toggle_enable_status) or
            (before_toggle_status != after_toggle_status)
    ) and not ret.get('changes', {}):
        ret['changes'][name] = after_toggle_status

    if after_toggle_status:
        ret['comment'] = 'Started Service {0}'.format(name)
    else:
        ret['comment'] = 'Service {0} failed to start'.format(name)
        ret['result'] = False

    if enable is True:
        ret.update(_enable(name, after_toggle_status, result=after_toggle_status, skip_verify=False, **kwargs))
    elif enable is False:
        ret.update(_disable(name, after_toggle_status, result=after_toggle_status, skip_verify=False, **kwargs))

    if init_delay:
        ret['comment'] = (
            '{0}\nDelayed return for {1} seconds'
            .format(ret['comment'], init_delay)
        )

    if unmask:
        ret['comment'] = '\n'.join([ret['comment'], unmask_ret['comment']])

    return ret