Alexays / Waybar

Highly customizable Wayland bar for Sway and Wlroots based compositors. :v: :tada:
MIT License
6.49k stars 696 forks source link

[Feature Request] systemd system/user unit control #1187

Open somini opened 3 years ago

somini commented 3 years ago

It should be possible to start, stop and monitor the state of systemd units, both for system and user.

For system units, there's the issue of requesting credentials, but that is not a problem for user units.

It should be possible to use the systemd dbus interface to avoid costly polling, instead of this custom module does:

    "custom/systemd#information": {
    "format": "§ {}",
    "interval": 5,
    "exec": "systemctl-state-waybar --user $UNIT",
    "on-click": "systemctl-toggle -u $UNIT",
    "on-click-right": "systemctl --user stop $UNIT"
    }
Anakael commented 3 years ago

Do you have idea how it should looks like? I think it can be useful only if several units can be attached to this module.

somini commented 3 years ago

There's already support for multiple blocks with separate configuration, right? It's enough to have a single dbus connection across all systemd sections.

Or maybe you mean the configuration? This is what I would do, for daemon1:

systemd#user_daemon1: {
  "unit": "$UNIT",
  "user": true,
  "format": "{unit}",
  "on-click": "systemctl-toggle {manager} {unit}",
  "on-click-right": "systemctl {manager} stop {unit}",
}

I can also share the bash scripts I'm using on this example:

systemd-state-waybar

Uses the old i3blocks protocol:

#!/bin/sh
set -e
what='--system'
unit="$1"
if [ "$1" = '--user' ]; then
    what="$1"
    unit="$2"
fi
text="${3:-$unit}"

echo "$text"
echo "Unit: $unit"
if systemctl --quiet "$what" is-active "$unit"; then
    echo "good"
else
    echo "bad"
fi
exit 0

systemctl-toggle

#!/bin/bash
extra_args=()
check_command='is-active'
start_command='start'
stop_command='stop'

usage() {
    # shellcheck disable=SC2016
    (
    echo "Usage: $0 [ -u ] [ -c CHECK_COMMAND ] UNIT"
    echo '  -u: Talk to the `--user` systemd daemon'
    )>&2
    exit 1
}

while getopts ':uc:' opt; do
    case "$opt" in
        u)
            extra_args+=('--user')
            ;;
        ?)
            usage
    esac
done
shift $((OPTIND - 1))
if [ $# -lt 1 ]; then
    usage
fi

unit="$1"

if systemctl "${extra_args[@]}" --quiet "$check_command" "$unit"; then
    exec systemctl "${extra_args[@]}" "$stop_command" "$unit"
else
    exec systemctl "${extra_args[@]}" "$start_command" "$unit"
fi
somini commented 2 years ago

@Alexays Sorry, was this close as "WONTFIX", or something like this was implemented now?

Alexays commented 2 years ago

Ohh sorry misunderstood the issue, will reopen it