Linux 设置服务开机自动启动配置

1、把要开机启动的程序配置成服务

 下面贴一个例子

#!/bin/bash

# chkconfig: 2345 80 05
# description: Node Exporter Service

RETVAL=0
PROG="node_exporter"
EXEC="/home/prometheus_exporters/node_exporter-0.18.1.linux-amd64/node_exporter"
LOCKFILE="/var/lock/subsys/$PROG"
OPTIONS="--web.listen-address=:9100"

# Source function library.
if [ -f /etc/rc.d/init.d/functions ]; then
  . /etc/rc.d/init.d/functions
else
  echo "/etc/rc.d/init.d/functions does not exist"
  exit 0
fi

start() {
  if [ -f $LOCKFILE ]
  then
    echo "$PROG is already running!"
  else
    echo -n "Starting $PROG: "
    nohup $EXEC $OPTIONS >/dev/null 2>&1 &
    RETVAL=$?
    [ $RETVAL -eq 0 ] && touch $LOCKFILE && success || failure
    echo
    return $RETVAL
  fi
}

stop() {
  echo -n "Stopping $PROG: "
  killproc $EXEC
  RETVAL=$?
  [ $RETVAL -eq 0 ] && rm -r $LOCKFILE && success || failure
  echo
}

restart ()
{
  stop
  sleep 1
  start
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status $PROG
    ;;
  restart)
    restart
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1
esac
exit $RETVAL

2、在服务脚本中增加chkconfig 配置

以/etc/init.d/node_exporter.sh为例:

# chkconfig: 2345 80 05
# description: Node Exporter Service

chkconfig 配置分为三段,分别为 启动级别 启动优先级 停止优先级

2345是默认启动级别,级别有0-6共7个级别:

  • 等级0表示:关机   

  • 等级1表示:单用户模式   

  • 等级2表示:无网络连接的多用户命令行模式   

  • 等级3表示:有网络连接的多用户命令行模式   

  • 等级4表示:不可用   

  • 等级5表示:带图形界面的多用户模式   

  • 等级6表示:重新启动

启动优先级,范围为0~100,数字越大优先级越低。本例中为80

停止优先级,范围为0~100,数字越大优先级越低。本例中为05

3、开启开机启动

增加上默认为开启状态

 # chkconfig --add node_exporter

可以查看开机自动启动列表

# chkconfig --list

服务开机自启打开与关闭

# chkconfig node_exporter on
# chkconfig node_exporter off
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值