0

i need your support to create a script necessary to deploy, start and stop a service from one server to the others server. This service is a jar with a properties file.

I would create a menu script with more choice.

I've created it like this:

#!/bin/bash
PS3='Please enter your choice: '
options=("Start" "Stop" "Deploy" "Quit")
select opt in "${options[@]}"
do
case $opt in
"Start Shaw")
echo "you chose choice 1"
;;
"Stop Shaw")
echo "you chose choice 2"
;;
"Check Shaw Status")
echo "you chose choice 3"
;;
"Quit")
break
;;
*) echo "invalid option $REPLY";;
esac
done

i don't know how to connect the choices to the various functions.

Start function:

I thought of such a thing:

function start_service ( )
{
ssh $1 'nohup /data/jdk1.8.0_151/bin/java -jar -Xmx1024M /data/apps/file.jar &'
}
echo "Starting servers"
for serv_name in $( cat /data/server_list/list.txt )
do start_service ${serv_name} &
echo $! > pid.file &
sleep 5
done

where list.txt is a file with all the servers where this service need to be start. The problem is that i need that the file pid.file is created on each server and that by launching it from the first server, a process is not created on the first server for each server listed but one process for each server.

Stop:

i need that starting from the pid.file, a command that read the file.pid and stop that process like: kill $(cat pid.file)

Deploy:

this is the very problem. I'd like to create a folder where putting the jar file or the properties file inside it, selecting this function, the file is deployed on all servers in the defined paths: jar file: /pathtofile/apps/file.jar file properties: /pathtofile/conf/file.properties

PS: i can't use systemd for security question i don't have root access to the servers.

2
  • There's a lot of questions here. I would recommend refactoring your post to focus on a specific question rather than asking for more general help writing your script. Commented Jul 1, 2020 at 20:44
  • can i have an help with the start and stop function....the last request of deploy at the moment it is not necessary. I would receive an help to associate the menu choice with the different functions Commented Jul 2, 2020 at 11:12

1 Answer 1

0

SystemD does not work for root processes only (that is the system level). You can create a SystemD user service (systemctl --user ...).

#!/bin/bash
PS3='Please enter your choice: '
options=("Start" "Stop" "Deploy" "Quit")
select opt in "${options[@]}"; do
        case $opt in
                "Start")
                        echo "Starting servers"
                        for serv_name in $( cat /data/server_list/list.txt ); do
                                start_service "$serv_name" &
                                echo $! > "$serv_name".pid & # PID of the background subshell
                                sleep 5
                        done
                ;;
                "Stop")
                        echo "you chose choice 2"
                ;;
                "Deploy")
                        echo "you chose choice 3"
                ;;
                "Quit")
                        break
                ;;
                *) echo "invalid option $REPLY";;
        esac
done
3
  • I've tried to use this script but the problem is that after use the option 1, on the first server i see: process /data/jdk1.8.0_151/bin/java -jar -Xmx1024M /data/app/file.jar but also server 2 process /data/jdk1.8.0_151/bin/java -jar -Xmx1024M /data/app/file.jar; server 3 process.....it is normal? Commented Jul 3, 2020 at 9:57
  • @Emanuele The for serv_name in $( cat /data/server_list/list.txt ); do is your code, not mine. So I had much reason to assume that is intentional. However, just change it to your needs (which are not really clear). Commented Jul 3, 2020 at 12:32
  • this part is only to insert in ssh the name of the server and the file list.txt is a list of the server including the first where the script is launched. I don't know why the service is started correctly on the other but on the first i see 33 process. Commented Jul 3, 2020 at 13:03

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.