How to halt Linux and windows systems immediately and safely by pressing power button

rupeshforu3

Disciple
Hi I am Rupesh from India and I have bought new PC with Intel i3 10th gen processor and asus prime h510 me motherboard. At present I have installed Fedora 35 and windows 11 and it is running properly.

I am living in a place where there is no constant power supply and so I have bought new ups power supply. Now my requirement is I want to shutdown the system properly and immediately by unmounting all file systems and abort all processes. Generally this can be done by pressing power off or shutdown button in desktop environments like gnome desktop or kde. There is no time to do this because my ups can supply power upto two minutes only. So is there any way to shutdown the windows and Linux systems properly by pressing power button present in cabinet box.

Many of you may ask what is the need of shutting down system immediately. If I don't do this my file system may be corrupted and even hardware may be damaged.

I have seen some options in Linux such as "select what to do when you press power button" and I have selected to shutdown but it is not working properly.

Is there any way to write a script in both windows and Linux systems to shutdown system properly and immediately when I press power button.

Regards,
Rupesh.
 
You can also use the hibernate option to quickly save the current state to your disk in both Windows and Linux, but need to enable it first.
 
For Linux,
Bash:
sudo poweroff

For Windows,
Bash:
shutdown.exe /s /t 00

Run this in a script and you're done. Modern OS can take care of a graceful shutdown.
How to execute automatically the commands given by you when I press power button.

You can also use the hibernate option to quickly save the current state to your disk in both Windows and Linux, but need to enable it first.
I don't prefer hibernate and may I know how to power off system properly and immediately by pressing power button present in cabinet.

May I know how to run poweroff or shutdown commands automatically when I press power button present in cabinet box.

Here the issue is there is no time to enter shutdown command in terminal emulator or press other button because even ups may not function after two minutes.
 
Last edited:
This is the settings for power button options in Windows.


xfdfdvsd.jpg
 
I don't prefer hibernate and may I know how to power off system properly and immediately by pressing power button present in cabinet.
In Linux you can edit the ACPI power events to shutdown the system by physically pressing the cabinet power button. The answer is from a member based on ubuntu 18.04 link. The basic steps are:

1. Create a file with the following contents at /etc/acpi/events/power:
Code:
event=button/power
action=/sbin/poweroff
2. Restart the service using sudo service acpid restart
 
Someone else from Fedora forums suggested as follows.

Try press and hold the power button for about 4 seconds.
It works on every machine I have here.
(It's a hobby, but I like it)

Can I apply this method to both windows and Linux.
 
Someone else from Fedora forums suggested as follows.

Try press and hold the power button for about 4 seconds.
It works on every machine I have here.
(It's a hobby, but I like it)

Can I apply this method to both windows and Linux.
This does a hard shutdown on most laptops/desktops. OS does not come into the picture, the long clock is handled at a hardware level. Which means filesystems will not be unmounted, services will not be stopped, etc.

On most common filesystems in Linux including ext4, xfs, btrfs, there is less than 1 in a billion chances of an issue. Even if you are on cryptsetup luks encrypted filesystem. But if there is any specialized service you are using that doesn't behave well with hard shutdown, you'll be in trouble.
 
Last edited:
Yes others in Unix forums suggested not to do as you said.

In Unix forums they also suggested to run "init 0" with super user permissions. They said that this is the safest and fastest way of shutting down Linux system.
 
In /etc/systemd directory there is a file called logind.conf and in this file there is the following line

#HandlePowerKey=poweroff

Is there any way to replace poweroff with init 0.

At present when I press power button present in cabinet box it is asking for confirmation whether to power off or not. Instead I want to power off with out any confirmation.
 
What I do for quick shutdown is :

1. First map ctrl-alt-del to poweroff.target

2. When I want to shutdown, ctrl-alt-f2 followed by ctrl-alt-del

If you skip step 1 (I'm not at the computer now, so can't give exact command for that), step 2 will make it instant reboot cleanly. When it comes to BIOS for next boot, you can cutoff power.
 
There is no time to do this because my ups can supply power upto two minutes only.
If you turn off the monitor then you may get 4 minutes out of UPS.

Why is your power button not working? What happens if you press it?

If it's not working properly then it means your BIOS settings are amiss. Check your BIOS settings.
 
If you turn off the monitor then you may get 4 minutes out of UPS.

Why is your power button not working? What happens if you press it?

If it's not working properly then it means your BIOS settings are amiss. Check your BIOS settings.
I guess power button is working. But a short press on power button makes the OS ask for confirmation, so need monitor & mouse or keyboard working and wastes valuable time. Long press on power button bypasses the OS, so is a little unsafe.



@Rupesh, ctrl-alt-del can be mapped to power off by

Bash:
sudo ln -sf  /usr/lib/systemd/system/poweroff.target /etc/systemd/system/ctrl-alt-del.target

After this, ctrl-alt-f2 followed by ctrl-alt-del will instantly start shutting down, without any confirmation.
 
Last edited:
But a short press on power button makes the OS ask for confirmation
I think some of your applications aren't closing with shutdown, that's why you are getting the confirmation box. In that case 'shutdown /s /t 0' is going to yield same result. What you need is 'shutdown /s /f /t 0', which will force the shutdown without confirmation box but you may lose data with application who don't feel like to close. Forcing shutdown is the fastest way to shutdown.


Secondly, you should really think about putting computer to sleep/hibernate if you are using SSD. Sleep on windows 10 is a hybrid sleep, meaning it hibernates too and hence there's no need to enable hibernation separately. In case if you have 'Hyper-V' enabled then hybrid sleep won't work and you'll have to use hibernation.

These days far too few people actually shutdown the computer. I do it once in two months when forced update restart is imminent. :p
 
I think some of your applications aren't closing with shutdown, that's why you are getting the confirmation box. In that case 'shutdown /s /t 0' is going to yield same result. What you need is 'shutdown /s /f /t 0', which will force the shutdown without confirmation box but you may lose data with application who don't feel like to close. Forcing shutdown is the fastest way to shutdown.


Secondly, you should really think about putting computer to sleep/hibernate if you are using SSD. Sleep on windows 10 is a hybrid sleep, meaning it hibernates too and hence there's no need to enable hibernation separately. In case if you have 'Hyper-V' enabled then hybrid sleep won't work and you'll have to use hibernation.

These days far too few people actually shutdown the computer. I do it once in two months when forced update restart is imminent. :p
Interesting. I personally prefer power button to suspend, and even that I use rarely, so I didn't come across such things.

Though this problem is Rupesh's, and I use Linux exclusively. I was only suggesting Linux solutions, maybe I didn't make it clear :). Your information should surely help people who use Windows, including OP here.
 
I guess power button is working. But a short press on power button makes the OS ask for confirmation, so need monitor & mouse or keyboard working and wastes valuable time. Long press on power button bypasses the OS, so is a little unsafe.



@Rupesh, ctrl-alt-del can be mapped to power off by

Bash:
sudo ln -sf /usr/lib/systemd/system/poweroff.target /etc/systemd/system/ctrl-alt-del.target

After this, ctrl-alt-f2 followed by ctrl-alt-del will instantly start shutting down, without any confirmation.
Hi I have created symbolic link as you said and at present when I press ALT ctrl del it is asking for confirmation instead of shutting down.
 
Hi I have created symbolic link as you said and at present when I press ALT ctrl del it is asking for confirmation instead of shutting down.
Did you do ctrl-alt-F2 before ctrl-alt-del ? You need to be out of the graphical display for it to work. ctrl-alt-F2 will do it for a majority of situations.
 
Currently I am using Fedora 36 pre release.

Previously while booting Linux system I was able to see console login by pressing alt ctrl F1 or alt ctrl F2 etc.,. At present when I am running desktop environments like gnome mate etc., when I press ALT ctrl F1 or alt ctrl F2 etc I am unable to see console and instead I am still in desktop environment itself.
 
Back
Top