I want to run a script from .bash_profile, but only for the first Terminal window/tab which I open. How can I prevent script execution when opening additional windows/tabs?
-
A separate shell binary runs in each of the windows. Wich problem are you trying to solve here?– nohillside ♦Jul 6, 2021 at 14:27
-
@nohillside I want to launch a script from .bash_profile but only from the first terminal shell– PuzzledJul 6, 2021 at 15:07
-
Does the script start another application in the background then, or similar?– nohillside ♦Jul 6, 2021 at 15:11
-
@nohillside yes it does– PuzzledJul 6, 2021 at 15:13
1 Answer
If this script starts an application/binary, the usual way to prevent several executions is to first check whether that binary is already running (man ps
or man psgrep
might help) and only launch it if it isn't already started.
As an alternative you could look into LaunchAgents to start the binary directly when you log in (and even have it restarted automatically in case it crashes).
-
Right @nohillside I can also use pgrep like here unix.stackexchange.com/a/246760– PuzzledJul 6, 2021 at 15:30
-