linux service服务自作开机自启动脚本

 1、安装文件服务器

进入/root/httpd 下执行相关指令:
启动: ./CloudDiskWeb config.json 或 ./CloudDiskWeb -c config.json

 2.自作启动脚本

#!/bin/sh

#启动脚本 !/usr/bin/env bash       !/bin/sh
ROOT_DIR='/root/httpd'
APP_PATH=${ROOT_DIR}/CloudDiskWeb
PID_PATH=${ROOT_DIR}/service.pid
LOG_PATH=${ROOT_DIR}/log
CONFIG=${ROOT_DIR}/config.json
#使用说明,用来提示输入参数
usage() {
  echo "Usage: sh 执行脚本.sh [start|stop|restart|status] [dev]|[test]|[prod]"
  exit 1
}
#检查程序是否在运行
is_exist() {
  pid=$(ps -ef | grep $APP_PATH | grep -v grep | awk '{print $2}')
  #如果不存在返回1,存在返回0
  if [ -z "${pid}" ]; then
    return 1
  else
    return 0
  fi
}

#启动方法
start() {
  if [ ! -d "${LOG_PATH}" ]; then
    mkdir ${LOG_PATH}
  fi
  if [ -f "$PID_PATH" ]; then
    echo "Service is already start ..."
  else
    echo "Service  start ..."
    #nohup ${APP_PATH} -c ${CONFIG} >${LOG_PATH}/log.out 2>&1 &
    nohup ${APP_PATH} ${CONFIG} >${LOG_PATH}/log.out 2>&1 &
    printf '%d' $! >$PID_PATH
    echo "Service  start SUCCESS "
  fi
}

#停止方法
stop() {
  if [ -f "$PID_PATH" ]; then
    kill -9 $(cat $PID_PATH)
    rm -rf $PID_PATH
    echo "Service is stop SUCCESS!"
  else
    pid=$(ps -ef | grep -i ${APP_PATH} | grep -v grep | awk -F" " '{print $2}')
    if [ ! -z $pid ]; then
      echo "Service is already stop ..."
    else
      kill -9 $pid
      echo "Service is stop SUCCESS!"
    fi
  fi
}

#输出运行状态
status() {
  if [ -f "$PID_PATH" ]; then
    pid=$(cat "$PID_PATH")
    pid=$(ps -ef | grep $pid | grep -v "grep" | awk -F" " '{print $2}')
    if [ -z "$pid" ]; then
      echo "${APP_PATH} is NOT running."
    else
      echo "${APP_PATH} is running. Pid is ${pid}"
    fi
  else
    is_exist
    if [ $? -eq "0" ]; then
      echo "${APP_PATH} is NOT running."
    else
      echo "${APP_PATH} is running. Pid is ${pid}"
    fi
  fi
}

#重启
restart() {
  stop
  start
}

if [ $2 ]; then
  CONFIG=$2
fi

#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
"start")
  start
  ;;
"stop")
  stop
  ;;
"status")
  status
  ;;
"restart")
  restart
  ;;
*)
  usage
  ;;
esac

 补充:启动、重启、停止

./run.sh  [start|stop|restart|status]

3.自作系统级别开机自启动脚本

创建启动脚本

vi /usr/lib/systemd/system/CloudDiskWeb.service

内容如下:

[Unit]
Description=CloudDiskWeb.linux-amd64
After=network.target

[Service]
Type=forking

Environment=HOME=/root/httpd
ExecStart=/root/httpd/run.sh start
ExecReload=/root/httpd/run.sh restart
ExecStop=/root/httpd/run.sh stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

 

4.启动、重启、停止

#启动该服务
systemctl start CloudDiskWeb.service

#查看该服务状态
systemctl status CloudDiskWeb.service

#重启服务
systemctl restart CloudDiskWeb.service

#停止服务状态
systemctl stop CloudDiskWeb.service
 

5.设置开机自启动

进行启动停止服务,执行如下命令

 
#重新加载所有service服务
systemctl daemon-reload

#开机启动CloudDiskWeb.service
systemctl enable CloudDiskWeb.service

#查看该service是否开机启用
systemctl is-enabled CloudDiskWeb.service

#查询开机启动项服务
systemctl list-unit-files 

#过滤启动项
systemctl list-unit-files | grep enable
#过滤未启动项
systemctl list-unit-files | grep disabled

#过滤某个服务
systemctl list-unit-files | grep CloudDiskWeb

posted on 2023-07-07 15:21  uestc2007  阅读(571)  评论(0编辑  收藏  举报

导航