The automatic upgrade of the server’s resident go service is implemented in the following way, which involves overwriting the service through a script.
The general logic is as follows
#!/bin/bash
version=$1
md5=$2
log_path=$3
echo "version:$version; 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 "Failed to remove rpm, exec_ret: $exec_ret" >> $log_path
exit 1
fi
echo "1、remove rpm successfully." >> $log_path
sleep 5
## 2. install 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 install new rpm, exec_ret: $exec_ret" >> $log_path
exit 1
fi
echo "2、install rpm successfully." >> update.log
sleep 5
## 3. kill old process
command_of_kill="ps -ef | grep server| grep -v update.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 "Failed to kill all server's process, exec_ret: $exec_ret" >> $log_path
exit 1
fi
echo "3、kill server's process successfully." >> $log_path
sleep 5
## 4. start server
command_of_server_start="export PATH=\$PATH:/usr/sbin/:/sbin/; cd /usr/local/server/; sudo ./start.sh start"
exec_ret=$(/bin/bash -c "$command_of_server_start" 2>&1)
exec_status=$?
if [ ${exec_status} != 0 ]; then
echo "Failed to start server, exec_ret: $exec_ret" >> $log_path
exit 1
fi
echo "4、start server successfully." >> $log_path
sleep 5
echo "all、Update server successfully." >> $log_path
I downloaded the upgrade script and upgrade rpm through go, and then executed the script using the following command
upgradeAbsPath:=/tmp/update.sh
logPath := fmt.Sprintf("/tmp/update-%s-%s.log", version, md5)
exec_script := fmt.Sprintf("nohup %s %s %s %s > /dev/null 2>&1 &", upgradeAbsPath, version, 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)
According to my understanding, it should be able to upgrade normally, but judging from the output, it stopped after deleting the process.
1、remove rpm successfully.
2、install rpm successfully.
3、kill server's process successfully.
The new service was replaced but failed to start.
I see the same thing by printing the relevant processes every second
========start========
2024-02-28 16:17:03
root 23413 1 0 16:16 ? 00:00:00 /usr/local/server/server
root 23437 23413 0 16:16 ? 00:00:00 /usr/local/server/plugins/plugin1 -f /usr/local/server/plugin/plugin1/plugin1.conf
root 23439 23413 0 16:16 ? 00:00:00 /usr/local/server/plugins/plugin2 -f /usr/local/server/plugin/plugin1/plugin2.conf
========end========
========start========
2024-02-28 16:17:04
root 23413 1 0 16:16 ? 00:00:00 /usr/local/server/server
root 23437 23413 0 16:16 ? 00:00:00 /usr/local/server/plugins/plugin1 -f /usr/local/server/plugin/plugin1/plugin1.conf
root 23439 23413 0 16:16 ? 00:00:00 /usr/local/server/plugins/plugin2 -f /usr/local/server/plugin/plugin1/plugin2.conf
root 26815 17337 0 16:17 pts/6 00:00:00 /bin/bash /tmp/update.sh 1.0.2 md5_fake /tmp/update.log
root 26894 26815 0 16:17 pts/6 00:00:00 /bin/bash /tmp/update.sh 1.0.2 md5_fake /tmp/update.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
========end========
========start========
2024-02-28 16:17:05
root 23413 1 0 16:16 ? 00:00:00 /usr/local/server/server
root 23437 23413 0 16:16 ? 00:00:00 /usr/local/server/plugins/plugin1 -f /usr/local/server/plugin/plugin1/plugin1.conf
root 23439 23413 0 16:16 ? 00:00:00 /usr/local/server/plugins/plugin2 -f /usr/local/server/plugin/plugin1/plugin2.conf
root 26815 17337 0 16:17 pts/6 00:00:00 /bin/bash /tmp/update.sh 1.0.2 md5_fake /tmp/update.log
root 26894 26815 0 16:17 pts/6 00:00:00 /bin/bash /tmp/update.sh 1.0.2 md5_fake /tmp/update.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
========end========
========start========
2024-02-28 16:17:06
root 23413 1 0 16:16 ? 00:00:00 /usr/local/server/server
root 23437 23413 0 16:16 ? 00:00:00 /usr/local/server/plugins/plugin1 -f /usr/local/server/plugin/plugin1/plugin1.conf
root 23439 23413 0 16:16 ? 00:00:00 /usr/local/server/plugins/plugin2 -f /usr/local/server/plugin/plugin1/plugin2.conf
root 26815 17337 0 16:17 pts/6 00:00:00 /bin/bash /tmp/update.sh 1.0.2 md5_fake /tmp/update.log
root 26894 26815 0 16:17 pts/6 00:00:00 /bin/bash /tmp/update.sh 1.0.2 md5_fake /tmp/update.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
========end========
========start========
2024-02-28 16:17:07
root 23413 1 0 16:16 ? 00:00:00 /usr/local/server/server
root 23437 23413 0 16:16 ? 00:00:00 /usr/local/server/plugins/plugin1 -f /usr/local/server/plugin/plugin1/plugin1.conf
root 23439 23413 0 16:16 ? 00:00:00 /usr/local/server/plugins/plugin2 -f /usr/local/server/plugin/plugin1/plugin2.conf
root 26815 17337 0 16:17 pts/6 00:00:00 /bin/bash /tmp/update.sh 1.0.2 md5_fake /tmp/update.log
root 26894 26815 0 16:17 pts/6 00:00:00 /bin/bash /tmp/update.sh 1.0.2 md5_fake /tmp/update.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
========end========
========start========
2024-02-28 16:17:08
root 23413 1 0 16:16 ? 00:00:00 /usr/local/server/server
root 23437 23413 0 16:16 ? 00:00:00 /usr/local/server/plugins/plugin1 -f /usr/local/server/plugin/plugin1/plugin1.conf
root 23439 23413 0 16:16 ? 00:00:00 /usr/local/server/plugins/plugin2 -f /usr/local/server/plugin/plugin1/plugin2.conf
root 26815 17337 0 16:17 pts/6 00:00:00 /bin/bash /tmp/update.sh 1.0.2 md5_fake /tmp/update.log
root 26894 26815 0 16:17 pts/6 00:00:00 /bin/bash /tmp/update.sh 1.0.2 md5_fake /tmp/update.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
========end========
========start========
2024-02-28 16:17:09
root 23413 1 0 16:16 ? 00:00:00 /usr/local/server/server
root 23437 23413 0 16:16 ? 00:00:00 /usr/local/server/plugins/plugin1 -f /usr/local/server/plugin/plugin1/plugin1.conf
root 23439 23413 0 16:16 ? 00:00:00 /usr/local/server/plugins/plugin2 -f /usr/local/server/plugin/plugin1/plugin2.conf
root 26815 17337 0 16:17 pts/6 00:00:00 /bin/bash /tmp/update.sh 1.0.2 md5_fake /tmp/update.log
root 26894 26815 0 16:17 pts/6 00:00:00 /bin/bash /tmp/update.sh 1.0.2 md5_fake /tmp/update.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
========end========
========start========
2024-02-28 16:17:10
root 23413 1 0 16:16 ? 00:00:00 /usr/local/server/server
root 23437 23413 0 16:16 ? 00:00:00 /usr/local/server/plugins/plugin1 -f /usr/local/server/plugin/plugin1/plugin1.conf
root 23439 23413 0 16:16 ? 00:00:00 /usr/local/server/plugins/plugin2 -f /usr/local/server/plugin/plugin1/plugin2.conf
root 26815 17337 0 16:17 pts/6 00:00:00 /bin/bash /tmp/update.sh 1.0.2 md5_fake /tmp/update.log
root 26894 26815 0 16:17 pts/6 00:00:00 /bin/bash /tmp/update.sh 1.0.2 md5_fake /tmp/update.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
========end========
========start========
2024-02-28 16:17:11
========end========
========start========
2024-02-28 16:17:12
========end========
========start========
2024-02-28 16:17:13
========end========
Based on the relevant process number, it stands to reason that the upgrade script process has separated from the parent process server, but I don’t know why it was deleted.
But when I entered the server and manually executed the following command, it completed executing the script, that is, the upgrade was successful.
nohup /tmp/update.sh 1.0.2 md5_fake /tmp/update.log > /dev/null 2>&1 &
Including process and log scripts can be reflected.
update.log
1、remove rpm successfully.
2、install rpm successfully.
3、kill server's process successfully.
4、start server successfully.
all、Update server successfully.
So it seems that there is nothing wrong with the script. I don’t know what the difference is between executing the upgrade script through the go language and executing it manually. As a result, the upgrade script cannot be fully executed.
I really can’t find the problem, I’m looking forward to your help.