I'm trying to use
#!/bin/bash
sudo gdbus call -y -d org.freedesktop.login1 \
-o /org/freedesktop/login1 \
-m org.freedesktop.login1.Manager.Inhibit \
sleep me because block
sleep 10
to acquire an inhibitor lock in a bash script. The introspection for this method looks like this:
Inhibit(in s what,
in s who,
in s why,
in s mode,
out h pipe_fd);
After my call to gdbus
is done, no inhibitor lock is shown as acquired when listing with systemd-inhibit --list
. I assume this is because the lock is acquired and then immediately released when exiting from gdbus
.
How can I duplicate the returned file descriptor, pipe_fd
, from the method call so it doesn't release the lock immediately after exiting from the call?
How can I duplicate the returned file descriptor,
pipe_fd
, from the method call so it doesn't release the lock immediately after exiting from the call?
You can’t do that with the command line gdbus
tool, as it exits immediately after making the D-Bus call, and hence the FD is returned to the kernel.
You will need to write a script in some other language (perhaps Python) which makes the D-Bus call and then does something appropriate with the returned FD before it exits. In that case, you may find it easier to use systemd’s own Python bindings rather than making a D-Bus call directly.
Alternatively, you could wrap your bash script in a call to systemd-inhibit
, which is a command line utility to inhibit shutdown while another script is running.