暂时阻止linux关闭

发布于 2024-07-09 14:56:31 字数 635 浏览 7 评论 0原文

我有一个备份脚本,每天在我的 Linux (Fedora 9) 计算机上后台运行。 如果在备份过程中关闭计算机,则备份可能会损坏,因此我想编写一个小脚本,暂时禁用用户重新启动或关闭计算机的能力。

该脚本不一定是不可破解的,只是让系统用户知道备份正在进行中并且他们不应该关闭。 我在 DBus Free 桌面电源管理规范上看到了 Inhibit 方法: http://people.freedesktop.org/~hughsient/temp /power-management-spec-0.3.html 但这只能防止系统在用户未明确请求时空闲的情况下关闭。

有没有一种简单的方法可以在 C/Python/Perl 或 bash 中做到这一点?

更新:为了澄清上述问题,它是一台具有多个用户的计算机,但用户通过插入的键盘/鼠标顺序使用它。 我并不是在寻找一个可以阻止我以 root 身份对其进行“黑客攻击”的系统。 但是,当我从 Gnome/GDM 菜单中选择关闭时,有一个脚本会提醒我(或其他用户)备份仍在运行

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

属性 2024-07-16 14:56:31

另一个入门解决方案:在关机期间,系统运行 /etc/init.d/ 中的脚本(或者实际上是 /etc/rc.*/ 中的脚本,但你明白了。)你可以创建一个该目录中的脚本用于检查备份的状态,并延迟关闭,直到备份完成。 或者更好的是,它会优雅地中断您的备份。

超级用户可以解决此脚本(例如使用 /sbin/halt),但如果超级用户确实下定决心要做某事,则您无法阻止他们做任何事情。

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.

一江春梦 2024-07-16 14:56:31

molly-guard 可以防止意外关闭、重启等等,直到满足所有必需的条件——条件可以自定义。

正如已经建议的,您也可以在关闭过程中执行备份操作。 例如,请参阅此页面

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.

℡Ms空城旧梦 2024-07-16 14:56:31

我不禁觉得你并没有理解 Unix 的比喻,而你所要求的是一个拼凑。

如果用户以 root 身份运行,则 root 无法阻止 root 关闭系统! 您可以做一些装饰性的事情,例如模糊关闭 UI,但这并不能真正完成任何事情。

我无法判断您是否在多用户计算机的上下文中谈论此问题,或者在一台被用作“桌面 PC”且单个用户坐在控制台前的计算机上讨论此问题。 如果是前者,您的用户确实不应该使用可以关闭系统进行日常活动的凭据来访问计算机。 如果是后者,我建议教育用户(a)检查脚本是否正在运行,或者(b)使用您指定的特定关闭脚本来检查脚本的进程并拒绝关闭直到它消失。

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.

束缚m 2024-07-16 14:56:31

更多的是入门而不是完整的解决方案,您可以将 shutdown 命令别名化,然后使用像

#!/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

shutdown 一样保存在用户路径中的脚本。 我预计会有一些变化,具体取决于您的用户调用关闭的方式(窗口管理器图标/命令行),也许也适用于不同的发行版。

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.

我不会写诗 2024-07-16 14:56:31

如果用户要通过 GNOME/KDE 关闭,只需禁止他们这样做。

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

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

淡笑忘祈一世凡恋 2024-07-16 14:56:31

但是一个脚本会提醒我(或其他用户)当我从 Gnome/GDM 菜单中选择关闭时备份仍在运行

人们可以使用 polkit 来完全阻止关闭/重新启动 - 但我未能找到提供一种方法明确答复为什么被阻止。

将以下行添加为 /etc/polkit-1/localauthority/50-local.d/restrict-login-powermgmt.pkla 有效:

[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

您仍然会看到确认对话框,但没有确认按钮。 看起来丑陋,但有效;)

不幸的是,这适用于所有用户,而不仅仅是 lightdm 会话,因此如果需要,您必须添加第二条规则将它们列入白名单。

请注意,此方法仅阻止从 GUI 发出的重新启动/等命令。 要从命令行阻止重新启动/等命令,可以使用 molly-guard - 如 https://askubuntu.com/questions/17187/disabling-shutdown-command-for-all-users-even-root-consequences/17255#17255

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文