1

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.

  • Is a shell script required, or are you open to other solutions? There are many advantages to starting and monitoring services (including daemons) using systemd instead of a shell script. – user535733 Mar 21 at 21:59
  • @user535733 I'd like to have it a shell script, since I want to put it in a repo for people who aren't oriented in the application yet. They'd just be able to run this script and the work would be done for them. – Luctia Mar 21 at 22:00
1

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.

| improve this answer | |
  • I don't see what this accomplishes, as I specified the commands that I'm trying to run do not terminate, and from the script you provided I only get back frontend\nbackend. – Luctia Mar 22 at 12:51
  • Try updated answer – Gryu Mar 22 at 13:05
  • 1
    Awesome, works like a charm! Thanks. – Luctia Mar 22 at 13:23
1
#!/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

| improve this answer | |
  • ng build doesn't terminate on its own (I've added --watch, but didn't feel like I'd have to add that in the question since I specified the commands wouldn't terminate on their own), and therefore this script doesn't get further than that command. – Luctia Mar 22 at 12:36
  • you are thinking of something like this: x-terminal-emulator -e "/bin/bash -c 'ls; exec bash'" & (exec bash isnt necessary if you dont intend to keep the terminal open after command has executed) – bac0n Mar 22 at 13:37

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.