通过脚本清除自定义 .deb 包时突然退出

通过脚本清除自定义 .deb 包时突然退出

我是一名开发人员,有时会处理与部署相关的工作。我习惯Jenkins将我的应用程序自动部署到ubuntu 14.04机器上。我的应用程序打包为.deb,并将一些支持应用程序打包为jar。我编写了一个小型 shell 脚本来安装 debian/jar。我正在尝试清除现有应用程序并安装新应用程序。但有时,脚本在清除时退出,其余步骤未执行。这给我带来了一些问题,因为很多时候我必须登录服务器并重新安装并重新启动我的应用程序。执行它时我没有在控制台中收到任何错误,因此无法提供更多信息。

将我的脚本粘贴到这里,有人可以指出我的脚本中可能存在什么问题。

mod=$1
defaultType="true"
isJar=${2:-$defaultType}
echo "before killing process mod: $1"
sudo pkill -f "${mod}-api"
echo "module ${mod}"
echo "after process kill"
cd /home/administrator/apps/

if [ ${isJar} = "false" ]
then
#       echo "Purging debian package"
#       sudo dpkg --purge ${mod}-api
        echo "Installing debian package"        
        sudo dpkg --install ${mod}-api*.deb
        sudo service ${mod}mod start
else
        jarfile=${mod}-api*.jar
        echo "$jarfile"
        nohup java -Dlogback.configurationFile="$mod-logback.xml" -jar ${jarfile} &>/dev/null &
fi
exit

我尝试了 Thomas 提供的解决方案,但出现以下错误:

dpkg: error: dpkg status database is locked by another process
Starting Common Module: Commonstart-stop-daemon: unable to stat /usr/bin/common-api (No such file or directory)
.
Selecting previously unselected package common-api.
(Reading database ... 177687 files and directories currently installed.)
Preparing to unpack common-api_0.8.9.55-DEV-SNAPSHOT_all.deb ...
Unpacking common-api (0.8.9.55-DEV-SNAPSHOT) ...
dpkg: error processing archive 2 (--install):
 cannot access archive: No such file or directory
Setting up common-api (0.8.9.55-DEV-SNAPSHOT) ...
Creating system group: common-api
Creating system user: common-api in common-api with common-api daemon-user and shell /bin/false
Errors were encountered while processing:
 2

答案1

我修改了你的脚本,制作了一些日志文件。此外,还进行了一些错误处理,这样如果失败,你只需在某个执行步骤中退出即可。此外,我还进行了一些小的样式调整,因为我非常需要添加某些样式。

请注意,我创建了一个目录,用于保存此处的日志。您可以将其创建在/tmp/任何custominstall-logs您想要的地方,但我建议将其保留在这里/tmp/(这样,如果没有发生错误,您就会有错误日志)。

我对风格和其他方面有点挑剔,所以我对你的脚本做了一些修改并添加了注释。(我没有添加错误检查来检查你注释掉的“清除包”内容的位置)

#!/bin/bash
mod=$1
defaultType="true"
isJar=${2:-$defaultType}
echo "before killing process mod: $1"
sudo pkill -f "${mod}-api"
echo "module ${mod}"
echo "after process kill"
cd /home/administrator/apps/

# Using an error checking folder in /tmp/... make it.
mkdir /tmp/custominstall-logs/

if [ ${isJar} = "false" ]
then
#       echo "Purging debian package"
#       sudo dpkg --purge ${mod}-api 2&>1 > /tmp/custominstall-logs/apt-log-purge
#       if [ $? -ne 0 ]; then
#           echo "Error removing package; please refer to the log file "
#           echo "in /tmp/custominstall-logs/apt-log-purge for details."
#           exit 1   # Exit with an error code, instead of 0
#       fi 
        echo "Installing debian package"        
        sudo dpkg --install ${mod}-api*.deb 2&>1 > /tmp/custominstall-logs/apt-log-install
        if [ $? -ne 0 ]; then
            echo "Error installing package; please refer to the log file "
            echo "in /tmp/custominstall-logs/apt-log-install for details."
            exit 2   # Exit with an error code, instead of 0
        fi 
        sudo service ${mod}mod start 2&>1 > /tmp/custominstall-logs/service-start-log
        if [ $? -ne 0 ]; then
            echo "Error starting the service; please refer to the log file "
            echo "in /tmp/custominstall-logs/service-start-log for details."
            exit 3   # Exit with an error code, instead of 0
        fi 
else
        jarfile=${mod}-api*.jar
        echo "$jarfile"
        nohup java -Dlogback.configurationFile="$mod-logback.xml" -jar \
            ${jarfile} &>/dev/null &
fi

# If we didn't exit earlier with an error code, then we exit with
# Code 0 for "Success".
exit 0  

相关内容