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 $$))

hymcode
  • 83

1 Answers1

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

MARco
  • 56
  • 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 03 '20 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 03 '20 at 16:51
  • Thank you very much @MARco this line works wonders and looks do to what I need. – hymcode Dec 04 '20 at 09:59