Linux self-starting method 1: self-starting service servcie

background

       Slag laptop, as a Linux server. Install oracle 11g into docker (about 11g dockerized installation will be described in other articles) in order to learn to obtain OCP certificate. However, every time after booting, you need to switch su to root to start the container, which is very cumbersome. So think about the boot option of Linux. So I got three solutions:

  1. Self-starting service service
  2. Crontab---timed task
  3. /etc/rc.local

This article describes the first scheme, self-starting service.

surroundings

jwllinux@jwlLinux ~ $ uname -a

Linux jwlLinux 4.4.0-21-generic #37-Ubuntu SMP Mon Apr 18 18:34:49 UTC 2016 i686 i686 i686 GNU/Linux

 

jwllinux@jwlLinux ~ $ hostnamectl

   Static hostname: jwlLinux

         Icon name: computer-laptop

           Chassis: laptop

        Machine ID: 30e8671244344b49af2510a06808f1b0

           Boot ID: 32a4ae15a47e4694908e5f18faaa2c56

  Operating System: Ubuntu 16.04 LTS

            Kernel: Linux 4.4.0-21-generic

      Architecture: x86

jwllinux@jwlLinux ~ $ lsb_release  -d

Description:    Linux Mint 18 Sarah

X86 architecture, Linux Mint 18

step

1. Create startup scripts for related programs under /etc/init.d

vim /etc/init.d/testservcie

chmod 755 testservcie

jwllinux @ jwlLinux ~ $ cat /etc/init.d/testservcie

#!/bin/sh

### BEGIN INIT INFO

# Provides:         testservice

# Required-Start:

# Required-Stop:

# Default-Start:     2 3 4 5

# Default-Stop:      0 1 6

# Short-Description: test for service

# Description:       test for service

### END INIT INFO

 

echo "1"

exit 0

Note: The script under /etc/init.d/ is the init startup item of the debian series Linux system . The script writing needs to follow a certain style ( LSB style), you can check /etc/init.d/README for details . Such as:

Provides=== What services are provided;

Required-Start=== Dependencies for service startup

Default-Start===The default startup level ( 0~6 ).

  0级:关闭系统(千万不要把initdefault设置为0,否则将开不了机)

  1级:单用户模式

  2级:没有网络多用户模式

  3级:有网络多用户模式

  4级:系统保留

  5级:有网络和图形的多用户模式 

  6级:重启系统(千万不要把initdefault设置为6,否则电脑将一直开机重启)

……

2. 注册服务

systemctl enable testservcie

update-rc.d testservcie defaults

可能的报错:(此服务已经被其他脚本提供)

此命令会解析脚本头,在相应层级的启动目录下创建指向/etc/init.d/XXX的符号链接,如下:

3. 查询注册的服务

4. 查看服务状态

 

5. service 服务名 start 手动启动服务

6. service 服务名 stop 手动关闭服务

其他

       如上步骤主要是使用systemctl命令注册了服务,并用service命令对服务进行操作。Service命令还有许多相关参数,如reloadrestart等,分别对应启动脚本不同的处理逻辑。本文只是简单介绍服务的注册,有关启动脚本的书写,可参阅存量脚本。

补充

对于Redhat/ centos系列的Linux系统,服务的注册可能是chkconfig命令,读者可以需要根据自己OS的适配。

 

Guess you like

Origin blog.csdn.net/zhaogang1993/article/details/105104375