Temporarily prevent linux from shutting down

前端 未结 6 2051
暖寄归人
暖寄归人 2021-01-11 14:06

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 dama

相关标签:
6条回答
  • 2021-01-11 14:16

    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.

    0 讨论(0)
  • 2021-01-11 14:17

    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

    0 讨论(0)
  • 2021-01-11 14:24

    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.

    0 讨论(0)
  • 2021-01-11 14:26

    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

    0 讨论(0)
  • 2021-01-11 14:32

    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.

    0 讨论(0)
  • 2021-01-11 14:32

    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.

    0 讨论(0)
提交回复
热议问题