[SOLVED] Sending System Command

Post your Gambas programming questions here.
Post Reply
AndyGable
Posts: 359
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

[SOLVED] Sending System Command

Post by AndyGable »

Hi All,

I have a question

Can I use Reboot and Shutdown from Gambas?

I ask as I have a Program that take over my system and it stays in FULL screen (and the terminal only has a 32 keyboard attached)

I have a Menu with in my software that shows the following Menu

1. Exit PoS
2. Reboot PoS
3. Shutdown PoS


how do I reboot a system from with in Gambas?

I tried

Shell "Reboot now"

but it says Reboot not found (as it ha to be down as a SU User)

any recommendation is most welcomed.
Last edited by AndyGable on Monday 7th November 2022 1:45pm, edited 1 time in total.
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Sending System Command

Post by BruceSteers »

AndyGable wrote: Sunday 6th November 2022 10:38pm Hi All,

I have a question

Can I use Reboot and Shutdown from Gambas?

I ask as I have a Program that take over my system and it stays in FULL screen (and the terminal only has a 32 keyboard attached)

I have a Menu with in my software that shows the following Menu

1. Exit PoS
2. Reboot PoS
3. Shutdown PoS


how do I reboot a system from with in Gambas?

I tried

Shell "Reboot now"

but it says Reboot not found (as it ha to be down as a SU User)

any recommendation is most welcomed.
i have commands reboot and shutdown in my system
If sCommand = "Reboot" Then 
  Shell "reboot"
Else If sCommand = "ShutDown" Then
  Shell "shutdown"
Endif
Ps. not a lot of linux commands have capital letters in them

also be sure to not exit the application to soon before its got a chance to run the shutdown/reboot command as you can kill the process before it starts.
If at first you don't succeed , try doing something differently.
BruceS
AndyGable
Posts: 359
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Re: Sending System Command

Post by AndyGable »

BruceSteers wrote: Sunday 6th November 2022 11:45pm
AndyGable wrote: Sunday 6th November 2022 10:38pm Hi All,

I have a question

Can I use Reboot and Shutdown from Gambas?

I ask as I have a Program that take over my system and it stays in FULL screen (and the terminal only has a 32 keyboard attached)

I have a Menu with in my software that shows the following Menu

1. Exit PoS
2. Reboot PoS
3. Shutdown PoS


how do I reboot a system from with in Gambas?

I tried

Shell "Reboot now"

but it says Reboot not found (as it ha to be down as a SU User)

any recommendation is most welcomed.
i have commands reboot and shutdown in my system
If sCommand = "Reboot" Then 
  Shell "reboot"
Else If sCommand = "ShutDown" Then
  Shell "shutdown"
Endif
Ps. not a lot of linux commands have capital letters in them

also be sure to not exit the application to soon before its got a chance to run the shutdown/reboot command as you can kill the process before it starts.
I have this printed in the console window "/bin/sh: 1: reboot: not found

on my Debain machine I have to use SU before I can run reboot is that why I am having issues with this command as well?
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Sending System Command

Post by BruceSteers »

did you try Desktop.RunAsRoot()

or
Shell "pkexec env DISPLAY=" & Env["DISPLAY"] & " XAUTHORITY=" & Env["XAUTHORITY"] & " reboot"
or DBus..
      If DBus.Session.Applications.Exist("org.gnome.SessionManager") Then
        If DBus["org.gnome.SessionManager"]["/org/gnome/SessionManager", "org.gnome.SessionManager"].CanShutdown() Then
          Try DBus["org.gnome.SessionManager"]["/org/gnome/SessionManager", "org.gnome.SessionManager"].Reboot()
          If Error Then Try DBus["org.gnome.SessionManager"]["/org/gnome/SessionManager", "org.gnome.SessionManager"].Shutdown()
         Endif
     Endif
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Sending System Command

Post by BruceSteers »

My Desktop(ish) appication tries to shutdown/reboot in many ways.
Check out the Sysmenu file at
https://gitlab.com/bsteers4/desktop-ish ... Menu.class

It tries a few methods to shutdown/reboot/etc.
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Sending System Command

Post by BruceSteers »

debian puts reboot and shutdown in
/usr/sbin/reboot
/usr/sbin/shutdown

Being in sbin they are sudo only
if you move them to /usr/bin they will work

sudo mv /usr/sbin/reboot /usr/bin/reboot
sudo mv /usr/sbin/shutdown /usr/bin/shutdown
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Sending System Command

Post by BruceSteers »

if you don't want to move the binaries then try dbus

dbus via shell.. (tested on debian11/MATE, needs dbus-send installed)
Shell "dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager.Reboot boolean:true"
Shell "dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager.PowerOff boolean:true"
Gambas commands using gb.dbus component...
DBus["system://org.freedesktop.login1"]["/org/freedesktop/login1", "org.freedesktop.login1.Manager"].Reboot(True)
DBus["system://org.freedesktop.login1"]["/org/freedesktop/login1", "org.freedesktop.login1.Manager"].PowerOff(True)
If at first you don't succeed , try doing something differently.
BruceS
AndyGable
Posts: 359
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Re: Sending System Command

Post by AndyGable »

BruceSteers wrote: Monday 7th November 2022 1:30pm if you don't want to move the binaries then try dbus

dbus via shell.. (tested on debian11/MATE, needs dbus-send installed)
Shell "dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager.Reboot boolean:true"
Shell "dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager.PowerOff boolean:true"
Gambas commands using gb.dbus component...
DBus["system://org.freedesktop.login1"]["/org/freedesktop/login1", "org.freedesktop.login1.Manager"].Reboot(True)
DBus["system://org.freedesktop.login1"]["/org/freedesktop/login1", "org.freedesktop.login1.Manager"].PowerOff(True)

@BruceSteers you sir are a GENUINES.
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Sending System Command

Post by BruceSteers »

also,,,,
if gnome exists..
DBus["session://org.gnome.SessionManager"]["/org/gnome/SessionManager", "org.gnome.SessionManager"].RequestShutdown()
DBus["session://org.gnome.SessionManager"]["/org/gnome/SessionManager", "org.gnome.SessionManager"].RequestReboot()

I try them all to try to make them work on any system. not all systems support freedesktop.login1 method ...
this tries gnome.sessionmanager / kde.sessionmanager and freedesktop.login1 methods and also checks for cancelled request...
Note some shutdown methods will just shutdown while other will pop up the shutdown message window that offers options.

      If DBus.Session.Applications.Exist("org.gnome.SessionManager") Then
        If DBus["org.gnome.SessionManager"]["/org/gnome/SessionManager", "org.gnome.SessionManager"].CanShutdown() Then
          Try DBus["org.gnome.SessionManager"]["/org/gnome/SessionManager", "org.gnome.SessionManager"].RequestShutdown()
          If Error Then
            Print Error.Text
            If LCase(Error.Text) Like "*cancelled*" Then
              Error.Raise("Canceled")
              Return
            Endif
            Print Error.Text
          Else
            Return
          Endif
        Endif

      Else If DBus.Session.Applications.Exist("org.kde.Shutdown") Then
        DBus["session://org.kde.Shutdown"]["/Shutdown", "org.kde.Shutdown"].logoutAndShutdown()

      Endif

      Try DBus["system://org.freedesktop.login1"]["/org/freedesktop/login1", "org.freedesktop.login1.Manager"].PowerOff(True)
Download the Gambas DBus Explorer from the farm to see what DBus has to offer :)
Or better still use my version.
I modified the DBus explorer to copy almost usable command text when double clicking an item.
so if i doubleclick the gnome session manager CanShutdown item i get this copied to my clipboard..
DBus["session://org.gnome.SessionManager"]["/org/gnome/SessionManager", "org.gnome.SessionManager"].CanShutdown() ' As Boolean

EDIT: oops forgot to attach the explorer
Attachments
DBusExplorer-1.1.tar.gz
(118.53 KiB) Downloaded 63 times
If at first you don't succeed , try doing something differently.
BruceS
Post Reply