Strict Confined Snap System Reboot? How To w/Python

How do you reboot the computer, inside a STRICT confined snap for Ubuntu Desktop? (w/python code)

since this recently comes up more often here, i quickly threw together a snap:

name: reboot-test
base: core20
version: '0.1' # just for humans, typically '1.2+git' or '1.3.2'
summary: Single-line elevator pitch for your amazing snap # 79 char long summary
description: |
  This is my-snap's description. You have a paragraph or two to tell the
  most important story about your snap. Keep it under 100 words though,
  we live in tweetspace and your description wants to look good in the snap
  store.

grade: stable
confinement: strict

apps:
  reboot:
    command: usr/bin/reboot
    environment:
      PYTHONPATH: $SNAP/usr/lib/python3/dist-packages:$PYTHONPATH
    plugs:
      - shutdown

parts:
  reboot:
    plugin: dump
    source: .
    organize:
      reboot.py: usr/bin/reboot
    stage-packages:
      - python3-dbus

and the reboot.py script:

#! /usr/bin/env python3

import dbus

bus = dbus.SystemBus()
bus_object = bus.get_object("org.freedesktop.login1", "/org/freedesktop/login1")
bus_object.Reboot(True, dbus_interface="org.freedesktop.login1.Manager")

calling it as: sudo reboot-test.reboot will then reboot your system …

4 Likes

Hi @ogra. If I want to call the restart.py script from another python code, should I use this?

import os
os.system("python3 /path_of_script/restart.py")

Or would you like to suggest any other method of calling this restart script that would work?

i’d just copy the code into a function in your python code instead of shelling out to a system() call … but totally up to you :slight_smile:

Thanks for the suggestion, I will try it out

Hi @ogra. It worked. I put the codes from the restart.py file inside a function and called it when needed and it successfully restarted the system. Thank you so much.

@ogra Thank you so much! This has been taking all our time for the past few weeks. You’re awesome!

1 Like