4

I want to write a script to activate a virtual environment and run my server for Django project in a new terminal. My startProject.sh is:

#!/bin/bash
source virtualenv/bin/activate
python manage.py runserver

And, I can run this script on my current terminal by:

source startProject.sh

I want to do this in a new terminal opened by a script.

#!/bin/bash
gnome-terminal --working-directory=/home/myname/project -x 'source startProject.sh'

I tried this too.

#!/bin/bash
gnome-terminal --working-directory=/home/myname/project -x '#!/bin/bash\n source startProject.sh'

Both do not work. Why? I read other questions but I still did not get it. I am a beginner, so please assume no experience.

pledab
  • 60
  • 9
Heuyie
  • 169
  • 1
  • 3
  • 12
  • Maybe try `gnome-terminal --working-directory=/home/myname/project -x './startProject.sh'` – wjandrea Oct 22 '17 at 19:49
  • 1
    @wjandrea, according to [this](https://askubuntu.com/questions/965475/cannot-activate-virtual-environment-with-a-shell-script) previous question of Heuyie it should be `...'source startProject.sh'`. – pa4080 Oct 22 '17 at 21:14
  • Does this answer your question? [how can I open a extra console and run a program in it with one command?](https://askubuntu.com/questions/974756/how-can-i-open-a-extra-console-and-run-a-program-in-it-with-one-command) – pa4080 Mar 01 '22 at 12:19

1 Answers1

5

According to my experience the command should be:

gnome-terminal --working-directory='/home/<user>/project' -x bash -c "source startProject.sh; exec bash"

Notes:

  • The path of --working-directory='/home/<user>/project' is enclosed with single quote marks in case it contains some special characters as spaces, etc.

  • The option -x means: execute - the remainder of the command line inside the terminal.

  • And our command is bash -c "<commands>". That means we execute a new bash shell, which should run some -c "<commands>".

  • We have two separated (by semicolon ; == new line) <commands>.

  • The first command source startProject.sh will source the script file.

  • The second command exec bash has a meaning - remain open the current gnome-terminal window. There are another possible approaches to do that. In the current case the command exec will replace the current process image with a new process image - in other words it will 'kill' the current process and will execute a new (bash) under the current PID.

Further reading:

pa4080
  • 29,351
  • 10
  • 85
  • 161
  • It worked! Thank you for explaining one by one! :) – Heuyie Oct 23 '17 at 19:07
  • Happy to help, @Heuyie! :) – pa4080 Oct 23 '17 at 19:11
  • One more question. Is gnome-terminal bash or dash command? Why do not I need to specify a shell to run this command? – Heuyie Oct 23 '17 at 23:40
  • @Heuyie: In the above solutions we specify a shell, because: **(1)** `source` is [Shell Builtin Command](https://www.gnu.org/software/bash/manual/html_node/Shell-Builtin-Commands.html) (find 'source' in the section [Bash Builtin Commands](https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html#Bash-Builtins)), and in other case `gnome-terminal` will do something wrong; **(2)** We want to execute a second command to remain the window open. – pa4080 Oct 24 '17 at 17:46
  • `gnome-terminal` is a program, executable file that is [located](https://askubuntu.com/a/968698/566421) within a directory listed in your `$PATH` [envvar](https://en.wikipedia.org/wiki/Environment_variable#Unix_2), so it is accesible as shell command. See also: [How do I modify my $PATH?](https://askubuntu.com/q/3744/566421) – pa4080 Oct 24 '17 at 17:47
  • > it is accesible as shell command I did not know this! I was looking at list of bash/dash commands and could not find it. Thank you again! :) – Heuyie Oct 24 '17 at 18:15
  • @Heuyie, I remembered for this collection: [“Envoriment variable PATH” on Ubuntu](https://askubuntu.com/questions/962945/envoriment-variable-path-on-ubuntu). – pa4080 Oct 24 '17 at 18:58