Running a Bash Script Continuously Across Multiple Servers (Local, Jumpbox and Dev server) - node.js

I wrote a bash script to build a docker container (a NODE JS app) at the end so that the application runs on a DEV server. The thing is that the bash script has to be run in stages across multiple servers i.e., we need to build the docker container in local, ssh to a jump box and rsync it over there and then rsync it from the jumpbox to the dev server and then ssh to it again (if this process is done manually).
If you look at step 5 in the BASH script below, the rsync gets completed and the script runs to SSH to the jumpbox server, however, the script stops over there and I am just logged in to the jumpbox.
Could someone let me know what is wrong with this bash script and how should I fix it?
Thank you in advance.
Best,
R
#!/bin/bash
#This script allows users to deploy the application with minimal work
echo -n "Shall we begin the deployment process? Are you ready to rule the world with QA Hero's next version? `\n` If you are, then type YES and press [ENTER]?: "
read begin
echo "Wohoooo! You did the right thing! We are now ready to roll and you can actually see your terminal scroll! Lol, what a troll!"
echo -n "Yo mate! Can you enter your enumber and press [ENTER]?: "
read enumber
docker build --build-arg NODE_ENV=staging -t course-hero-x -f Dockerfile .
echo "The file has been built! Step 1 completed"
termdown 3
echo "BOOM! Get ready for step 2."
docker save -o course-hero.tar course-hero-x
termdown 3
echo "BOOM! Get ready for step 3."
rsync -avzh --progress --stats course-hero.tar `echo ${enumber}`#10.188.129.25:/home/`echo ${enumber}`
termdown 3
echo "BOOM! Get ready for step 4."
ssh `echo ${enumber}`#10.188.129.25
termdown 3
echo "BOOM! Get ready for step 5."
ls -la
termdown 3
echo "BOOM! Get ready for step 6."
if ["$enumber" == "e30157" || "$enumber" == "E30167"]; then
rsync -avzh --progress --stats course-hero.tar `echo ${enumber^^}`#10.80.63.65:/home/eh7/`echo ${enumber^^}`
elif ["$enumber" == "e32398" || "$enumber" == "E32398"]; then
rsync -avzh --progress --stats course-hero.tar `echo ${enumber^^}`#10.80.63.65:/home/eh8/`echo ${enumber^^}`
else
echo "You cannot access this system."
fi
termdown 3
echo "BOOM! Get ready for step 7."
ssh `echo ${enumber^^}`#10.80.63.65
echo "BOOM! Get ready for step 8."
ls -la
echo "BOOM! Get ready for step 9."
sudo docker load -i course-hero.tar
termdown 3
echo "BOOM! Get ready for step 10."
sudo docker ps
termdown 3
echo "BOOM! Get ready for step 11."
sudo docker stop $(sudo docker ps -a -q)
termdown 3
echo "BOOM! Get ready for step 12."
sudo docker rm $(sudo docker ps -a -q)
termdown 3
echo "BOOM! Get ready for step 13."
sudo docker load -i course-hero.tar
termdown 3
echo "BOOM! Get ready for step 14."
sudo docker run -d -e TZ=Australia/Melbourne --net=courseHero -p 0.0.0.0:80:3000 course-hero-x
echo "QA Hero is up and running. Go to http://cohero-dev.rmit.edu.au to checkout the latest version!"
termdown 3
echo "Step 15 completed! We are done here! See you next time homey!"

Once you've open an ssh session to the other box your local script stops because it's waiting for the ssh session to end. You're just left sitting at a command prompt on the remote box.
It looks like what you're intending to have happen is the commands following your ssh run on the remote box. To send the commands to the remote machine redirect them:
ssh ${enumber}#10.188.129.25 <<'ENDSSH'
# series of commands to
# run on remote host
ENDSSH
Consider running your script through Shellcheck — you've got several problems with it beyond the one you asked about.

Related

How to automatically terminate ssh connection after starting a script in tmux window?

I'm trying to run a script in a tmux environment on another computer using ssh, but the ssh connection won't terminate until the script has finished. Let me explain this in detail:
This is test_ssh.sh:
#!/bin/bash
name="computername"
ssh $name /bin/bash <<\EOF
cd /scratch
mkdir test
cd test
cp /home/user/test_tmux3.sh .
tmux -c ./test_tmux3.sh &
echo 1 # at this point it waits until test_tmux3.sh is finished, instead of exiting :(
EOF
This is test_tmux3.sh (as a test to see if anything happens):
#!/bin/bash
mkdir 0min
sleep 60
mkdir 1min
sleep 60
mkdir 2min
At the end I would like to loop over multiple computers ($name) to start a script on each of them. The problem I am having right now is that test_ssh.sh waits after the echo 1 and only exits after tmux -c test_tmux3.sh & is finished (after 2 minutes). If I manually enter control-C test_ssh.sh stops and tmux -c test_tmux3.sh & continues running on the computer $name (which is what I want). How can automate that last step and get ssh to exit on its own?
Start the command in a detached tmux session.
#!/bin/bash
name="computername"
ssh $name /bin/bash <<\EOF
mkdir /scratch/test
cd /scratch/test
cp /home/user/test_tmux3.sh .
tmux new-session -d ./test_tmux3.sh
echo 1
EOF
Now, the tmux command will exit as soon as the new session is created and the script is started in that session.
Have you tried to use nohup command to tell to the process keep running after exit?:
#!/bin/bash
name="computername"
ssh $name /bin/bash <<\EOF
cd /scratch
mkdir test
cd test
cp /home/user/test_tmux3.sh .
nohup tmux -c ./test_tmux3.sh &
echo 1 # at this point it waits until test_tmux3.sh is finished, instead of exiting :(
EOF

Ubuntu run script as non-root user

I've an init script (/etc/init.d) that should run a my executable jar file as a serviceat boot. I need that this script is runned by a specified user.
With su & sudo is possibile but it split the process and I don't like this.
There is another way to run this script as limited user?
This is the relevant part of my init script:
#!/bin/bash
APP_NAME="myapp"
APP_HOME=/home/user1/jetty
JAVA_HOME=/opt/local/java/latest
echo "Service $APP_NAME - [$1]"
echo "JAVA_HOME -> $JAVA_HOME"
echo "APP_HOME -> $APP_HOME"
echo "APP_NAME -> $APP_NAME"
function start {
if pkill -0 -f $APP_NAME.jar > /dev/null 2>&1
then
echo "Service [$APP_NAME] is already running. Ignoring startup request."
exit 1
fi
echo "Starting application..."
cd $APP_HOME
nohup $JAVA_HOME/bin/java -jar $APP_HOME/$APP_NAME.jar\
< /dev/null > $APP_HOME/logs/app.log 2>&1 &
}
On Ubuntu you should use the program start-stop-daemon for this. It has options for launching daemons as different users, managing pid files, changing the working directory, and pretty much anything else that is usually needed by init scripts.

Run file for game server

Alright, so I have a .sh file that I run that will launch my server with the certain specifics that I'm looking for. It launches the server through screen into it's own screen. Here's the code for my run.sh file.
#!/bin/bash
# run.sh
# conversion of run.bat to shell script.
echo "Protecting srcds from random crashes"
echo "Now launching Garrys Mod RequiemRP"
sleep 5
screen -A -m -d -S gmserver ./srcds_run -console -game garrysmod +maxplayers 32 +map rp_downtown_v6 -autoupdate
echo "Server initialized. Type screen -x to resume"
Usually I use a batch file to do this, but I'm now using linux for my server hosting. Part of that batch file was if srcds (the server itself) were to crash, the run.bat file would restart the server automatically. I'm looking to do this with my run.sh file, but I'm unsure how to.
Perhaps you could make a service or script that will periodically check if the process is running. This will check if it's on and if it isn't, it will turn it on when executed.
#!/bin/bash
ps cax | grep srcds > /dev/null
if [ $? -eq 0 ]; then
exit
else
bash /path/to/run.sh
fi
I tested the command and it works. For my virtualized debian 9 system.

Linux Ubuntu: Script works in terminal, but not .sh

Issue Summary: My script works as it should when typed into the terminal, however, it does not work correctly when executed in terminal from a .sh file, why is this?
Script:
echo World of Clucky - Frisnuk "\033]0;Frisnuk - World of Clucky\a"
#! /usr/bin/env bash
BINDIR="$(dirname "$(readlink -fn "$0")")"
cd "$BINDIR"
while true
do
source /home/clucky/MinecraftServers/Frisnuk/serverconfig/config.sh
#Start Server
java -Xms2000M -Xmx2000M -jar $serverjar.jar nogui
if [ "`date +%w%H`" = "001" ]
then
#Delete map files for The End
rm -R /Frisnuk_the_end
echo "End has been successfully reloaded"
echo "[`date +%D\ %T`] End Reloaded" >> /home/clucky/MinecraftServers/Frisnuk/EndRestart.txt
#Change Item of The Week
weekofyear=`date +%y\-%U`
s=$(<serverconfig/ItemofTheWeek/item$weekofyear.txt)
set -- $s
itemoftheweekid=$2
itemoftheweekprice=$3
xmlstarlet edit -L -u "/scs-shop/itemStack[#type='double']" -v $itemoftheweekid /plugins/ShowCaseStandalone/ffs-storage/ffss_cac8480951254352116d5255e795006252d404d8
xmlstarlet edit -L -u "/scs-shop/price[#type='double']" -v $itemoftheweekprice /plugins/ShowCaseStandalone/ffs-storage/ffss_cac8480951254352116d5255e795006252d404d8
fi
echo "If you want to stop the restart and shut the server off instead, please press Ctrl+C at this time"
for i in 5 4 3 2 1
do
echo "$i..."
sleep 1
done
echo "Restarting Server"
clear
done
Instead of working and running the server, it just says this:
World of Clucky - Frisnuk
/home/clucky/MinecraftServers/Frisnuk/craftminecraft.sh: 7: /home/clucky/MinecraftServers/Frisnuk/craftminecraft.sh: source: not found
Error: Unable to access jarfile .jar
If you want to stop the restart and shut the server off instead, please press Ctrl+C at this time
5...
4...
3...
2...
1...
I am going to take a shower shortly, but I will be returning either later tonight, or tomorrow morning. Thank you in advanced for your assistance.
You put an echo before the shebang, so your script is being interpreted by dash, not bash.
dash doesn't include source, because it's not standard.
Correct your shebang and it'll do the trick.
The standard way to source a script is executing it with ..
Instead of source ./myScript.sh, you do . ./myScript.sh. They're the same in bash.

Adding a service startup script for Amazon linux AMI

I am using an Amazon Linux AMI and doing some custom modifications(added an axis2server, etc) on it and saving it as a new AMI. Now what I want to do is when the AMI boots up, start up axis2server(ie.axis2server should automatically start when the instance boots up). For that I used a init script like below and ran the following command:
chkconfig --add axisservice
But when I launch a new instance from my image, the axis2server is not getting started.
I just only need to execute the script /home/ec2-user/axis2-1.6.1/bin/axis2server.sh at startup. Am I missing anything here?
#! /bin/sh
# Basic support for IRIX style chkconfig
###
# chkconfig: 235 98 55
# description: Manages the services you are controlling with the chkconfig command
###
case "$1" in
start)
echo -n "Starting axisservice"
touch ~/temp.txt
cd /home/ec2-user/axis2-1.6.1/bin
./axis2server.sh &
echo "."
;;
stop)
echo -n "Stopping axisservice"
echo "."
;;
*)
echo "Usage: /sbin/service axisservice {start|stop}"
exit 1
esac
exit 0
I went through https://help.ubuntu.com/community/CloudInit as well and it provides a mechanism called User-Data Scripts, where a user can execute a script when launching the script.
$ euca-run-instances --key mykey --user-data-file myscript.sh ami-axxxx
This is a command line option and what I want is something like when I launch the instance through the UI, the script should be started.Therefore, I think the above option can not be used in my case. Please correct me if I am wrong.
Thanks,
H.
I bet the environment is not set(up correctly). This means that I am guessing that your shell script tries to start another program and it's not to be found.
So at first, I'd adjust the start part of your script (current):
echo -n "Starting axisservice"
touch ~/temp.txt
cd /home/ec2-user/axis2-1.6.1/bin
./axis2server.sh &
echo "."
Edited:
echo -n "Starting axisservice"
touch ~/temp.txt
cd /home/ec2-user/axis2-1.6.1/bin
./axis2server.sh
RETVAL=$?
[ $RETVAL -eq 0 ] && echo Success
[ $RETVAL -ne 0 ] && echo Failure
echo "."
So what did I do?
removed & so script waits for your shell script (axis2server.sh) to complete
checked the return status ($?) of your shell script
Further debugging:
Add set -x to your scripts to enable tracing and log both stderr and stdout.
Questions:
Are you are aware that stop (in your service script) doesn't do anything?
touch ~/temp.txt is that supposed to create /root/temp.txt? (I'm guessing root runs this script.)
If none of my suggestions work, can you share axis2server.sh and paste stderr and stdout?

Resources