Bash script to open multiple terminal tabs and run some scripts - bash

Since, my product follow a micro-service architecture, I want to run multiple apps same time.
I want to open new terminal tabs and run some commands in that. Can you help me to write a bash script to perform these action. Its a repeated task I need to do daily/restart my system. I would like get it automated.
eg:
Tab1: 'cd Documents/my_app1; rails s -p 4000'
Tab2: 'cd Documents/my_app2; rails s -p 5000'
Tab3: 'cd Desktop/angular_app; yarn start --port 3000'
etc..
So, I need a script to run this with one command.

What's with the tabs?
gnome-terminal -- /bin/bash -c 'cd Documents/my_app1; rails s -p 4000'
gnome-terminal -- /bin/bash -c 'cd Documents/my_app2; rails s -p 5000'
gnome-terminal -- /bin/bash -c 'cd Desktop/angular_app; yarn start --port 3000'

Related

How to run ENTRYPOINT as root and switch to non-root to run CMD using gosu?

To connect to my container from Azure WebApp admin I need to start ssh server at startup. Then I need to run web server once the db is up.
In my Dockerfile I create a dedicated non-root user to run the web server.
RUN groupadd -g 1000 wagtail && \
useradd -u 1000 wagtail -m -d /home/wagtail -g wagtail
I copy startup-ssh.sh and startup-main.sh scripts into the container:
COPY startup-ssh.sh /app/
COPY startup-main.sh /app/
RUN chmod +x /app/startup-ssh.sh
RUN chmod +x /app/startup-main.sh
ENTRYPOINT ["/bin/bash", "-c", "/app/startup-ssh.sh"]
CMD ["/bin/bash", "-c", "/app/startup-main.sh"]
In the startup-ssh.sh I start the ssh server and then use gosu to switch user:
#!/bin/bash
# start ssh server
sed -i "s/SSH_PORT/$SSH_PORT/g" /etc/ssh/sshd_config
/usr/sbin/sshd
# restore /app directory rights
chown -R wagtail:wagtail /app
# switch to the non-root user
exec gosu wagtail "$#"
I expect the CMD's startup-main.sh script to be executed next but I get this in the Docker Desktop logs when the container is started.
Exited(1)
Usage: gosu user-spec command [args]
gosu nobody:root bash -c 'whoami && id'
gosu 1000:1 idie: gosu tianon bash
gosu version: 1.10 (go1.11.5 on linux/amd64; gc)
license: GPL-3 (full text at https://github.com/tianon/gosu)
I believe that Docker Desktop uses root when connecting to the container.
Maybe I'm missing something critical and/or this is something obvious. Please point me.
The code passed no arguments to the script. Imagine it like this:
bash -c '/app/startup-ssh.sh <NO ARGUMENTS HERE>' ignored ignored2 ignored3...
Test:
bash -c 'echo' 1
bash -c 'echo' 1 2
bash -c 'echo $0' 1 2 3
bash -c 'echo $1' 1 2 3
bash -c 'echo "$#"' 1 2 3
You want:
ENTRYPOINT ["/bin/bash", "-c", "/app/startup-ssh.sh \"$#\"", "--"]
Or, why the explicit shell if the file has a shebang and is executable, really just:
ENTRYPOINT ["/app/startup-ssh.sh"]

Terminal script involving 2 terminals

Is it possible to make a command that does something, Opens another terminal and then does something there?
Im trying to combine the start up of a server and its api.
"god": "next dev && cd server && strapi develop"
I want the 'cd' part to start in a new terminal. Ive tried open -a terminal but it doesnt seem to work.
you can try to use tmux for that
tmux new-session \; \
send-keys 'next dev' C-m \; \
split-window -h -p 50 \; \
send-keys 'cd server && strapi develop' C-m \;
If I understand the question correctly, you simply need to run two commands in parallel. This can be achieved easily in bash:
command1 & command2 # will run command1, send it to background,
# and run command2 (which will be kept in foreground)
In your case:
next dev & cd server && strapi develop
You can read more about this here: How do you run multiple programs in parallel from a bash script?

Source script on interactive shell inside Docker container

I want to open a interactive shell which sources a script to use the bitbake environment on a repository that I bind mount:
docker run --rm -it \
--mount type=bind,source=$(MY_PATH),destination=/mnt/bb_repoistory \
my_image /bin/bash -c "cd /mnt/bb_repoistory/oe-core && source build/conf/set_bb_env.sh"
The problem is that the -it argument does not seem to have any effect, since the shell exits right after executing cd /mnt/bb_repoistory/oe-core && source build/conf/set_bb_env.sh
I also tried this:
docker run --rm -it \
--mount type=bind,source=$(MY_PATH),destination=/mnt/bb_repoistory \
my_image /bin/bash -c "cd /mnt/bb_repoistory/oe-core && source build/conf/set_bb_env.sh && bash"
Which spawns an interactive shell, but none of the macros defined in set_bb_env.sh
Would there be a way to provide a tty with the script properly sourcered ?
The -it flag is conflicting with the command to run in that you're telling docker to create the pseudo-terminal (ptty), and then running a command in that terminal (bash -c ...). When that command finishes, then the run is done.
What some people have done to work around this is to only have export variables in their sourced environment, and the last command would be exec bash. But if you need aliases or other items that aren't inherited like that, then your options are a bit more limited.
Instead of running the source in a parent shell, you could run it in the target shell. If you modified your .bash_profile to include the following line:
[ -n "$DOCKER_LOAD_EXTRA" -a -r "$DOCKER_LOAD_EXTRA" ] && source "$DOCKER_LOAD_EXTRA”
and then had your command be:
... /bin/bash -c "cd /mnt/bb_repository/oe-core && DOCKER_LOAD_EXTRA=build/conf/set_bb_env.sh exec bash"
that may work. This tells your .bash_profile to load this file when the env variable is already set, but not otherwise. (There can also be the -e flag on the docker command line, but I think that sets it globally for the entire container, which is probably not what you want.)

Gnome Terminal: How to open up multiple tabs, execute commands and return to zsh/bash

I'm trying to automate my morning start up process. Often I'll start multiple running scripts that I will exit (using ctrl+c) and restart manually as needed.
So I'm looking to create a bash script that
Starts gnome-terminal
Opens some tabs and executes a number of
commands
Returns back to zsh upon exit or completion of the script so I can
manually enter more commands
Currently I have,
#!/bin/bash
gnome-terminal \
--tab -t "Server" -e "bash -ic \"cd ~/Dev/server; npm start; exec zsh\"" \
--tab -t "Framework" -e "bash -ic \"cd ~/Dev/framework; npm start; exec zsh\"" \
--tab -t "Client" -e "bash -ic \"cd ~/Dev/client; npm start; exec zsh\"" \
--tab -t "Admin" -e "bash -ic \"cd ~/Dev/admin;npm start; exec zsh\""
The problem with this solution is that tabs may or may not jump back into zsh. Sometimes 3 tabs will, sometimes one. Ideally I'd like all 4 to go back into zsh.
If anyone could help me around this, I'd be grateful.
You can take gnome-terminal out of the equation and it still won't work:
bash -ic "npm start; bash"
Press Control-C, and you won't get a shell. For bash, we can get around this problem using the --rcfile option:
bash --rcfile <(echo "npm start")
This way we don't read the real .bashrc, so since we probably want to read that, let's modify this a bit:
bash --rcfile <(echo ". ~/.bashrc; npm start")
For zsh on the other hand it doesn't work with the equivalent(?) --rcs option, but it does when started with the ZDOTDIR variable. So we create a .zshrc for each of ~/Dev/server, ~/Dev/framework, ~/Dev/client, ~/Dev/admin with the following content:
First:
. ~/.zshrc
cd ~/Dev/server
npm start
Second:
. ~/.zshrc
cd `~/Dev/framework`
npm start
etc.
Your gnome-terminal command will look like this:
gnome-terminal \
--tab "Server" -e "sh -c 'ZDOTDIR=/path/to/directory/containing/first/.zshrc'" \
--tab "Framework" -e "sh -c 'ZDOTDIR=/path/to/directory/containing/second/.zshrc'"
# etc

Run inline command with pipe in docker container [duplicate]

I'm trying to run MULTIPLE commands like this.
docker run image cd /path/to/somewhere && python a.py
But this gives me "No such file or directory" error because it is interpreted as...
"docker run image cd /path/to/somewhere" && "python a.py"
It seems that some ESCAPE characters like "" or () are needed.
So I also tried
docker run image "cd /path/to/somewhere && python a.py"
docker run image (cd /path/to/somewhere && python a.py)
but these didn't work.
I have searched for Docker Run Reference but have not find any hints about ESCAPE characters.
To run multiple commands in docker, use /bin/bash -c and semicolon ;
docker run image_name /bin/bash -c "cd /path/to/somewhere; python a.py"
In case we need command2 (python) will be executed if and only if command1 (cd) returned zero (no error) exit status, use && instead of ;
docker run image_name /bin/bash -c "cd /path/to/somewhere && python a.py"
You can do this a couple of ways:
Use the -w option to change the working directory:
-w, --workdir="" Working directory inside the container
https://docs.docker.com/engine/reference/commandline/run/#set-working-directory--w
Pass the entire argument to /bin/bash:
docker run image /bin/bash -c "cd /path/to/somewhere; python a.py"
You can also pipe commands inside Docker container, bash -c "<command1> | <command2>" for example:
docker run img /bin/bash -c "ls -1 | wc -l"
But, without invoking the shell in the remote the output will be redirected to the local terminal.
bash -c works well if the commands you are running are relatively simple. However, if you're trying to run a long series of commands full of control characters, it can get complex.
I successfully got around this by piping my commands into the process from the outside, i.e.
cat script.sh | docker run -i <image> /bin/bash
Just to make a proper answer from the #Eddy Hernandez's comment and which is very correct since Alpine comes with ash not bash.
The question now referes to Starting a shell in the Docker Alpine container which implies using sh or ash or /bin/sh or /bin/ash/.
Based on the OP's question:
docker run image sh -c "cd /path/to/somewhere && python a.py"
If you want to store the result in one file outside the container, in your local machine, you can do something like this.
RES_FILE=$(readlink -f /tmp/result.txt)
docker run --rm -v ${RES_FILE}:/result.txt img bash -c "grep root /etc/passwd > /result.txt"
The result of your commands will be available in /tmp/result.txt in your local machine.
For anyone else who came here looking to do the same with docker-compose you just need to prepend bash -c and enclose multiple commands in quotes, joined together with &&.
So in the OPs example docker-compose run image bash -c "cd /path/to/somewhere && python a.py"
If you don't mind the commands running in a subshell, just put a set of outer parentheses around the multiple commands to run:
docker run image (cd /path/to/somewhere && python a.py)
TL;DR;
$ docker run --entrypoint /bin/sh image_name -c "command1 && command2 && command3"
A concern regarding the accepted answer is below.
Nobody has mentioned that docker run image_name /bin/bash -c just appends a command to the entrypoint. Some popular images are smart enough to process this correctly, but some are not.
Imagine the following Dockerfile:
FROM alpine
ENTRYPOINT ["echo"]
If you try building it as echo and running:
$ docker run echo /bin/sh -c date
You will get your command appended to the entrypoint, so that result would be echo "/bin/sh -c date".
Instead, you need to override the entrypoint:
$ docker run --entrypoint /bin/sh echo -c date
Docker run reference
In case it's not obvious, if a.py always needs to run in a particular directory, create a simple wrapper script which does the cd and then runs the script.
In your Dockerfile, replace
CMD [ 'python', 'a.py' ]
or whatever with
CMD [ '/wrapper' ]
and create a script wrapper in your root directory (or wherever it's convenient for you) with contents like
#!/bin/sh
set -e
cd /path/to/somewhere
python a.py
In many situations, perhaps also consider rewriting a.py so that it doesn't need a wrapper. Either make it os.chdir() where it needs to be, or have it look for its data files in a directory you configure in its environment or similar.

Resources