17

What is the simplest way to disable or temporarily suspend reboot/shutdown when an important process is running? The process takes too long to finish and cannot be paused/resumed so I like to avoid shutting down the pc while it is running. It is run from cron so unless I manually check for running processes, I wouldn't know that it is running. Thanks.

3
  • 5
    Possible duplicate: SO: Temporarily prevent linux from shutting down, the answers hint on the various levels on which this can be done, i.e. shell scripts or the desktop environment's power management.
    – sr_
    Mar 19, 2012 at 9:07
  • I don't think that answer is specific enough without an either explanation of runlevels or link to explanation. It's not in the U&L SE. May I ask which distro and version you're using?
    – bsd
    Mar 19, 2012 at 22:13
  • Rather than modifying the shutdown procedure, write a wrapper so the job indicates its finished, either mail or touch a file. Test for job completion, then run shutdown. (cron mails the output of its jobs to owner, but that can be tweaked).
    – bsd
    Mar 22, 2012 at 10:53

6 Answers 6

34

For newer systems with systemd this has been solved by systemd-inhibit. Example of usage:

systemd-inhibit --why="Doing weekly backup" bash my-backups.sh

Then, if a user attempts to shutdown it will not be allowed unless forced.

❯ systemctl poweroff
Operation inhibited by "bash my-backups.sh" (PID 2414 "systemd-inhibit", user ntrrgc),
reason is "Doing weekly backup".
Please retry operation after closing inhibitors and logging out other users.
Alternatively, ignore inhibitors and users with 'systemctl poweroff -i'.
4
  • There is this issue though: running it as root non-interactively will ignore the locks. github.com/systemd/systemd/issues/2680
    – Alicia
    Apr 21, 2016 at 23:10
  • I think you misread that bug report, they are talking about running poweroff as root non-interactive
    – Ferrybig
    Aug 23, 2017 at 16:04
  • 1
    How did I misread the bug report exactly? :S
    – Alicia
    Aug 23, 2017 at 20:16
  • @Alicia I tried your suggestion on CentOS and it immediately run my script and don't add in systemd-inhibit list. Apr 29, 2021 at 10:24
3

Run which shutdown to see where the path to the shutdown program is. You can rename the file, although I recommend against it.

Another (safer) method. Use an alias: alias shutdown=' ' Something like this is more reversible. If you're trying to prevent shutdown from all users, add the alias globally.

4
  • I'm thinking of using an alias, now I just have to write my script. Thanks!
    – zang3tsu
    Mar 23, 2012 at 7:07
  • What about other commands such as halt or poweroff. Would you need to create alias' / rename them too? Aug 25, 2014 at 18:43
  • Just keep in mind that sudo doesn't expand aliases by default. To protect from sudo reboot, you need to also alias sudo='sudo '
    – Expurple
    Jan 24, 2022 at 13:56
  • The alias method does not work with background processes, such as monitors and web management interfaces. I ended up resorting to renaming poweroff and shutdown, leaving halt as an option that I can use from the shell. This worked for me.
    – Sam Sirry
    Feb 6, 2022 at 14:35
3

Disable the "shutdown/reboot/poweroff/halt/hostname" like commands in mission critical servers

chmod 0 /sbin/shutdown

then if you need to use it, chmod it back to chmod 0755.

1
  • 5
    What about halt, poweroff, and systemctl poweroff? Aug 11, 2018 at 6:57
1

I would disable the command in the /etc/sudoers file so that only root can execute it.

in /etc/sudoers

# Cmnd alias specification
Cmnd_Alias     SHUTDOWN = /sbin/shutdown

# User privilege specification
root    ALL=(ALL:ALL) ALL
vmule   ALL=(ALL:ALL) ALL, !SHUTDOWN

and when I try to run the command:

vmule@debian:~$ sudo /sbin/shutdown  -r now
Sorry, user vmule is not allowed to execute '/sbin/shutdown -r now' as root on debian.vm
2
  • can you elaborate your answer ? shutdown may not be explicitly specify in /etc/sudoers.
    – Archemar
    Oct 17, 2014 at 9:21
  • Sure, I edited the answer with a better explanation. Cheers
    – vmule
    Oct 17, 2014 at 9:51
0

I wanted to prevent a local user to shutdown a laptop during an upgrade. Disabling the input device was the best way to go. For me it was sufficient to disable the mouse, but the keyboard can be disabled as well.

Of course there's a drawback, the computer can't be used during this period.

Disable Keyboard & Mouse input on unix (under X)

0

shutdown is technically an application too, which you can remove. Run which shutdown. which is a command which shows you the path of an application. In my case the output is /sbin/shutdown. It's a file so you can just do rm shutdown. Or if you want to disable it temporarily copy it, change the name and remove the original file.

You must have the sufficient permissions if you want to do rm shutdown. It's read-only, hence why you may get an error. Try removing it as root or if necessary try using the command rm shutdown.

1
  • 4
    Why the -r flag? You wont need it for one file, and if you mistype something, disaster ensues.
    – MadTux
    Aug 25, 2014 at 19:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .