#!/usr/bin/env bash
logger  -p local0.info "`who -m | cut -d\( -f2 |cut -d\) -f1`  ${BASH_SOURCE}; start;successfully execute ${BASH_SOURCE}"

reboot_env()
{
    if [ -f "/opt/SEK/reboot.flag" ];then
        rm -rf "/opt/SEK/reboot.flag"
    fi
    #生成系统启动项脚本
    echo "#!/bin/bash
    logger -p local0.info \"reboot_sek.sh;Successful;Start reboot_sek.sh successful\"
    echo \"reboot  successfully.\" > /opt/SEK/reboot.flag
    sed -i 's/^.*reboot_sek.*$//g' /etc/rc.d/rc.local
    logger -p local0.info \"reboot_sek.sh;Successful;reboot_sek.sh execute successful.\"
    " >/opt/SEK/reboot_sek.sh
    chmod 700 /opt/SEK/reboot_sek.sh

    #设置系统启动项
    grep 'reboot_sek.sh' /etc/rc.d/rc.local >/dev/null 2>&1
    [ $? -ne 0 ] && echo "bash /opt/SEK/reboot_sek.sh" >> /etc/rc.d/rc.local
    chmod +x  /etc/rc.d/rc.local

    service sshd stop
    sleep 60
    sync;sync;sync;sync

    #重启操作系统
    echo "Reboot the OS"
    shutdown -r now
}

reboot_env