M BUZZ CRAZE NEWS
// general

Running shell script to open two terminals and run commands in them

By John Parsons

I'm setting up a repo and I want to make a shell script that will run two commands that run a backend and build a frontend. What I have so far is something like this:

#!/bin/sh
sh -e "sh -c 'cd frontend && ng build'"
sh -e "sh -c 'cd backend && symfony server:start'"

When I try running that though, I get back sh: 0: Can't open sh -c ... for both lines. What is the solution to this? It should be clear that both these commands will not terminate until you do by hand. It is not necessary to open a new terminal for both commands, one can be executed in the current terminal.

2

2 Answers

It could be simplified:

$ ./twocmds.sh
frontend
backend
$ cat ./twocmds.sh
#!/bin/sh
/bin/sh -ec 'cd frontend && echo frontend'
/bin/sh -ec 'cd backend && echo backend'

Also, try to specify full paths in scripts. frontend and backend folders I've created in current directory, but it is better to specify full path to them.

It's an example output, because of I do not have your services. You should change echo frontend and echo backend by your commands, that means by /usr/bin/ng build and /usr/bin/symfony server:start. But before it do whereis ng && whereis symfony to find out full path to your commands.

Your script could look like:

#!/bin/sh /bin/sh -ec 'cd frontend && /usr/bin/ng build &' /bin/sh -ec 'cd backend && /usr/bin/symfony server:start'

Firstly try to use without &. But if the second command waits for the first command execution, then use & to send the first command into background. Also, if you need waiting before some commands execution, use sllep X, where X number of seconds.

3
#!/bin/bash
a=/absolute/path/frontend
b=/absolute/path/backend
if [[ ! -d $a ]] || [[ ! -d $b ]]; then echo "panic! path not found" exit 1
fi
cd $a
ng build
cd $b
symfony server:start
2

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