是否可以设置 Ubuntu 使其在脚本完成之前不关闭?

是否可以设置 Ubuntu 使其在脚本完成之前不关闭?

我使用脚本将 btrfs 分区从一个磁盘增量备份到另一个磁盘。

该脚本由 cron.weekly 在一天中的随机时间启动。

如果我在脚本运行时关闭系统,我就会遇到麻烦,因为旧备份会被删除,而新备份不会被创建。

有没有办法设置系统等待脚本完成?

我正在使用带有 systemd 的 Ubuntu 16.04。

答案1

对于 Ubuntu 16.04+ 使用 systemd(默认)。

systemd-inhibit --why="Wait for this script to finish" bash script.sh

===

测试:

$ systemctl poweroff
Operation inhibited by "bash script.sh" (PID 23912 "systemd-inhibit", user rinzwind),
reason is "Wait for this script to finish".
Please retry operation after closing inhibitors and logging out other users.

===

有 7 把锁

  • sleep禁止(非特权)用户请求的系统挂起和休眠
  • shutdown禁止(非特权)用户请求的高级系统关机和重启
  • idle禁止系统进入空闲模式,可能导致系统自动挂起或关闭(具体取决于配置)。
  • handle-power-key抑制系统电源硬件键的低级(即 logind-internal)处理,而允许(可能是非特权的)外部代码来处理该事件。
  • handle-suspend-key抑制系统硬件暂停键的低级处理。
  • handle-hibernate-key抑制系统硬件休眠键的低级处理。
  • handle-lid-switch抑制 systemd 硬件盖子开关的低级处理。

您可能还想预防suspendidlehibernate


使用“包管理器”的示例

fd = Inhibit("shutdown:idle", "Package Manager", "Upgrade in progress...", "block");
/* ...
      do your work
                 ... */
close(fd);

与此类似,您可以编写自己的版本并在此脚本末尾添加“关机”(或添加一种方法来确定下一步操作是否需要关机)。

答案2

时光倒流我使用几种不同的 DBus 方法在所有主要的 DE 上工作。唯一的缺点是这不适用于,root因为root没有dbus.SessionBus

#!/usr/bin/env python3
import sys
import dbus
from time import sleep

INHIBIT_LOGGING_OUT = 1
INHIBIT_USER_SWITCHING = 2
INHIBIT_SUSPENDING = 4
INHIBIT_IDLE = 8

INHIBIT_DBUS = (
               {'service':      'org.gnome.SessionManager',
                'objectPath':   '/org/gnome/SessionManager',
                'methodSet':    'Inhibit',
                'methodUnSet':  'Uninhibit',
                'interface':    'org.gnome.SessionManager',
                'arguments':    (0, 1, 2, 3)
               },
               {'service':      'org.mate.SessionManager',
                'objectPath':   '/org/mate/SessionManager',
                'methodSet':    'Inhibit',
                'methodUnSet':  'Uninhibit',
                'interface':    'org.mate.SessionManager',
                'arguments':    (0, 1, 2, 3)
               },
               {'service':      'org.freedesktop.PowerManagement',
                'objectPath':   '/org/freedesktop/PowerManagement/Inhibit',
                'methodSet':    'Inhibit',
                'methodUnSet':  'UnInhibit',
                'interface':    'org.freedesktop.PowerManagement.Inhibit',
                'arguments':    (0, 2)
               })

def inhibitSuspend(app_id = sys.argv[0],
                    toplevel_xid = None,
                    reason = 'take snapshot',
                    flags = INHIBIT_SUSPENDING | INHIBIT_IDLE):
    """
    Prevent machine to go to suspend or hibernate.
    Returns the inhibit cookie which is used to end the inhibitor.
    """
    if not app_id:
        app_id = 'backintime'
    if not toplevel_xid:
        toplevel_xid = 0

    for dbus_props in INHIBIT_DBUS:
        try:
            bus = dbus.SessionBus()
            interface = bus.get_object(dbus_props['service'], dbus_props['objectPath'])
            proxy = interface.get_dbus_method(dbus_props['methodSet'], dbus_props['interface'])
            cookie = proxy(*[(app_id, dbus.UInt32(toplevel_xid), reason, dbus.UInt32(flags))[i] for i in dbus_props['arguments']])
            print('Inhibit Suspend started. Reason: %s' % reason)
            return (cookie, bus, dbus_props)
        except dbus.exceptions.DBusException:
            pass
    print('Inhibit Suspend failed.')

def unInhibitSuspend(cookie, bus, dbus_props):
    """
    Release inhibit.
    """
    assert isinstance(cookie, int), 'cookie is not int type: %s' % cookie
    assert isinstance(bus, dbus.bus.BusConnection), 'bus is not dbus.bus.BusConnection type: %s' % bus
    assert isinstance(dbus_props, dict), 'dbus_props is not dict type: %s' % dbus_props
    try:
        interface = bus.get_object(dbus_props['service'], dbus_props['objectPath'])
        proxy = interface.get_dbus_method(dbus_props['methodUnSet'], dbus_props['interface'])
        proxy(cookie)
        print('Release inhibit Suspend')
        return None
    except dbus.exceptions.DBusException:
        print('Release inhibit Suspend failed.')
        return (cookie, bus, dbus_props)

if __name__ == '__main__':
    cookie, bus, dbus_props = inhibitSuspend()
    print('do something here')
    sleep(10)
    unInhibitSuspend(cookie, bus, dbus_props)

相关内容