首页 > 代码库 > ubuntu在关机画面出现前进行网络连接的关闭的修改

ubuntu在关机画面出现前进行网络连接的关闭的修改

/*********************************************************************
 * Author  : Samson
 * Date    : 05/07/2014
 * Test platform:
 *              3.11.0-12-generic #19-Ubuntu
 *              GNU bash, version 4.2.45
 * *******************************************************************/
mdm.conf 界面关闭服务配置


# mdm - MDM Display Manager
#
# The display manager service manages the X servers running on the
# system, providing login and auto-login services

description     "MDM Display Manager"
author          "Linux Mint <root@linuxmint.com>"

#表示只有当事件filesystem被激活且(运行在不是关机和重启的运行级别的时候)且(dbus服务已经启动)且(card0已经添加或udev-fallback-graphics服务已经关闭)时,此服务启动
start on ((filesystem
           and runlevel [!06]
           and started dbus
           and (drm-device-added card0 PRIMARY_DEVICE_FOR_DISPLAY=1
                or stopped udev-fallback-graphics))
          or runlevel PREVLEVEL=S)

#表示当系统运行在关机、单用户、重启的状态下时,关闭此服务
stop on runlevel [016]

#以下是注册3个事件到upstart事件驱动中
emits login-session-start
emits desktop-session-start
emits desktop-shutdown

#以下为进行服务启动后的处理过程
script
    if [ -n "$UPSTART_EVENTS" ]
    then
        # Check kernel command-line for inhibitors, unless we are being called
        # manually
        for ARG in $(cat /proc/cmdline); do
            if [ "$ARG" = "text" ]; then
            #plymouth为开机、关机画面程序
                  plymouth quit || :
                          stop
                  exit 0
            fi
        done

        if [ -f /etc/X11/default-display-manager ]; then
            [ "$(cat /etc/X11/default-display-manager 2>/dev/null)" = "/usr/sbin/mdm" ] || { stop; exit 0; }
        else
            type lightdm >/dev/null 2>&1 && { stop; exit 0; } || true
        fi

        if [ "$RUNLEVEL" = S -o "$RUNLEVEL" = 1 ]
        then
            # Single-user mode
            plymouth quit || :
            exit 0
        fi
    fi
    //测试各种环境脚本是否存在并载入环境变量
    test -f /etc/profile && . /etc/profile

    if [ -r /etc/default/locale ]; then
        . /etc/default/locale
        export LANG LANGUAGE LC_MESSAGES LC_ALL
    elif [ -r /etc/environment ]; then
        . /etc/environment
        export LANG LANGUAGE LC_MESSAGES LC_ALL
    fi    
    
    exec mdm
end script
#以下为停止此服务后需要进行的处理
post-stop script
    if [ "$UPSTART_STOP_EVENTS" = runlevel ]; then
        #得到当前网络连接(可能存在多个),依次关闭
        netlinklist=`less /proc/net/if_inet6 | grep -v lo | grep -v vmnet | awk ‘{print $6}‘`
        echo "netlist is $netlinklist" >> /home/xxx/shutdownlog1
        for node in $netlinklist
        do
            echo "node is $node" >> /home/xxx/shutdownlog2
            /sbin/ifconfig $node down
        done
        #激活desktop-shutdown事件,会触发关机画面,注意下面的plymouth.conf配置文件中的desktop-shutdown所在位置
        initctl emit desktop-shutdown
    fi
end script


关机画面配置脚本:
# plymouth - Userspace bootsplash utility
#
# plymouth provides a boot splash screen on the system console using
# the kernel framebuffer device.  On boot, this is nominally started by
# the initramfs so the pre-start script, script and post-start script
# parts are actually not run.  These are normally run on shutdown instead.

description     "userspace bootsplash utility"

start on (starting mountall
          or (runlevel [016]
              and (desktop-shutdown
                   or stopped xdm
                   or stopped uxlaunch)))

#此任务调用fork成为一个进程进行处理
expect fork
#upstart对此服务最多等待60秒,超过则进行kill此进程
kill timeout 60

script
    if [ "$RUNLEVEL" = "0" -o "$RUNLEVEL" = "1" -o "$RUNLEVEL" = "6" ]; then
    #若是关机、重启、单用户运行状态下,执行关机画面
        exec /sbin/plymouthd --mode=shutdown
    else
    #执行开机画面
        exec /sbin/plymouthd --mode=boot --attach-to-session
    fi
end script

#当服务启动后,执行以下脚本
post-start script
    if [ "$RUNLEVEL" = "0" -o "$RUNLEVEL" = "1" -o "$RUNLEVEL" = "6" ]; then
    #执行关机动画
        exec /bin/plymouth show-splash
    fi
end script

#pre-stop exec /bin/plymouth quit
pre-stop script
     exec /bin/plymouth quit
end script


参考资料:

http://upstart.ubuntu.com/cookbook/#footer

http://upstart.ubuntu.com/