Search code examples
bashdebian

Bash Script Autoclose Terminal after nohup


I am trying to make a bash script to improve my skills. The goal is to have the bash script to open another terminal launch the browser and then close it by itself.

I can do everything but make it close on its own.

#! /bin/bash

echo "type apps you want to launch"
read -r String

if [[ $String =~ "Brave" ]];
        then gnome-terminal --window-with-profile=BashProfile -- nohup bash -c "sleep 1s; brave-browser &" && exit
fi

Anyone have a better solution to what I am trying to do?

I have tried && exit after my command to close it both inside and outside the quotes to see if that closes it. It seems that no command runs after the launch though, which makes sense.


Solution

  • The exit in your script only exits the script which contains the commands and not the terminal from which it is launched.

    I am not sure what you want to kill (the initial terminal or the launched terminal) but try using kill -9 $$ to kill the parent process. in place of exit

    (UNTESTED CODE)

    #! /bin/bash
    
    echo "type apps you want to launch"
    read -r String
    
    if [[ $String =~ "Brave" ]];
            then gnome-terminal --window-with-profile=BashProfile -- nohup bash -c "sleep 1s; brave-browser &" && kill -9 $$
    fi