If I run sudo apt-get upgrade then I get shown a list of packages and if any of these are services, they may need to be restarted (which is done automatically when installing the updates). How can I tell if one of these packages will require a restart of a service, for example if I update a package which is not named mysql* then I'd like to know before I install the updates that this might trigger a restart of the mysql service (if the package is named mysql* I'll assume a restart to service is required). Or does this never happen? If a restart of the mysql service is required, I'll remove that node from the load balancer pools and let all connections to it finish before installing the updates.

Just to clarify, I'm referring to restarting of services, not rebooting the whole machine.

Thanks for your help.


Use the -s flag:

-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.

Just the output is printed, you can see if it would restart a service there.


One way is to check their Debian package control files before installing them. I tested this only with few packages. (It may need tunning)

  1. Remove APT packages cache, Just an easy way to track the new packages:

    sudo rm -f /var/cache/apt/archives/*.deb
  2. Run APT in download-only mode

    sudo apt-get -d upgrade


    sudo apt-get -d install ...
  3. Extract their control files:

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

    Keep only preinst, postinst, prerm & postrm scripts:

    find tmp/ ! -name "preinst" ! -name "postinst" ! -name "prerm" ! -name "postrm"  -type f -exec rm -f {} \;
  4. Search for service (init.d/upstart) related commands:

    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 to show 2 lines before match. Some scripts uses variables within command, so showing some heading lines may provides better understand of what the script trying to do.

  5. Install packages then clean up

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

Example output with no lines before:

$ 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
  • 1
    Thanks, this sounds a fail-safe way to do it, but I'm going to have to accept the answer from Pabi as it is much simpler and shorter. Thanks though. – Luke Cousins Jun 18 '14 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.