Temporarily prevent linux from shutting down

时光总嘲笑我的痴心妄想 提交于 2019-12-04 00:13:01

问题


I have a backup script that runs in the background daily on my linux (Fedora 9) computer. If the computer is shut down while the backup is in progress the backup may be damaged so I would like to write a small script that temporarily disables the ability of the user to reboot or shut the computer down.

It is not necessary that the script is uncirumventable, it's just to let the users of the system know that the backup is in progress and they shouldn't shut down. I've seen the Inhibit method on the DBus Free desktop power management spec: http://people.freedesktop.org/~hughsient/temp/power-management-spec-0.3.html but that only prevents shutdowns if the system is idle not explicitly at the users request.

Is there an easy way to do this in C/Python/Perl or bash?

Update: To clarify the question above, it's a machine with multiple users, but who use it sequentially via the plugged in keyboard/mouse. I'm not looking for a system that would stop me "hacking" around it as root. But a script that would remind me (or another user) that the backup is still running when I choose shut down from the Gnome/GDM menus


回答1:


Another get-you-started solution: During shutdown, the system runs the scripts in /etc/init.d/ (or really, a script in /etc/rc.*/, but you get the idea.) You could create a script in that directory that checks the status of your backup, and delays shuts down until the backup completes. Or better yet, it gracefully interrupts your backup.

The super-user could workaround this script (with /sbin/halt for example,) but you can not prevent the super-user for doing anything if their mind is really set into doing it.




回答2:


There is molly-guard to prevent accidental shutdows, reboots etc. until all required conditions are met -- conditions can be self-defined.

As already suggested you can as well perform backup operations as part of the shutdown process. See for example this page.




回答3:


If users are going to be shutting down via GNOME/KDE, just inhibit them from doing so.

http://live.gnome.org/GnomePowerManager/FAQ#head-1cf52551bcec3107d7bae8c332fd292ec2261760




回答4:


More a get-you-started than a complete solution, you could alias the shutdown command away, and then use a script like

#!/bin/sh
ps -ef|grep backupprocess|grep -v grep > /dev/null
if [ "$?" -eq 0 ]; then
 echo Backup in progress: aborted shutdown
 exit 0
else
 echo Backup not in progress: shutting down
 shutdown-alias -h now
fi

saved in the user's path as shutdown. I expect there would be some variation dependant on how your users invoke shutdown (Window manager icons/command line) and perhaps for different distros too.




回答5:


I can't help but feel that you're not grokking the Unix metaphor, and what you're asking for is a kludge.

If a user running as root, there's nothing root can do to stop root from shutting down the system! You can do window dressing things like obscuring shutdown UI, but that's not really accomplishing anything.

I can't tell if you're talking about this in the context of a multi-user machine, or a machine being used as a "desktop PC" with a single user sitting at a console. If it's the former, your users really shouldn't be accessing the machine with credentials that can shutdown the system for day-to-day activities. If it's the latter, I'd recommend educating the users to either (a) check that the script is running, or (b) use a particular shutdown script that you designate that checks for the script's process and refuses to shutdown until it's gone.




回答6:


But a script that would remind me (or another user) that the backup is still running when I choose shut down from the Gnome/GDM menus

One may use polkit to completely block shutdown/restart - but I failed to find method that would provide a clear response why it is blocked.

Adding the following lines as /etc/polkit-1/localauthority/50-local.d/restrict-login-powermgmt.pkla works:

[Disable lightdm PowerMgmt]
Identity=unix-user:*
Action=org.freedesktop.login1.reboot;org.freedesktop.login1.reboot-multiple-sessions;org.freedesktop.login1.power-off;org.freedesktop.login1.power-off-multiple-sessions;org.freedesktop.login1.suspend;org.freedesktop.login1.suspend-multiple-sessions;org.freedesktop.login1.hibernate;org.freedesktop.login1.hibernate-multiple-sessions
ResultAny=no
ResultInactive=no
ResultActive=no

You still see a confirmation dialog but there are not buttons to confirm. Looks ugly, but works ;)

Unfortunately this applies to all users, not only the lightdm session, so you have to add a second rule to white-list them if desired.

Note that this method block solely reboot/etc commands issued from GUI. To block reboot/etc commands from command line one may use molly-guard - as explained in https://askubuntu.com/questions/17187/disabling-shutdown-command-for-all-users-even-root-consequences/17255#17255



来源:https://stackoverflow.com/questions/322254/temporarily-prevent-linux-from-shutting-down

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!