Thursday, November 14, 2024
HomeGolangThe shell script kills the go course of, inflicting the script to...

The shell script kills the go course of, inflicting the script to be killed as properly – Code Evaluate


The computerized improve of the server’s resident go service is carried out within the following manner, which includes overwriting the service via a script.

The final logic is as follows

#!/bin/bash

model=$1
md5=$2
log_path=$3
echo "model:$model; md5:$md5" >> $log_path

## 1. delete server rpm
# 1、删除rpm
rm_cmd="sudo rpm -e server-20240202.rpm"
exec_ret=$($rm_cmd 2>&1)
exec_status=$?
if [ ${exec_status} != 0 ]; then
    echo "Didn't take away rpm, exec_ret: $exec_ret" >> $log_path
    exit 1
fi
echo "1、take away rpm efficiently." >> $log_path
sleep 5

## 2. set up new rpm
install_rpm_cmd="sudo rpm -ivh server-20240228.rpm"
exec_ret=$($install_rpm_cmd 2>&1)
exec_status=$?
if [ ${exec_status} != 0 ]; then
    echo "Failed to put in new rpm, exec_ret: $exec_ret" >> $log_path
    exit 1
fi

echo "2、set up rpm efficiently." >> replace.log
sleep 5

## 3. kill outdated course of
command_of_kill="ps -ef | grep server| grep -v replace.sh | grep -v grep | awk '{print $2}' |xargs sudo kill -9"
exec_ret=$(/bin/bash -c "$command_of_kill" 2>&1)
exec_status=$?
if [ ${exec_status} != 0 ]; then
    echo "Didn't kill all server's course of, exec_ret: $exec_ret" >> $log_path
    exit 1
fi

echo "3、kill server's course of efficiently."  >> $log_path
sleep 5

## 4. begin server
command_of_server_start="export PATH=$PATH:/usr/sbin/:/sbin/; cd /usr/native/server/; sudo ./begin.sh begin"

exec_ret=$(/bin/bash -c "$command_of_server_start" 2>&1)
exec_status=$?
if [ ${exec_status} != 0 ]; then
    echo "Failed to begin server, exec_ret: $exec_ret" >> $log_path
    exit 1
fi

echo "4、begin server efficiently." >> $log_path
sleep 5

echo "all、Replace server efficiently." >> $log_path

I downloaded the improve script and improve rpm via go, after which executed the script utilizing the next command

    upgradeAbsPath:=/tmp/replace.sh
	logPath := fmt.Sprintf("/tmp/update-%s-%s.log", model, md5)
	exec_script := fmt.Sprintf("nohup %s %s %s %s > /dev/null 2>&1 &", upgradeAbsPath, model, md5, logPath)
	cmd := exec.Command("/bin/bash", "-c", exec_script)
	log.Infof("exec command: /bin/bash -c %s", exec_script)
	err = cmd.Run()
	if err != nil {
		return fmt.Errorf("cmd Run failed, err: %w", err)
	}
	log.Infof("exec log path %s", logPath)

In response to my understanding, it ought to have the ability to improve usually, however judging from the output, it stopped after deleting the method.

1、take away rpm efficiently.
2、set up rpm efficiently.
3、kill server's course of efficiently.

The brand new service was changed however failed to begin.

I see the identical factor by printing the related processes each second

========begin========
2024-02-28 16:17:03
root     23413     1  0 16:16 ?        00:00:00 /usr/native/server/server
root     23437 23413  0 16:16 ?        00:00:00 /usr/native/server/plugins/plugin1 -f /usr/native/server/plugin/plugin1/plugin1.conf
root     23439 23413  0 16:16 ?        00:00:00 /usr/native/server/plugins/plugin2 -f /usr/native/server/plugin/plugin1/plugin2.conf
========finish========
========begin========
2024-02-28 16:17:04
root     23413     1  0 16:16 ?        00:00:00 /usr/native/server/server
root     23437 23413  0 16:16 ?        00:00:00 /usr/native/server/plugins/plugin1 -f /usr/native/server/plugin/plugin1/plugin1.conf
root     23439 23413  0 16:16 ?        00:00:00 /usr/native/server/plugins/plugin2 -f /usr/native/server/plugin/plugin1/plugin2.conf
root     26815 17337  0 16:17 pts/6    00:00:00 /bin/bash /tmp/replace.sh 1.0.2 md5_fake /tmp/replace.log
root     26894 26815  0 16:17 pts/6    00:00:00 /bin/bash /tmp/replace.sh 1.0.2 md5_fake /tmp/replace.log
root     26895 26894  0 16:17 pts/6    00:00:00 sudo rpm -ivh server-20240228.rpm
root     26896 26895  0 16:17 pts/6    00:00:00 rpm -ivh server-20240228.rpm
root     26897 26896  0 16:17 pts/6    00:00:00 rpm -ivh server-20240228.rpm
========finish========
========begin========
2024-02-28 16:17:05
root     23413     1  0 16:16 ?        00:00:00 /usr/native/server/server
root     23437 23413  0 16:16 ?        00:00:00 /usr/native/server/plugins/plugin1 -f /usr/native/server/plugin/plugin1/plugin1.conf
root     23439 23413  0 16:16 ?        00:00:00 /usr/native/server/plugins/plugin2 -f /usr/native/server/plugin/plugin1/plugin2.conf
root     26815 17337  0 16:17 pts/6    00:00:00 /bin/bash /tmp/replace.sh 1.0.2 md5_fake /tmp/replace.log
root     26894 26815  0 16:17 pts/6    00:00:00 /bin/bash /tmp/replace.sh 1.0.2 md5_fake /tmp/replace.log
root     26895 26894  0 16:17 pts/6    00:00:00 sudo rpm -ivh server-20240228.rpm
root     26896 26895 99 16:17 pts/6    00:00:01 rpm -ivh server-20240228.rpm
========finish========
========begin========
2024-02-28 16:17:06
root     23413     1  0 16:16 ?        00:00:00 /usr/native/server/server
root     23437 23413  0 16:16 ?        00:00:00 /usr/native/server/plugins/plugin1 -f /usr/native/server/plugin/plugin1/plugin1.conf
root     23439 23413  0 16:16 ?        00:00:00 /usr/native/server/plugins/plugin2 -f /usr/native/server/plugin/plugin1/plugin2.conf
root     26815 17337  0 16:17 pts/6    00:00:00 /bin/bash /tmp/replace.sh 1.0.2 md5_fake /tmp/replace.log
root     26894 26815  0 16:17 pts/6    00:00:00 /bin/bash /tmp/replace.sh 1.0.2 md5_fake /tmp/replace.log
root     26895 26894  0 16:17 pts/6    00:00:00 sudo rpm -ivh server-20240228.rpm
root     26896 26895 99 16:17 pts/6    00:00:02 rpm -ivh server-20240228.rpm
========finish========
========begin========
2024-02-28 16:17:07
root     23413     1  0 16:16 ?        00:00:00 /usr/native/server/server
root     23437 23413  0 16:16 ?        00:00:00 /usr/native/server/plugins/plugin1 -f /usr/native/server/plugin/plugin1/plugin1.conf
root     23439 23413  0 16:16 ?        00:00:00 /usr/native/server/plugins/plugin2 -f /usr/native/server/plugin/plugin1/plugin2.conf
root     26815 17337  0 16:17 pts/6    00:00:00 /bin/bash /tmp/replace.sh 1.0.2 md5_fake /tmp/replace.log
root     26894 26815  0 16:17 pts/6    00:00:00 /bin/bash /tmp/replace.sh 1.0.2 md5_fake /tmp/replace.log
root     26895 26894  0 16:17 pts/6    00:00:00 sudo rpm -ivh server-20240228.rpm
root     26896 26895 79 16:17 pts/6    00:00:03 rpm -ivh server-20240228.rpm
========finish========
========begin========
2024-02-28 16:17:08
root     23413     1  0 16:16 ?        00:00:00 /usr/native/server/server
root     23437 23413  0 16:16 ?        00:00:00 /usr/native/server/plugins/plugin1 -f /usr/native/server/plugin/plugin1/plugin1.conf
root     23439 23413  0 16:16 ?        00:00:00 /usr/native/server/plugins/plugin2 -f /usr/native/server/plugin/plugin1/plugin2.conf
root     26815 17337  0 16:17 pts/6    00:00:00 /bin/bash /tmp/replace.sh 1.0.2 md5_fake /tmp/replace.log
root     26894 26815  0 16:17 pts/6    00:00:00 /bin/bash /tmp/replace.sh 1.0.2 md5_fake /tmp/replace.log
root     26895 26894  0 16:17 pts/6    00:00:00 sudo rpm -ivh server-20240228.rpm
root     26896 26895 84 16:17 pts/6    00:00:04 rpm -ivh server-20240228.rpm
========finish========
========begin========
2024-02-28 16:17:09
root     23413     1  0 16:16 ?        00:00:00 /usr/native/server/server
root     23437 23413  0 16:16 ?        00:00:00 /usr/native/server/plugins/plugin1 -f /usr/native/server/plugin/plugin1/plugin1.conf
root     23439 23413  0 16:16 ?        00:00:00 /usr/native/server/plugins/plugin2 -f /usr/native/server/plugin/plugin1/plugin2.conf
root     26815 17337  0 16:17 pts/6    00:00:00 /bin/bash /tmp/replace.sh 1.0.2 md5_fake /tmp/replace.log
root     26894 26815  0 16:17 pts/6    00:00:00 /bin/bash /tmp/replace.sh 1.0.2 md5_fake /tmp/replace.log
root     26895 26894  0 16:17 pts/6    00:00:00 sudo rpm -ivh server-20240228.rpm
root     26896 26895 88 16:17 pts/6    00:00:05 rpm -ivh server-20240228.rpm
========finish========
========begin========
2024-02-28 16:17:10
root     23413     1  0 16:16 ?        00:00:00 /usr/native/server/server
root     23437 23413  0 16:16 ?        00:00:00 /usr/native/server/plugins/plugin1 -f /usr/native/server/plugin/plugin1/plugin1.conf
root     23439 23413  0 16:16 ?        00:00:00 /usr/native/server/plugins/plugin2 -f /usr/native/server/plugin/plugin1/plugin2.conf
root     26815 17337  0 16:17 pts/6    00:00:00 /bin/bash /tmp/replace.sh 1.0.2 md5_fake /tmp/replace.log
root     26894 26815  0 16:17 pts/6    00:00:00 /bin/bash /tmp/replace.sh 1.0.2 md5_fake /tmp/replace.log
root     26895 26894  0 16:17 pts/6    00:00:00 sudo rpm -ivh server-20240228.rpm
root     26896 26895 90 16:17 pts/6    00:00:06 rpm -ivh server-20240228.rpm
========finish========
========begin========
2024-02-28 16:17:11
========finish========
========begin========
2024-02-28 16:17:12
========finish========
========begin========
2024-02-28 16:17:13
========finish========

Based mostly on the related course of quantity, it stands to motive that the improve script course of has separated from the mum or dad course of server, however I don’t know why it was deleted.

However after I entered the server and manually executed the next command, it accomplished executing the script, that’s, the improve was profitable.

nohup /tmp/replace.sh 1.0.2 md5_fake /tmp/replace.log > /dev/null 2>&1 &

Together with course of and log scripts will be mirrored.

replace.log

1、take away rpm efficiently.
2、set up rpm efficiently.
3、kill server's course of efficiently.
4、begin server efficiently.
all、Replace server efficiently.

So it appears that there’s nothing improper with the script. I don’t know what the distinction is between executing the improve script via the go language and executing it manually. Because of this, the improve script can’t be absolutely executed.

I actually can’t discover the issue, I’m wanting ahead to your assist.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments