0

Is it possible to close the parent terminal window once an application has been loaded?

I have a program I need to run using root privileges to work properly and currently I have made a script file which checks if the user is root if not then they are asked to confirm the root password before the application is loaded.

Original

Here is the contents of my script file:

#!/bin/bash
if [ "$EUID" -ne 0 ]
  then 
    echo "You need root privileges to run this utility"
    echo "Do you want to continue? (y/n):"
    read userInput
    
    if [ $userInput == "y" ] || [ $userInput == "Y" ]
      then 
        sudo ./myGuiProgram
      exit
    elif [ $userInput == "n" ] || [ $userInput == "N" ]
      then
        echo "Exiting now..." 
      exit 
    fi
  exit
elif [ "$EUID" -eq 0 ]
  then
    ./myGuiProgram 
  exit
fi

Is there anything I can add to this that will close the terminal window and not myGuiProgram ?

On my Centos 7 machine I have a desktop config file which executes the script file which in turns runs myGuiProgram

2nd Attempt

I've modified my script since, but still no luck. This method allows me to exit the terminal window manually without closing my program

#!/bin/bash
if [ "$EUID" -ne 0 ]
  then 
    echo "You need root privileges to run this utility"
    echo "Do you want to continue? (y/n):"
    read userInput
    
    if [ $userInput == "y" ] || [ $userInput == "Y" ]
      then 
        sudo nohup ./myGuiProgram > /dev/null & disown && kill $PPID
    elif [ $userInput == "n" ] || [ $userInput == "N" ]
      then
        echo "Exiting now..."  
    fi
elif [ "$EUID" -eq 0 ]
  then
    nohup ./myGuiProgram > /dev/null & disown && kill $PPID
fi

MARco Working Solution

New changes made based on @MARco response. This method works well.

#!/bin/bash
if [ "$EUID" -ne 0 ]
  then 
    echo "You need root privileges to run this utility"
    echo "Do you want to continue? (y/n):"
    read userInput
    
    if [ $userInput == "y" ] || [ $userInput == "Y" ]
      then 
        sudo -b nohup ./myGuiProgram 2>&1> /dev/null
    elif [ $userInput == "n" ] || [ $userInput == "N" ]
      then
        echo "Exiting now..."
        sleep 1
        exit 0
    fi
elif [ "$EUID" -eq 0 ]
  then
    nohup ./myGuiProgram > /dev/null 2>&1> /dev/null & 
fi
kill $(ps -ho ppid -p $(ps -ho ppid -p $$))
9
  • 1
  • Wouldn't it make more sense for myGuiProgram to error out if it's not being started by root?
    – Panki
    Dec 3, 2020 at 11:00
  • Does this answer your question? How can I run a command which will survive terminal close?
    – thanasisp
    Dec 3, 2020 at 11:06
  • @Quasímodo thanks for the suggestion. I'm attempting to do what shown in the feed but I keep getting a command not found error in my terminal window
    – hymcode
    Dec 3, 2020 at 11:08
  • @Panki true I could cause an error if it's not been started in root, but I want to at least inform the user and give them the ability to try confirming root privileges for me. Think of it like Windows UAC control maybe. My script file will be loaded from a desktop config file which then loads the program
    – hymcode
    Dec 3, 2020 at 11:27

1 Answer 1

0

In general you can launch a gui program and close immediately the xterm that launched it with:

exec program&exit

or better

exec program2>&1>/dev/null &exit

You have a script that acquire input still in terminal, so the terminal for you doesn't have to exit soon, so here is the solution:

  1. replace ./myGuiProgram with nohup ./myGuiProgram 2>&1>/dev/null &
  2. replace sudo ./myGuiProgram with nohup sudo ./myGuiProgram 2>&1>/dev/null &
  3. remove all the four exit in your script (the way you used it is only redundant)
  4. append sleep 1 in the end of your script

launch the script in the terminal with

exec script

Note: the only way to launch the script without exec and have the external exec functionality within the script is not very nice: end suddenly xterm (parent of xterm's shell process accepting script command which is parent pf bash the script interpreter) within the script with

kill $(ps -ho ppid -p $(ps -ho ppid -p $$))

as last line of the script

The need to use 1. and 2. also gave me an idea: nohup should have an argument to ignore program's output because otherwise it only add a extensive pipe to what is wasted

3
  • Thanks for responding. I tried your method but the sleep 1 is too short of a window to enter the root user password. I've updated my question incorporating your method and I can confirm that it worked great. It would be nice to not need the sleep command.
    – hymcode
    Dec 3, 2020 at 16:26
  • 1
    I didn't know your sudo configuration, I assumed NOPASS, the sleep 1 was not in the purpose to give time to enter a password... the sudo ... & is not for you then, you have to invert sudo -b nohup ./myGuiProgram 2>&1>/dev/null
    – MARco
    Dec 3, 2020 at 16:51
  • Thank you very much @MARco this line works wonders and looks do to what I need.
    – hymcode
    Dec 4, 2020 at 9:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .