10

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.

  • 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 '12 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 '12 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 '12 at 10:53
1

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.

  • I'm thinking of using an alias, now I just have to write my script. Thanks! – zang3tsu Mar 23 '12 at 7:07
  • What about other commands such as halt or poweroff. Would you need to create alias' / rename them too? – mintyfreshpenguin Aug 25 '14 at 18:43
16

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'.
  • There is this issue though: running it as root non-interactively will ignore the locks. github.com/systemd/systemd/issues/2680 – Alicia Apr 21 '16 at 23:10
  • I think you misread that bug report, they are talking about running poweroff as root non-interactive – Ferrybig Aug 23 '17 at 16:04
  • How did I misread the bug report exactly? :S – Alicia Aug 23 '17 at 20:16
2

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.

  • What about halt, poweroff, and systemctl poweroff? – Martin Ueding Aug 11 '18 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
  • can you elaborate your answer ? shutdown may not be explicitly specify in /etc/sudoers. – Archemar Oct 17 '14 at 9:21
  • Sure, I edited the answer with a better explanation. Cheers – vmule Oct 17 '14 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)

-1

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.