Ubuntu 创建自定义服务 并设置开机自启

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37972348/article/details/80152424

我们经常会有这样的一种需求:将 jar 包,或者其他程序设置成开机自启,这种问题最好的实现方法就是,将相关代码写成脚本,并注册成服务,再将服务设置开机自启,接下来我们就一步步实际操作一下。如有任何错误和问题,还请大家指正和讨论。


一、 软件版本

虚拟机版本:VMware Workstation 12.5.7

Ubuntu 版本:ubuntu-14.04.5-server-i386


二、 操作步骤

1. 切换 root 账号。

$ su root

输入 root 账号的密码即可。

注:如初次安装系统,没有设置 root 账号的密码,则可如下操作。

$ sudo passwd

接下来按照提示设置密码,再切换成 root 账号即可。


2. 进入 /etc/rc.d 目录下,创建一个服务脚本,本文以“myServer”为例。

注:Ubuntu 的所有服务文件都在此目录下,文件名就是服务名,我们平时启动一个服务就会自动调用相应服务脚本的“start”方法,同理,停止和重启一个服务也会调用相应方法。

# cd /etc/rc.d
# touch myService


3. 编辑创建的服务脚本,填入以下模板,并修改。

#!/bin/bash

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

start() {
    if [ -e "$PID_PATH/$PROG.pid" ]; then
        ## Program is running, exit with error.
        echo "Error! $PROG is currently running!" 1>&2
        exit 1
    else
        ## Change from /dev/null to something like /var/log/$PROG if you want to save output.
        $PROG_PATH/$PROG $PROG_ARGS 2>&1 >/var/log/$PROG &
    $pid=`ps ax | grep -i 'myService' | sed 's/^\([0-9]\{1,\}\).*/\1/g' | head -n 1`

        echo "$PROG started"
        echo $pid > "$PID_PATH/$PROG.pid"
    fi
}

stop() {
    echo "begin stop"
    if [ -e "$PID_PATH/$PROG.pid" ]; then
        ## Program is running, so stop it
    pid=`ps ax | grep -i 'myService' | sed 's/^\([0-9]\{1,\}\).*/\1/g' | head -n 1`
    kill $pid

        rm -f  "$PID_PATH/$PROG.pid"
        echo "$PROG stopped"
    else
        ## Program is not running, exit with error.
        echo "Error! $PROG not started!" 1>&2
        exit 1
    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

一般来讲,我们只用改写其中的几个变量,start 方法和 stop 方法。

几个需要注意的地方:

a. 如果不需要服务区执行某些可执行文件,比如运行 jar 包,可以不用定义PROG相关的变量。

b. 需要服务执行的代码全部放到 start 方法中。

c. stop 方法中,如果需要终止其他进程,如关闭 mongodb,结束 Java 程序等,需要将这些语句置于最前,否则将不会执行。

原因是以下命令会直接终止该服务脚本的进程。

"pid=`ps ax | grep -i 'myServer' | sed's/^\([0-9]\{1,\}\).*/\1/g' | head -n 1`

kill $pid"

d. 关于执行某些命令所产生的输出语句重定向的详细说明,可以参考这篇文章。

linux 重定向 1>&2 2>&1


4. 启动服务。

# service myService start        //启动服务
# service myService stop         //关闭服务
# service myService restart      //重启服务


5. 将服务设置开机自启。

# update-rc.d myServicce defaults    //设置服务开机自启(默认条件)
# update-rc.d -f myService remove    //设置服务删除开机自启


6. 重启,测试。

# reboot




------------------------------------------------------------------------------------------------------------------------------------------------------------------------

参考资料:
https://blog.csdn.net/xkjcf/article/details/78698232




猜你喜欢

转载自blog.csdn.net/m0_37972348/article/details/80152424
今日推荐