Linux Shutdown with dbus-send
HAL, which function you are calling, does not provide this feature. But you can always go with something like this, if you are launching an external command anyway:
sh -c "sleep 1h; dbus-send --print-reply --system --dest=org.freedesktop.Hal /org/freedesktop/Hal/devices/computer org.freedesktop.Hal.Device.SystemPowerManagement.Shutdown"
Linux: Programatically shutdown or reboot computer from a user-level process
You can use ConsoleKit. Send a org.freedesktop.ConsoleKit.Manager.Stop
DBus message to org.freedesktop.ConsoleKit
. From the command line, that would be something like:
dbus-send \
--system \
--dest=org.freedesktop.ConsoleKit \
--type=method_call \
--print-reply \
--reply-timeout=2000 \
/org/freedesktop/ConsoleKit/Manager \
org.freedesktop.ConsoleKit.Manager.Stop
If the current user is authorized to perform shutdown, then no root privileges are needed.
You can also take a look at the KShutdown utility. It contains source code for different shutdown methods, ranging from ConsoleKit to Gnome and KDE APIs.
How to shutdown/reboot linux machine programatically without using commands(Runtime)
So I managed to find the solution on my own after I did some research online. There is a library called JNA(Java Native Access) that lets you load libraries natively and resolves and dependencies. Using JNA I loaded the POSIX library on java through an interface. Running your code as sudo will let you shut down or reboot the machine. Here is the code.
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.ptr.IntByReference;
public interface CLibrary extends Library {
public CLibrary INSTANCE = (CLibrary) Native.loadLibrary(Platform.isWindows()?"msvcrt":"c",CLibrary.class);
int kill(long pid, int sig);
int reboot(int magic, int magic2, int cmd, IntByReference arg);
int reboot(int cmd);
int LINUX_REBOOT_MAGIC1 = 0xfee1dead;
int LINUX_REBOOT_MAGIC2 = 672274793;
int LINUX_REBOOT_MAGIC2A = 85072278;
int LINUX_REBOOT_MAGIC2B = 369367448;
int LINUX_REBOOT_MAGIC2C = 537993216;
int LINUX_REBOOT_CMD_RESTART = 0x01234567;
int LINUX_REBOOT_CMD_HALT = 0xCDEF0123;
int LINUX_REBOOT_CMD_CAD_ON =0x89ABCDEF;
int LINUX_REBOOT_CMD_CAD_OFF = 0x00000000;
int LINUX_REBOOT_CMD_POWER_OFF = 0x4321FEDC;
int LINUX_REBOOT_CMD_RESTART2 = 0xA1B2C3D4;
int LINUX_REBOOT_CMD_SW_SUSPEND = 0xD000FCE2;
int LINUX_REBOOT_CMD_KEXEC = 0x45584543;
}
p s v main(){
CLibrary.INSTANCE.reboot(Clibrary.LINUX_REBOOT_CMD_POWER_OFF);
}
Note: This works only on Linux and not on Windows. To shutdown a machine programmatically look for ExitWindowsEx. Built into the JNA library.
All C POSIX headers files can be found C POSIX Library - Wikipedia
How to detect pending system shutdown on Linux?
There is no way to determine if a SIGTERM
is a part of a shutdown sequence. To detect a shutdown sequence you can either use use rc.d
scripts like ereOn and Eric Sepanson suggested or use mechanisms like DBus.
However, from a design point of view it makes no sense to ignore SIGTERM
even if it is not part of a shutdown. SIGTERM
's primary purpose is to politely ask apps to exit cleanly and it is not likely that someone with enough privileges will issue a SIGTERM
if he/she does not want the app to exit.
Remotely shutdown/restart a Linux machine without password
It takes root privileges to restart a Linux machine. Some desktop environments use a daemon to get around this.... but I suggest editing the sudoers file
https://help.ubuntu.com/community/Sudoers for a howto. Basically you'll want to allow the restart command - and only the restart command - to be run without a password.
Something like:
ALL ALL=(ALL) NOPASSWD: /usr/sbin/shutdown
will let any user on the machine restart it without using a password. You'll probably need to prefix the 'shutdown' in your system command with 'sudo', though it looks like it's being called automatically somehow. If this isn't secure enough, you can make a group, make your program run as that group, then allow that group to restart.
EDIT: Apparently this can be done with DBus (note: I haven't tested this):
dbus-send --system --print-reply --dest="org.freedesktop.Hal" /org/freedesktop/Hal/devices/computer org.freedesktop.Hal.Device.SystemPowerManagement.Restart int32:0
This works because dbus runs as root (or has root privs) and can therefore accept requests to restart from nonpriveleged processes and act on them. I still think the sudo way is cleaner, and so will anyone who maintains this code.
use powershell and plink to shutdown remote linux computer with sudo command
Make sure that the user can indeed run sudo
commands.
The issue might be that the user that is trying to run the command is requested to insert the sudo password.
In the sudoers
file insert <YOUR_USERNAME> ALL=(ALL) NOPASSWD
Related Topics
Installing PHPsh on Linux, Python Error
In Linux, Do There Exist Functions Similar to _Clearfp() and _Statusfp()
How to Write on Serial Port Using Qextserialport
How to Run an Image Windows Docker on Ubuntu
Linux: Triggering Shell Command on File Save
Avoid Copying of Data Between User and Kernel Space and Vice-Versa
Question About Epoll and Splice
Is It Necessary to Flush Write Combine Memory Explicitly by Programmer
How to Avoid High CPU Usage While Reading/Writing Character Device
Jmeter:Difference Between Jmeter.Sh and Jmeter Without Extension File in Jmeter
How Does Iwlist() Command Scans The Wireless Networks
Why Sigint Can Stop Bash in Terminal But Not via Kill -Int
Qemu Simple Backend Tracing Dosen'T Print Anything
Ansible - Winrm or Requests Is Not Installed
Can Not Add New User in Docker Container with Mounted /Etc/Passwd and /Etc/Shadow