0

I'm trying to open 4 Terminal Windows at startup and I'm using the script below to do so. However, it's only opening 1 terminal Window instead of 4.

/bin/sh -c gnome-terminal; /bin/sh -c gnome-terminal; /bin/sh -c gnome-terminal; /bin/sh -c gnome-terminal

I'm running the script from within "Startup Applications". If I run the script manually from a Terminal Window it does open 4 Terminal Windows.

So here is my question. Why is the script only opening 1 windows from within the "Startup Applications" when it should be opening 4?

  • Vinnie, could you give some feedback on the answer(s)? Would be useful to understand what works for you. – Jacob Vlijm May 18 '15 at 16:01
2

You do not need /bin/sh -c if you are running it from script. Also, use & instead of ;, like so

#!/bin/bash



gnome-terminal -t WINDOW-ONE &
gnome-terminal -t WINDOW-TWO &
gnome-terminal -t WINDOW-THREE &
gnome-terminal -t WINDOW-FOUR &

I believe the reason why it behaves like it does in your original script is because the shell is waiting for each line of script to complete first, so it is waiting for the first window to get done. With &, you disconnect gnome-terminal from script, and the script can go on to the next line. Hope that makes sense

  • I edited your answer to include your comment. All information should be in the answer itself, comments can be deleted without warning and are easy to miss. They should, ideally, only be used to request clarification. – terdon May 15 '15 at 17:37
  • @terdon thank you. I wasn't quite sure about the reason why Startup Applications behaves that way, so I posted it as comment. I'll delete the comment in a second, as it is already in the answer. Again, thank you for the edit – Sergiy Kolodyazhnyy May 15 '15 at 17:43
  • The & does not "disconnect" the gnome-terminal starting command from the running script in a way that if you abort the script (or if it runs in a terminal window close the terminal) the freshly opened gnome-terminal-windows will stay open. They are still child processes of the invoking script. The & just lets it run in background. If the script was run from a terminal, you would also see all outputs (stdout and stderr) of all child processes printed to the parent terminal with just an &. I don't know how Startup Apps handles this, so it might work, but you should mention that. – Byte Commander May 15 '15 at 18:37
  • @ByteCommander see, that's what I am also confused about. I know how & behaves in terminal , but I lack knowledge of terminology. – Sergiy Kolodyazhnyy May 15 '15 at 18:48
  • Thanks so much Serg for your answer, very much appreciated. When I run your script above manually it works fine Howerver I do not understand why when I call it from a script from the .bashrc file like: /home/vgerman/windowsopener instead of just opening the 4 windows it just keeps on opening windows one after another till I have to reboot the machine. Why it runs manually and when I try calling it from within /home/vgerman/.bashrc it does not work? – Vinnie German May 19 '15 at 21:07
0

No need to use a separate script to run four terminal windows on Startup

You do not need a separate script to open four gnome-terminal windows, you can simply combine them into one command, to be added to Startup Applications.

How to use complex commands in a .desktop file

When you add a command to Startup Applications, it actually creates a .desktop file in ~/.config/autostart, that runs your command on startup (actually log in). The syntax for complex commands to be used in a .desktop file is as follows:

/bin/bash -c "<complicated_command>"

However: You only need to use the /bin/bash -c a single time, so the complete command should look like:

/bin/bash -c "gnome-terminal&gnome-terminal&gnome-terminal&gnome-terminal"

Doing so, you do not need a separate script to do the job.

Chaining commands

Like already mentioned, you should chain the commands with a &, not with ;.

From man bash:

If a command is terminated by the control operator &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0. Commands separated by a ; are executed sequentially; the shell waits for each command to terminate in turn. The return status is the exit status of the last command executed.

  • Jacob, Thanks a lot for your explanation, very well detailed. I ended up using the Dash Home Startup applications and created the scripts to start those 4 windows automatically and it's working well. Thanks again. :) – Vinnie German May 19 '15 at 21:25
  • @VinnieGerman ???? Adding it to Startup Applications was already assumed, since that was the question to start with. However, as mentioned, you do not need to add a script to Startup Applications, just the single command in the answer: /bin/bash -c "gnome-terminal&gnome-terminal&gnome-terminal&gnome-terminal" – Jacob Vlijm May 20 '15 at 5:18

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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