Bash script to run jar service remotely

Hi everyone,

I would need someone to enlighten me and support me for the creation of a script in bash that allows me from the first server on which it will be launched to manage an application in jar (start, stop, deploy) on all the other machines (in total 32) .
I thought of something like this as a script that provides the option to choose a menu option:

#!/bin/bash

## ----------------------------------
# Step #1: Define variables
# ----------------------------------
SERVER=/data/SpringBoot/Shaw/scripts/server_list/list.txt

# ----------------------------------
# Step #2: User defined function
# ----------------------------------
pause(){
  read -p "Press [Enter] key to continue..." fackEnterKey
}

start_shaw ()
{
for serv_name in $(cat $SERVER)
do
ssh $serv_name 'bash -s' < /data/SpringBoot/Shaw/app/Shaw_start.sh
echo "Starting Shaw on $serv_name"
sleep 5
done
echo
wait
exit 0
}

# do something in two()
two(){
        echo "two() called"
        pause
}

# function to display menus
show_menus() {
        clear
        echo "~~~~~~~~~~~~~~~~~~~~~"    
        echo "SEBASTIAN SHAW MANAGEMENT"
        echo "~~~~~~~~~~~~~~~~~~~~~"
        echo "1. Start Service"
        echo "2. Stop Service"
        echo "3. Exit"
}
# read input from the keyboard and take a action
# invoke the one() when the user select 1 from the menu option.
# invoke the two() when the user select 2 from the menu option.
# Exit when user the user select 3 form the menu option.
read_options(){
        local choice
        read -p "Enter choice [ 1 - 3] " choice
        case $choice in
                1) start_shaw ;;
                2) two ;;
                3) exit 0;;
                *) echo -e "${RED}Error...${STD}" && sleep 2
        esac
}

# ----------------------------------------------
# Step #3: Trap CTRL+C, CTRL+Z and quit singles
# ----------------------------------------------
trap '' SIGINT SIGQUIT SIGTSTP

# -----------------------------------
# Step #4: Main logic - infinite loop
# ------------------------------------
while true
do

        show_menus
        read_options
done 

where the script Shaw_start.sh that is stored on each server is:

#!/bin/bash

nohup /data/jdk1.8.0_151/bin/java -jar -Xmx1024M Shaw.jar &

echo $! > pid.file &

exit 0

The problem is that although I get out that he is performing the starting then in the end he did nothing.
No service is started and its file is created.

Can someone please help me?

Try this:

ssh -o BatchMode=yes -o StrictHostKeyChecking=no -tt $serv_name /bin/bash /data/SpringBoot/Shaw/app/Shaw_start.sh

I'm assuming your servers are password less and communicating will each other. If not then you can also use sshpass utility

2 Likes

Also you need to do more investigation at your end - Some question might have help you
1. Did you see any PID generated for the command you ran in the background.
2. Did you try manually and see what's causing this.
3. Did you try invoking the jar from remote server and see what's the behaviour

Hi Mannu25251,

yes, servers are password less and communicating will each other.

  1. using my script from first server, nothing will made on each server and so no PID.
  2. launching manually the script Shaw_sart.sh, the jar component start correctly
  3. launching ssh server02 '/data/jdk1.8.0_151/bin/java -jar -Xmx1024M /data/SpringBoot/Shaw/app/Shaw.jar', service start correctly.

i tried and i receive this error:
Starting Shaw
tcgetattr: Inappropriate ioctl for device
Connection to server02 closed.

tcgetattr: Inappropriate ioctl for device normally means that some program attempted to do a terminal control operation but its standard I/O streams weren't connected to a terminal. This simply means you have no terminal associated with the "current" process.

For debugging, run something else instead of Shaw_start.sh, just a simple bash command and see if this error still persist. It's working at my end though!

My assumption would be something in your script is doing this. I can't help you any further, but perhaps something you can search that offers some clues like /dev/null or something else, I'm not sure!

2 Likes

The first Google search for "tcgetattr: Inappropriate ioctl for device" hit this post.
Hope it helps.

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.