[SOLVED] Sending System Command

Post

Posted
Rating:
#1 (In Topic #925)
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
 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.
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’

AndyGable said

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

Code (gambas)

  1. If sCommand = "Reboot" Then
  2.   Shell "reboot"
  3. Else If sCommand = "ShutDown" Then
  4.   Shell "shutdown"
  5.  

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.
Online now: No Back to the top

Post

Posted
Rating:
#3
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

BruceSteers said

AndyGable said

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

Code (gambas)

  1. If sCommand = "Reboot" Then
  2.   Shell "reboot"
  3. Else If sCommand = "ShutDown" Then
  4.   Shell "shutdown"
  5.  

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?
Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’
did you try Desktop.RunAsRoot()

or

Code (gambas)

  1. Shell "pkexec env DISPLAY=" & Env["DISPLAY"] & " XAUTHORITY=" & Env["XAUTHORITY"] & " reboot"
  2.  

or DBus..

Code (gambas)

  1.       If DBus.Session.Applications.Exist("org.gnome.SessionManager") Then
  2.         If DBus["org.gnome.SessionManager"]["/org/gnome/SessionManager", "org.gnome.SessionManager"].CanShutdown() Then
  3.           Try DBus["org.gnome.SessionManager"]["/org/gnome/SessionManager", "org.gnome.SessionManager"].Reboot()
  4.           If Error Then Try DBus["org.gnome.SessionManager"]["/org/gnome/SessionManager", "org.gnome.SessionManager"].Shutdown()
  5.          Endif
  6.      Endif
  7.  
Online now: No Back to the top

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘Guru’
My Desktop(ish) appication tries to shutdown/reboot in many ways.
Check out the Sysmenu file at
.src/SysMenu/fMenu.class · main · Bruce Steers / Desktop-ish · GitLab

It tries a few methods to shutdown/reboot/etc.
Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’
 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
Online now: No Back to the top

Post

Posted
Rating:
#7
Guru
BruceSteers is in the usergroup ‘Guru’
if you don't want to move the binaries then try dbus

dbus via shell..  (tested on debian11/MATE, needs dbus-send installed)

Code (gambas)

  1. Shell "dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager.Reboot boolean:true"
  2.  

Code (gambas)

  1. Shell "dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager.PowerOff boolean:true"
  2.  

Gambas commands using gb.dbus component…

Code (gambas)

  1. DBus["system://org.freedesktop.login1"]["/org/freedesktop/login1", "org.freedesktop.login1.Manager"].Reboot(True)
  2.  

Code (gambas)

  1. DBus["system://org.freedesktop.login1"]["/org/freedesktop/login1", "org.freedesktop.login1.Manager"].PowerOff(True)
  2.  
Online now: No Back to the top

Post

Posted
Rating:
#8
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

BruceSteers said

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

dbus via shell..  (tested on debian11/MATE, needs dbus-send installed)

Code (gambas)

  1. Shell "dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager.Reboot boolean:true"
  2.  

Code (gambas)

  1. Shell "dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager.PowerOff boolean:true"
  2.  

Gambas commands using gb.dbus component…

Code (gambas)

  1. DBus["system://org.freedesktop.login1"]["/org/freedesktop/login1", "org.freedesktop.login1.Manager"].Reboot(True)
  2.  

Code (gambas)

  1. DBus["system://org.freedesktop.login1"]["/org/freedesktop/login1", "org.freedesktop.login1.Manager"].PowerOff(True)
  2.  


BruceSteers  you sir are a GENUINES.
Online now: No Back to the top

Post

Posted
Rating:
#9
Guru
BruceSteers is in the usergroup ‘Guru’
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.

Code (gambas)

  1.  
  2.       If DBus.Session.Applications.Exist("org.gnome.SessionManager") Then
  3.         If DBus["org.gnome.SessionManager"]["/org/gnome/SessionManager", "org.gnome.SessionManager"].CanShutdown() Then
  4.           Try DBus["org.gnome.SessionManager"]["/org/gnome/SessionManager", "org.gnome.SessionManager"].RequestShutdown()
  5.           If Error Then
  6.             Print Error.Text
  7.             If LCase(Error.Text) Like "*cancelled*" Then
  8.               Error.Raise("Canceled")
  9.               Return
  10.             Endif
  11.             Print Error.Text
  12.           Else
  13.             Return
  14.           Endif
  15.         Endif
  16.  
  17.       Else If DBus.Session.Applications.Exist("org.kde.Shutdown") Then
  18.         DBus["session://org.kde.Shutdown"]["/Shutdown", "org.kde.Shutdown"].logoutAndShutdown()
  19.  
  20.       Endif
  21.  
  22.       Try DBus["system://org.freedesktop.login1"]["/org/freedesktop/login1", "org.freedesktop.login1.Manager"].PowerOff(True)
  23.  

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

Attachment
Online now: No Back to the top
1 guest and 0 members have just viewed this.