-2

Due to strange window focus and/or key-binding behavior on my system, sometimes I accidentally close a Nautilus/Files window when my intention is to close the uppermost application window (e.g., a LibreOffice document). I end up having to manually relaunch Nautilus/Files. I have to do this way more often than I would like.

I can see no use-case where I would desire for Nautilus/Files to not immediately relaunch after quitting in my daily use.

How can I automatically check to see whether Nautilus/Files is running, and, if it is not, to relaunch it?

Bonus points for efficiency of resource use in a solution.

Answers predicated on me not wanting this behavior will be down-voted.

Lexible
  • 1,355
  • 4
  • 16
  • 31
  • 3
    -1 "Answers predicated on me not wanting this behavior will be down-voted" - quite an aggresive statement. Remember answers on any questions are not like a personal helpdesk-answer, and a threat to downvote any (possibly useful) information is misplaced. Information should be useful to a broader audience than just you. – Jacob Vlijm Oct 10 '19 at 16:21
  • @JacobVlijm Answers of the form "I am going to answer a separate question than the one you asked" are aggressive, unfriendly, and explicitly the reason several folks I know actively refuse to participate in SE in any manner. Frame-challenges can happen in comments. – Lexible Oct 10 '19 at 16:48
  • @JacobVlijm "Remember answers on any questions are not like a personal helpdesk-answer" Remember that a question's author is the sole arbiter of the question's accepted answer. – Lexible Oct 10 '19 at 16:50
  • @DKBose Not at all. Thank you. – Lexible Oct 10 '19 at 16:51
  • 1
    It's the general rule: if someone asks: "I want to know how to run firefox as root", anyone with a bit of responsibility should say "you don't want that". When and if that is the case, you don't know, nor should you block it. Answers should be given freely to the best of knowledge. – Jacob Vlijm Oct 10 '19 at 16:51
  • @DKBose Thank you am attending to incorporate those suggested edits now. That said: these two questions are radically different: the older question "why is X happening?" is not the same thing as "I would like to achieve Y behavior." (As it so happens, I want Y behavior even without X happening.) – Lexible Oct 10 '19 at 16:59
  • @JacobVlijm I am conducting experiment in system security and behavioral metrics in the propagation of various exploits in a mini-network of computer nodes isolated from the internet contained in a virtual network environment. Being able to run Firefox as root will be useful to some of my ends, which are far too complex to state here. So tell me more about this "general rule." Is it written in the CoC? Is it in the Help Center section of writing good answers? – Lexible Oct 10 '19 at 17:02
  • 2
    Just write a start up script to call nautilus in a loop. When you exit it starts up again. I'm on my phone so can't post answer now. – WinEunuuchs2Unix Oct 10 '19 at 19:49
  • @WinEunuuchs2Unix Thank you. Do you have an idea about how to construct the loop so that I minimize resources (i.e. don't have a cron job running every second)? – Lexible Oct 10 '19 at 23:44
  • 1
    @Lexible Yes I'm actually testing the program now. Nautilus does some strange things I've just discovered. – WinEunuuchs2Unix Oct 10 '19 at 23:49
  • 1
    @Lexible I've spent hours on the problem and had to post a question here:
    • https://unix.stackexchange.com/questions/546241/best-way-to-check-if-nautilus-file-manager-is-running
    – WinEunuuchs2Unix Oct 11 '19 at 02:32

1 Answers1

3

Update: October 16, 2019.

Thanks to gnome expert's answer on linked question below, a faster more stable method of relaunching Nautilus File Manager has been implemented.


This turned out to be a challenging problem because Nautilus is always running to control the desktop. If you kill the program nautilus all your desktop icons will disappear. When nautilus is running and your desktop icons are present and you type nautilus in the terminal then the Nautilus File Manager appears.

Copy this script to a file called ~/ttlus:

#!/bin/bash

# NAME: ttlus (Twenty Thousand Loops Under Startup)
# PATH: $HOME/askubuntu/
# DESC: Answer for: https://askubuntu.com/questions/1180043/how-can-i-automatically-relaunch-nautilus-if-i-quit-the-program
#       Call Nautilus, named after Twenty Thousand Leagues Under the Sea novel,
#       20,000 times in loop from Startup Applications
# DATE: October 10, 2019.  Modified October 16, 2019.

# NOTE: Things that don't work:
#       https://askubuntu.com/questions/965052/how-to-make-script-wait-for-nautilus-to-exit
#       https://ubuntuforums.org/showthread.php?t=1604843

# UPDT: Rpelace loop with occassional focus grabbing and unpredictable delays:
#    while ps -L -p "$PID" -o pid,nice,lwp,comm | grep pool > /dev/null ; do
#       with:
#    while gdbus introspect --session --dest org.gnome.Nautilus \ ... 3 lines

LoopLimit=20000
Program="nautilus"
#Program="/usr/bin/nautilus --gapplication-service"
PID=$(pgrep nautilus)
Sec=3

if [[ $PID == "" ]] ; then
    notify-send "ERROR in $0: Cannot find nautilus PID"
    exit
fi

for (( l=0; l<LoopLimit; l++ )) ; do
    # Is nautilus file manager running? 1 Window only is desktop icons
    while gdbus introspect --session --dest org.gnome.Nautilus \
        --object-path /org/gnome/Nautilus --recurse | \
        grep -q '^ *node /org/gnome/Nautilus/window/' | \
        grep -v '/window/1' ; do
            sleep "$Sec"
    done
    "$Program" "$HOME" 2> /dev/null
    sleep $(( Sec / 2 ))
done

notify-send "ERROR in $0: $Program exceeded $LoopLimit loop limit"

Make it executable with chmod a+x ~/ttlus

First test is by typing ~/ttlus & in the terminal. After you are satisfied add it in Startup Applications.

If program runs amok use:

$ pgrep ttlus
7970

$ kill 7970 [1]+ Terminated ttlus


Notes:

  • Due to the sleep "$Sec" command after exiting Nautilus with Alt+F4 or clicking X on window a three second delay (to reduce resource usage) will occur before Nautilus reappears on the desktop.