debain系统code-server云IDE开机启动脚本

code-server开机启动脚本

参考debain11安装code-server以非root用户开机启动
本文是一个code-server开机启动脚本,拿来即用。可以在外面使用云端IDE工具。

code-server开机启动脚本

#!/bin/bash 

#创建日期:2022-11-15
#脚本说明:管理code-server

#获得系统时间命令
DATE=`date "+%F  %H:%M:%S"`
log_path=/home/zzyy/logs
log_codeserver=/home/zzyy/logs/code-server.log
command_server="nohup /usr/bin/code-server --host 0.0.0.0 --port 8080 --auth password"
MY_PROGRAM='code-server'

init_dir(){
    
    
    if [ ! -d ${log_path} ];then
        mkdir ${log_path}
    else
        echo "${log_path}文件夹已经存在"
    fi
}

start_codeServer(){
    
    
    res=`ps -ef | grep code-server | grep -v grep | grep -v tail | awk '{print $2}' | sed -n '1p'`
    if [ -n "$res" ]
    then
        echo "$MY_PROGRAM already running"
    else
        export PASSWORD="111"
        su zzyy -c "${command_server} > ${log_codeserver} 2>&1 &"
        sleep 3s
        echo "waiting ... ... ... "
        sleep 3s
        unset res
        res=`ps -ef | grep code-server | grep -v grep | grep -v tail | awk '{print $2}' | sed -n '1p'`
        if [ -n "$res" ]
        then
            echo "$MY_PROGRAM start success"
        else
            echo "$MY_PROGRAM start error"
        fi
    fi

}


start(){
    
    
    start_codeServer
}

stop_codeserver(){
    
    
    # 停止code-server
    #ps -ef | grep code-server | grep -v grep | grep -v tail | awk '{print $2}' | sed -n '1p' | xargs kill -9
    # awk获取第二行, sed获取第一列
    proc_id=`ps -ef | grep code-server | grep -v grep | grep -v tail | awk '{print $2}' | sed -n '1p'`
    if [ -n "$proc_id" ]
    then
        echo "$MY_PROGRAM is start,now kill..."
        kill -9 $proc_id
        echo "$MY_PROGRAM pid=$proc_id kill ok!"
    else
        echo "$MY_PROGRAM is not start!"
    fi
}

stop(){
    
    
    status
    stop_codeserver
}

status(){
    
    
    echo "查看code-server在线IDE工具状态"
    ps -ef | grep code-server | grep -v grep | grep -v tail
}

clear_log(){
    
    
    # Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值,字符和文件三个方面的测试
    # -e 文件名	如果文件存在则为真
    if [ ! -e ${log_codeserver} ]
    then
        echo "${log_codeserver} 未找到!"
        exit 10
    else
        echo "${log_codeserver} 找到,开始删除!"
        rm -rf ${log_codeserver}
        echo -n "${log_codeserver} 删除成功!"
    fi
}

case $1 in 
   start)  # 服务启动需要做的步骤
           start
           ;;
   stop)  # 服务停止需要做的步骤
           stop
           ;;
   restart) # 重启服务需要做的步骤
            stop
            sleep 3s
            start
            ;;
   status) # 查看状态需要做的步骤
             status
             ;;
   clear) # 删除日志
             clear_log
             ;;                         
   *) echo "$0 {start|stop|restart|status|clear}"
             exit 4
             ;;
esac

npc脚本

#!/bin/bash 

#创建日期:2022-11-15
#脚本说明:管理npc远程内网穿透

#获得系统时间命令
DATE=`date "+%F  %H:%M:%S"`
log_path=/home/zzyy/logs
log_npc=/home/zzyy/logs/npc.log
FREE_NAME='/home/zzyy/soft/npc/npc -server=free.svipss.top:8024 -vkey=111'
VIP_NAME='/home/zzyy/soft/npc/npc -server=qqqq.vipnps.vip:8024 -vkey=111'
MY_PROGRAM="free.svipss.top"

start_npc(){
    
    
    # ps -ef | grep 'free.svipss.top' | grep -v grep | grep -v tail | awk '{print $2}'
    # 注意 , 变量名和等号之间不能有空格
    res=`ps -ef | grep $MY_PROGRAM | grep -v grep | grep -v tail | awk '{print $2}'`
    if [ -n "$res" ]
    then
        echo "$MY_PROGRAM already running"
    else
        # https端口
        su zzyy -c "nohup ${FREE_NAME} > ${log_npc} 2>&1 &"
        # tcp端口
        #su zzyy -c "nohup ${VIP_NAME} > ${log_npc} 2>&1 &"
        sleep 2s
        echo "waiting ... ... ... "
        sleep 3s
        unset res
        res=`ps -ef | grep "$MY_PROGRAM" | grep -v grep | grep -v tail | awk '{print $2}'`
        if [ -n "$res" ]
        then
            echo "$MY_PROGRAM start success"
        else
            echo "$MY_PROGRAM start error"
        fi
    fi
} 


start(){
    
    
    start_npc
}

stop_npc(){
    
    
    proc_id=`ps -ef | grep $MY_PROGRAM | grep -v grep | grep -v tail | awk '{print $2}'`
    if [ -n "$proc_id" ]
    then
        echo "$MY_PROGRAM is start,now kill..."
        kill -9 $proc_id
        echo "$MY_PROGRAM pid=$proc_id kill ok!"
    else
        echo "$MY_PROGRAM is not start!"
    fi
}

stop(){
    
    
    status
    sleep 1s
    stop_npc
}

status(){
    
    
    echo "查看npc内网穿透状态"
    # ps -ef | grep 'free.svipss.top' | grep -v grep | grep -v tail
    ps -ef | grep $MY_PROGRAM | grep -v grep | grep -v tail
}

clear_log(){
    
    
    if [ ! -e ${log_npc} ]
    then
        echo -n "${log_npc} 找到,开始删除!"
        exit 10
    else
        echo -n "${log_npc} 找到,开始删除!"
        rm -rf ${log_npc}
        echo -n "${log_npc} 删除成功!"
    fi
    echo "--- --- ---"
}

case $1 in 
   start)  # 服务启动需要做的步骤
           start
           ;;
   stop)  # 服务停止需要做的步骤
           stop
           ;;
   restart) # 重启服务需要做的步骤
            stop
            sleep 3s
            start
            ;;
   status) # 查看状态需要做的步骤
             status
             ;;
   clear) # 删除日志
             clear_log
             ;;             
   *) echo "$0 {start|stop|restart|status|clear}"
             exit 4
             ;;
esac
exit

参考资料

Ubuntu 环境下配置 Nginx 开机自启
ubuntu18.04脚本开机自启动 root用户和非root用户
debain11安装code-server以非root用户开机启动
如何增加一个系统服务service

猜你喜欢

转载自blog.csdn.net/e891377/article/details/127877654
今日推荐