如何通过安装其他软件包的更新来检查需要重新启动的服务(服务,而不是机器)

如何通过安装其他软件包的更新来检查需要重新启动的服务(服务,而不是机器)

如果我运行sudo apt-get upgrade,则会看到一个软件包列表,如果其中任何一个是服务,则可能需要重新启动它们(安装更新时会自动完成)。如何判断这些软件包中是否需要重新启动服务,例如,如果我更新一个未命名的软件包,mysql*那么我希望在安装更新之前知道这是否会触发 mysql 服务的重新启动(如果该软件包已命名,mysql*我会假设需要重新启动服务)。还是这种情况永远不会发生?如果需要重新启动 mysql 服务,我将从负载均衡器池中删除该节点,并让所有与它的连接在安装更新之前完成。

只是为了澄清,我指的是重新启动服务,而不是重新启动整个机器。

感谢您的帮助。

答案1

使用 -s 标志:

-s, --simulate, --just-print, --dry-run, --recon, --no-act
No action. Perform a simulation of events that would occur but do not actually change the system.

只打印输出,您可以查看它是否会在那里重新启动服务。

答案2

一种方法是在安装之前检查 Debian 软件包control文件。我只用几个软件包测试了这种方法。(可能需要调整)

  1. 删除 APT 包缓存,这只是一种跟踪新包的简单方法:

    sudo rm -f /var/cache/apt/archives/*.deb
    
  2. 在仅下载模式下运行 APT

    sudo apt-get -d upgrade
    

    或者

    sudo apt-get -d install ...
    
  3. 提取其control文件:

    mkdir tmp
    for i in $(ls /var/cache/apt/archives/*.deb) ; do echo $i;  dpkg -e $i tmp/$(basename $i .deb); done
    

    仅保留preinstpostinstprerm&postrm脚本:

    find tmp/ ! -name "preinst" ! -name "postinst" ! -name "prerm" ! -name "postrm"  -type f -exec rm -f {} \;
    
  4. 搜索服务(init.d/upstart)相关命令:

    grep -B2 -r -e"service " -e" start" -e"start " -e" restart" -e"restart " -e" stop" -e"stop " -e "/etc/init.d" -e "invoke-rc.d " tmp/
    

    -B2在匹配前显示 2 行。一些脚本在命令中使用变量,因此显示一些标题行可能有助于更好地理解脚本试图做什么。

  5. 安装软件包然后清理

    # WARRNING, recheck your folder for correct name
    rm -rf tmp
    

之前没有任何行的示例输出:

$ grep -r -e"service " -e" start" -e"start " -e" restart" -e"restart " -e" stop" -e"stop " -e "/etc/init.d" -e "invoke-rc.d " tmp/
tmp/mysql-server-5.5_5.5.37-0ubuntu0.14.04.1_amd64/postinst:  if [ -x /usr/sbin/invoke-rc.d ]; then
tmp/mysql-server-5.5_5.5.37-0ubuntu0.14.04.1_amd64/postinst:    invoke-rc.d mysql $1
tmp/mysql-server-5.5_5.5.37-0ubuntu0.14.04.1_amd64/postinst:    /etc/init.d/mysql $1
tmp/mysql-server-5.5_5.5.37-0ubuntu0.14.04.1_amd64/postinst:# In case the server wasn't running at all it should be ok if the stop
tmp/mysql-server-5.5_5.5.37-0ubuntu0.14.04.1_amd64/postinst:set +e; invoke stop; set -e
tmp/mysql-server-5.5_5.5.37-0ubuntu0.14.04.1_amd64/postinst:db_stop # in case invoke failes
tmp/mysql-server-5.5_5.5.37-0ubuntu0.14.04.1_amd64/postinst:if [ -x "/etc/init.d/mysql" ] || [ -e "/etc/init/mysql.conf" ]; then
tmp/mysql-server-5.5_5.5.37-0ubuntu0.14.04.1_amd64/postinst:    invoke-rc.d mysql start || exit $?
tmp/mysql-server-5.5_5.5.37-0ubuntu0.14.04.1_amd64/prerm:if [ -x "/etc/init.d/mysql" ] || [ -e "/etc/init/mysql.conf" ]; then
tmp/mysql-server-5.5_5.5.37-0ubuntu0.14.04.1_amd64/prerm:   invoke-rc.d mysql stop || exit $?
tmp/mysql-server-5.5_5.5.37-0ubuntu0.14.04.1_amd64/preinst:# Try to stop the server in a sane way. If it does not success let the admin
tmp/mysql-server-5.5_5.5.37-0ubuntu0.14.04.1_amd64/preinst:    if [ ! -x /etc/init.d/mysql ]; then return; fi
tmp/mysql-server-5.5_5.5.37-0ubuntu0.14.04.1_amd64/preinst:    if [ -x /usr/sbin/invoke-rc.d ]; then
tmp/mysql-server-5.5_5.5.37-0ubuntu0.14.04.1_amd64/preinst:      cmd="invoke-rc.d mysql stop"
tmp/mysql-server-5.5_5.5.37-0ubuntu0.14.04.1_amd64/preinst:      cmd="/etc/init.d/mysql stop"
tmp/mysql-server-5.5_5.5.37-0ubuntu0.14.04.1_amd64/preinst:      echo "There is a MySQL server running, but we failed in our attempts to stop it." 1>&2
tmp/mysql-server-5.5_5.5.37-0ubuntu0.14.04.1_amd64/preinst:      db_stop    
tmp/mysql-server-5.5_5.5.37-0ubuntu0.14.04.1_amd64/preinst:6691f2fdc5c6d27ff0260eb79813e1bc  /etc/init.d/mysql
tmp/mysql-server-5.5_5.5.37-0ubuntu0.14.04.1_amd64/preinst: if [ -e "/etc/init.d/mysql" ] && [ -L "/etc/init.d/mysql" ] \
tmp/mysql-server-5.5_5.5.37-0ubuntu0.14.04.1_amd64/preinst:    && [ $(readlink -f "/etc/init.d/mysql") = /lib/init/upstart-job ]
tmp/mysql-server-5.5_5.5.37-0ubuntu0.14.04.1_amd64/preinst:     rm -f "/etc/init.d/mysql"
tmp/mysql-server-5.5_5.5.37-0ubuntu0.14.04.1_amd64/postrm:# Try to stop the server in a sane way. If it does not success let the admin
tmp/mysql-server-5.5_5.5.37-0ubuntu0.14.04.1_amd64/postrm:  if [ -x /usr/sbin/invoke-rc.d ]; then
tmp/mysql-server-5.5_5.5.37-0ubuntu0.14.04.1_amd64/postrm:    invoke-rc.d mysql stop
tmp/mysql-server-5.5_5.5.37-0ubuntu0.14.04.1_amd64/postrm:    /etc/init.d/mysql stop
tmp/mysql-server-5.5_5.5.37-0ubuntu0.14.04.1_amd64/postrm:    echo "Trying to stop the MySQL server resulted in exitcode $?." 1>&2
tmp/mysql-server-5.5_5.5.37-0ubuntu0.14.04.1_amd64/postrm:      stop_server
tmp/tracker-miner-fs_0.16.4-0ubuntu0.1_amd64/postinst:        if [ -x /etc/init.d/procps ]; then
tmp/tracker-miner-fs_0.16.4-0ubuntu0.1_amd64/postinst:            invoke-rc.d procps start || true

相关内容