Linux服务编写及服务开机启动

274 阅读2分钟

这是我参与2022首次更文挑战的第24天,活动详情查看:2022首次更文挑战

简介

在某些情况下,需要将自己在服务器上面的一个服务用linux Service之类的命令进行启动,并且能够开机启动,本篇文章中就会介绍如何linux下新建自己的服务

一、服务编写

编写服务脚本

    首先用编辑器自己写一个脚本文件,模板和大致的内容如下(简单的按照下面的进行操作即可):比如写 /etc/init.d/test

#!/bin/bash
### BEGIN INIT INFO
#
# chkconfig:   2345 90 10
# Provides:  location_server
# Required-Start:   $local_fs  $remote_fs
# Required-Stop:    $local_fs  $remote_fs
# Default-Start:    2 3 4 5
# Default-Stop:     0 1 6
# Short-Description:    initscript
# Description:  This file should be used to construct scripts to be placed in /etc/init.d.
#
### END INIT INFO

## Fill in name of program here.
PROG="location_server"
PROG_PATH="/opt/location_server" ## Not need, but sometimes helpful (if $PROG resides in /opt for example).
PROG_ARGS="" 
PID_PATH="/var/run/"

# 上面的保持原样就行,需要写的start和stop函数
start() {
    echo "current PID: $$" #这句删除也可以
    bin/kibana  > kibana.log 2>&1 &
    echo "$!"
    echo "$!" >  /var/run/kibana.pid #将上一个后台进程写入到标准的pid文件和路径中
}

stop() {
    if [ -f  "/var/run/kibana.pid" ]; then
        echo "Will kill kibana"
        kill -9 `cat /var/run/kibana.pid`
    else
        echo "Programe don't run"
    fi
}

# 下面这些也用改
## Check to see if we are running as root first.
## Found at http://www.cyberciti.biz/tips/shell-root-user-check-script.html
if [ "$(id -u)" != "0" ]; then
    echo "This script must be run as root" 1>&2
    exit 1
fi

case "$1" in
    start)
        start
        exit 0
    ;;
    stop)
        stop
        exit 0
    ;;
    reload|restart|force-reload)
        stop
        start
        exit 0
    ;;
    **)
        echo "Usage: $0 {start|stop|reload}" 1>&2
        exit 1
    ;;
esac

添加服务

    先赋予文件执行权限后使用命令将上面写的test添加到服务里面

chmod +x /etc/init.d/test

# Ubuntu,Kali下的操作
# update-rc.d 服务名 defaults,并且这样设置服务也就开机启动了
update-rc.d test defaults
# 删除
# update-rc.d -f 服务名 remove

# CtenOS下的操作
chkconfig --add agent_server
chkconfig agent_server on

启动、关闭、重启服务

    接下来就可以使用service命令去启动它了

# service 方式
# service 服务名 start
# service 服务名 stop
# service 服务名 start
service test start

# systemctl  方式
systemctl start service
systemctl stop service
systemctl restart service
systemctl status service

编写rc.local文件

chmod +x /etc/rc.d/rc.local
vi /etc/rc.d/rc.local

参考链接