Ubuntu – How to make script wait for Nautilus to exit

nautilusscripts

I have a script that will extract files for examining to a temporary folder then call nautilus.

I'm trying to make the script wait for Nautilus to exit, then remove the temporary folder. However, the folder is removed instantly after Nautilus is called.

I understand that Nautilus is a sub-window of Gnome which is controlling the Desktop. However, I'm trying to figure out a way to call a new instance of Nautilus or workaround.

The Script:

#/bin/bash

tempdir=$(mktemp -d)

unzip ~/document.odt -d $tempdir
nautilus $tempdir

# waitfor nautilus to exit
# workaround 
zenity --info --title="Find Icons" --text="Click OK to exit" 2>/dev/null

rm -r $tempdir

Currently I'm using Zenity as a workaround to make the script wait by giving the user a prompt. I'm trying to remove this extra step and have the script recognize the Nautilus window had closed, then automatically finish.

Best Answer

Keeping track of the PID and killing it from the shellscript works for me in a live system of Ubuntu 16.04.1 LTS with the Xenial kernel and also in an up to date installed system with Ubuntu 16.04 LTS (and the Xenial kernel).

#!/bin/bash

echo "Starting Nautilus"

nautilus --no-desktop ~/Downloads & pid=$!

echo "pid=$pid"
ps -A|grep "$pid"

read -p "Press Enter when you are ready to kill the Nautilus window"

kill "$pid"
sync
echo "Checking that the process is gone"
ps -A|grep "$pid"

Edit:

It works, when killed like this, but when closed with the x control button, the process is still alive, which is a problem with Nautilus.

But if you use Thunar instead of Nautilus, the process will be killed, when the window is closed with the x button. So I suggest to switch file browser for this task,

#!/bin/bash

echo "Starting Thunar"

thunar ~/Downloads

echo "Checking that the process is gone"
ps -A|grep "thunar"