Skip to content

Example to restart/start/stop systemd1 unit and login1 poweroff/reboot #288

@stephenhu

Description

@stephenhu

firstly, i need to apologize as i know this is abuse of the issues system for godbus/dbus, but i had been struggling with using godbus/dbus even after looking at the examples, freedesktop documentation, busctl documentation, and github.com/coreos/go-systemd projects and i didn't know of a better place to put this, but hopefully this can help other users that want to leverage this great library, i will blog about this sometime later.

anyway, long story short, here's some sample code of how to use the godbus/dbus to call systemd1 and login1, i had initially used os.Exec to call out to systemctl directly, but felt that dbus is a cleaner and more programmatic way to do this.

github.com/stephenhu/rpic

you'll need to add this file (probably a security issue, use at your own risk as this allows scripts access without the need for interactive authentication which circumvents the intent of systemd, but i think with my use case of a raspberry pi appliance, my device needs to allow programmatic access to reboot) /etc/polkit-1/localauthority/50-local.d/45-piguard.pkla with the following contents:

'''
[Allow specific user to bypass interactive authentication]
Identity=unix-user:xyz
Action=org.freedesktop.systemd1.manage-units;org.freedesktop.login1.power_off;org.freedesktop.login1.reboot
ResultAny=yes
ResultInactive=yes
ResultActive=yes
'''

note that i am using ubuntu 20.04.3 which has polkit 0.105 by default which doesn't support .rules files, so i am forced to use .pkla files instead.

this is not the best code, but it shows you how to end to end restart/start/stop a wireguard service and shutdown/reboot a server.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions