This is imperfect but close enough:
#!/bin/bash
set -e
gksudo --description "Launch a root Nautilus" -- \
sh -c "echo 'Password accepted'"
gksudo -- sh -c "xdg-open / &" &
PID_GKSUDO=`pgrep -n gksudo`
sleep 10
PID_XDGOPEN=`pgrep -u root -n xdg-open`
sudo kill $PID_XDGOPEN $PID_GKSUDO
exit 0
Some explanations:
set -e
This ensures the script exits if the user cancels out of the password prompt or if anything else fails.
gksudo --description "Launch a root Nautilus" -- \
sh -c "echo 'Password accepted'"
This line won't return until the user has entered the password or cancelled out of the prompt. The echo
command is inoccuous and just makes for nice feedback when the script is run from a command line.
gksudo -- sh -c "xdg-open / &" &
The gksudo ... &
returns right away (because it detaches from the script process), regardless of an eventual password prompt. This is why we prompted for the password on the previous line, because we otherwise can't tell how long we'll need to sleep later.
Note also that the xdg-open / &
will also detach from gksudo
, so we end up with two orphans: gksudo
(childless) and xdg-open
(parent of the desired root nautilus
process).
PID_GKSUDO=`pgrep -n gksudo`
sleep 10
Here we recover the xdg-open
process ID (immediately available) and then wait for nautilus-gdu
to initialize and the window to appear. If we did not wait, the next PID-fetching command would fail because the process wouldn't exist yet (pgrep
would return an error 1 and the script would not proceed to the kill
line).
PID_XDGOPEN=`pgrep -u root -n xdg-open`
sudo kill $PID_XDGOPEN $PID_GKSUDO
exit 0
Finally, we get the xdg-open
PID and kill
the useless processes before exiting the script.
disown
, which is a bash-builtin, but I'm not sure you can detach the process fromgksudo
.Thus:gksudo --description "..." -- bash -c "xdg-open / &; disown"