Run multiple gui programs from terminal without disowning them

I made a very simple (but useful for me) bash script. All it does is just opening some applications that I was opening manually. Here is the code:

#!/bin/bash
netbeans &
mysql-workbench &
opera &
chromium-browser &

Now when I close the terminal the applications opened by the script keep running. Which is normal, that is what the "&" is there for.

My question is: Is there a way to still run those applications automatically but also close them automatically? If they were still attached to the terminal they would close, but when I remove the ampersand it only runs the first application.

Thanks

5

2 Answers

These two functions can be used from your .bashrc file to launch and close them, simply call them with lapp and kapp:

lapp(){ netbeans & mysql-workbench & opera & chromium-browser &
}
kapp() { pkill 'netbeans|mysql-workbench|opera|chromium-browser'
}
#Or
kapp() { killall 'netbeans|mysql-workbench|opera|chromium-browser'
}

Information:

  • remember to run source .bashrc after adding these functions

(source: man pkill)

12

You'll have to source the script so that these commands are executed in your current shell, instead of in the forked-off shell started for the script:

. ./foo.sh

Then, these background processes will be part of your shell's job control.

It might be easier to use a function. In your bashrc, for example, add:

foo () { netbeans & mysql-workbench & opera & chromium-browser &
}

Then, when you run foo from bash, it will run the commands in your current shell.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like