---
title: Charmhub | Deploy Operator Libs Linux using Charmhub - The Open Operator Collection
description: Deploy the latest version of Operator Libs Linux on any cloud.
url: https://charmhub.io/operator-libs-linux/libraries/juju_systemd_notices
---

# Operator Libs Linux

[Jon Seager](https://charmhub.io/publisher/jnsgruk "View all packages from Jon Seager")

* [Jon Seager](https://charmhub.io/publisher/jnsgruk "View all packages from Jon Seager")

Platform:

22.04

20.04

stable 2

```
juju deploy operator-libs-linux
```

[Learn to deploy on juju >](https://juju.is/docs/juju/manage-applications)

[Toggle side navigation](https://charmhub.io/operator-libs-linux/libraries/juju_systemd_notices#drawer)

## charms.operator\_libs\_linux.v0.juju\_systemd\_notices

* [*Docstrings*Docstrings](https://charmhub.io/operator-libs-linux/libraries/juju_systemd_notices)
  [*Code*Source code](https://charmhub.io/operator-libs-linux/libraries/juju_systemd_notices/source-code)
* + Download

    Fetch library

    ```
    charmcraft fetch-lib charms.operator_libs_linux.v0.juju_systemd_notices
    ```

    [Download juju\_systemd\_notices.py](https://charmhub.io/operator-libs-linux/libraries/juju_systemd_notices/download)
  + *Last updated* 21 Jul 2024
  + *Revision* Library version 0.2

Library for utilizing systemd to observe and emit notices when services change state.

This library provides both the public API for observing systemd services from within
charmed operators, and utilities for running a minimal juju-systemd-notices daemon that watches
observed services running on the machine. The juju-systemd-notices daemon watches observed
services using DBus; it observes messages received on the DBus message bus and evaluates the
contents of the messages to determine if a service state-change event must be emitted.

##### How to use within a charmed operator (machine only)

Here is an example of subscribing a charmed operator to observe the state of an internal
systemd service and handle events based on the current emitted state:

```
from charms.operator_libs_linux.v0.juju_systemd_notices import (
    Service,
    ServiceStartedEvent,
    ServiceStoppedEvent,
    SystemdNotices,
)

class ApplicationCharm(CharmBase):
    # Application charm that needs to observe the state of an internal service.

    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)

        # Register services with charm. This adds the events to observe.
        self._systemd_notices = SystemdNotices(self, [Service("snap.slurm.slurmd", alias="slurmd")])
        self.framework.observe(self.on.install, self._on_install)
        self.framework.observe(self.on.stop, self._on_stop)
        self.framework.observe(self.on.service_slurmd_started, self._on_slurmd_started)
        self.framework.observe(self.on.service_slurmd_stopped, self._on_slurmd_stopped)

    def _on_install(self, _: InstallEvent) -> None:
        # Subscribe the charmed operator to the services on the machine.
        # .subscribe() configures the notices hooks and starts the juju-systemd-notices daemon.
        # The juju-systemd-notices daemon is per unit. This means that the unit name will be
        # meshed into the service name. E.g. juju-systemd-notices becomes
        # juju-{unit_name}-{unit_number}-systemd-notices.
        self._systemd_notices.subscribe()

    def _on_start(self, _: StartEvent) -> None:
        # This will trigger the juju-systemd-notices daemon to
        # emit a `service-slurmd-started` event.
        snap.slurmd.enable()

    def _on_stop(self, _: StopEvent) -> None:
        # To stop the juju-systemd-notices service running in the background.
        # .stop() also disables the juju-systemd-notices so that it does not
        # start back up if the underlying machine is rebooted.
        self._systemd_notices.stop()

    def _on_slurmd_started(self, _: ServiceStartedEvent) -> None:
        self.unit.status = ActiveStatus()
        time.sleep(60)

        # This will trigger the juju-systemd-notices daemon to
        # emit a `service-slurmd-stopped` event.
        snap.slurmd.stop()

    def _on_slurmd_stopped(self, _: ServiceStoppedEvent) -> None:
        self.unit.status = BlockedStatus("slurmd not running")
```

---

Index

* [class Service](https://charmhub.io/operator-libs-linux/libraries/juju_systemd_notices#service)
* [class ServiceStartedEvent](https://charmhub.io/operator-libs-linux/libraries/juju_systemd_notices#servicestartedevent)
* [class ServiceStoppedEvent](https://charmhub.io/operator-libs-linux/libraries/juju_systemd_notices#servicestoppedevent)
* [class SystemdNotices](https://charmhub.io/operator-libs-linux/libraries/juju_systemd_notices#systemdnotices)
* + [def \_\_init\_\_(
    self,
    charm,
    services)](https://charmhub.io/operator-libs-linux/libraries/juju_systemd_notices#systemdnotices-__init__)
* + [def subscribe(
    self)](https://charmhub.io/operator-libs-linux/libraries/juju_systemd_notices#systemdnotices-subscribe)
* + [def stop(
    self)](https://charmhub.io/operator-libs-linux/libraries/juju_systemd_notices#systemdnotices-stop)

#### class Service

Systemd service to observe.

Arguments

name

Name of systemd service to observe on dbus.

alias

Event name alias for service.

#### class ServiceStartedEvent

Description

Event emitted when service has started. None

#### class ServiceStoppedEvent

Description

Event emitted when service has stopped. None

#### class SystemdNotices

Description

Observe systemd services on your machine base. None

Methods

SystemdNotices.
\_\_init\_\_(

*self*

,
charm: CharmBase

,
services
)

Description

Instantiate systemd notices service. None

SystemdNotices.
subscribe(

*self*
)

Description

Subscribe charmed operator to observe status of systemd services. None

SystemdNotices.
stop(

*self*
)

Description

Stop charmed operator from observing the status of subscribed services. None
