7

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

  • killall <app-name> is what you mean to auto kill them ? auto kill them when ? at a specific time ? when a arguement is true like cpu usage bigger then a X% ? specify when you want to auto-kill them and make a loop with "while" or "if" command . don't forget to add a sleep timer for the cpu to rest a bit and don't loop like crazy 24/7. – Pavlos Theodorou Feb 21 '17 at 4:04
  • @PavlosTheodorou I think they want closing the terminal to also close the multiple applications started therein. – Chai T. Rex Feb 21 '17 at 4:14
  • @Chai T.Rex , closing the terminal kills them all. i don't think he means that. Maybe he means to seperate the app using nohup, for example: $nohup firefox, either use disown for example: $firefox &; disown. And then close the apps seperately. Or he wants to use the command killall <app name> We wait from Karim to explain what he means to auto close the apps. – Pavlos Theodorou Feb 21 '17 at 4:24
  • 1
    @PavlosTheodorou with & after a GUI program, the GUI program will start, but closing the terminal won't close the GUI program. – Chai T. Rex Feb 21 '17 at 4:48
  • "Now when I close the terminal the applications opened by the script keep running. Which is normal, that is what the "&" is there for." - No, that is not what & is for. Using & has no bearing on whether or not the applications quit when you close the terminal. Closing the terminal causes the application to get SIGHUP. If the application doesn't terminate it is because it purposefully ignores that signal. Not because you used &. – marcelm Feb 21 '17 at 12:22
5

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)

  • sorry for asking this, but it's my first time i read about adding commands to .bashrc. This way the apps open without owning the terminal right ? so the disown or nohup are not necessary ? we run lapp , close terminal if we want and then run kapp and close them ? – Pavlos Theodorou Feb 21 '17 at 4:48
  • nvm i never tried opening apps from terminal so.... nub question from me. already answered in comments. opening with & it doesn't own the terminal. – Pavlos Theodorou Feb 21 '17 at 4:52
  • I don't like this because any other instances of these applications would be killed as well. Admittedly some of these might be single-instance applications but you wouldn't want an instance you had open prior to opening your terminal to be killed. – Muzer Feb 21 '17 at 10:41
  • @Muzer, the OP said he wants to close what he had opened so what other instance are you talking about? – George Udosen Feb 21 '17 at 11:14
  • @Gergoe let's say OP already had an instance of netbeans running. Then when they opened their terminal a second instance would be opened. When they later closed their terminal both copies of netbeans would be closed! – Muzer Feb 21 '17 at 11:28
5

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.

  • oh , nice one , i didn't know i can do that. don't delete those lines when you edit the answer later (if you edit) to fit the auto close too. – Pavlos Theodorou Feb 21 '17 at 4:30

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.