# Linux开机自启动shell脚本
## 〇、Ubuntu设置
先准备要运行shell脚本。由于版本更替原因,Ubuntu18.04之后抛弃了`init.d` 的启动方式,所以Ubuntu16.04和Ubuntu18.04的开机自启动shell脚本是不一样的。
脚本2
```bash
#!/bin/bash
# start MKDocs
cd mkdocs-server/
mkdocs serve -a 172.16.33.106:8686
exit 0
```
**Ubuntu16.04方法**
```bash
#先写一个bash脚本
=====================================
#!/bin/bash
# start code-server
code-server --bind-addr 172.16.33.106:8484 \
--password wyd19940118 \
--config ~/.config/code-server/config.yaml
exit 0
=====================================
#给脚本文件增加执行权限
#方式一
sudo chmod +755 code-server.sh
#方式二
sudo chmod +x code-server.sh
#移动脚本并指定启动优先级
sudo mv code-server.sh /etc/init.d/
cd /etc/init.d/
sudo update-rc.d code-server.sh defaults 90
#移除开机自启动
sudo update-rc.d -f code-server.sh remove
```
**Ubuntu18.04方法**
```bash
1. 复制命令(设置启动参数) vim /lib/systemd/system/rc.local.service
======================================================================
[Unit]
Description=/etc/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=no
GuessMainPID=no
#这一段原文件没有,需要自己添加
[Install]
WantedBy=multi-user.target
Alias=rc-local.service
=======================================================================
2. 复制命令(设置软连接,开机启动会去/etc/……这个目录下去找文件)
sudo ln -s /lib/systemd/system/rc.local.service /etc/systemd/system/rc.local.service
3. 复制命令(本身是没有rc.local文件的,后来上帝说要有它,就有了)
sudo vim /etc/rc.local
#添加执行权限
sudo chmod +x /etc/rc.local
#要开机启动的脚本、服务或者其他的操作都把命令写入这个脚本
===============================================
#!/bin/bash
echo "hello" > /etc/test.log
/etc/init.d/webserver start
exit 0
===============================================
4.启动服务
sudo systemctl enable rc-local
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service
5.重启测试
```
## 一、CentOS系统和Redhat系统
**将shell脚本加入rc.local中**
1、修改 `/etc/rc.d/rc.local`
Redhat中的运行模式2、3、5都把`/etc/rc.d/rc.local`做为初始化脚本中的最后一个,所以用户可以自己在这个文件中添加一些需要在其他初始化工作之后,登录之前执行的命令。
```
sudo vim /etc/rc.d/rc.local
=======================================================================
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
source /root/Desktop/start.sh #其实就是添加这一句, 意思是运行start.sh该脚本
========================================================================
```
2、为/etc/rc.d/rc.local赋予执行权限
```bash
sudo chmod 777 /etc/rc.d/rc.local
```
3、重启系统就可以按预设定的执行脚本程序,此种自启动方式不会在GUI界面显示执行的具体进程。
**使用chkconfig设置**
1、先看一下允许的服务列表
```bash
chkconfig --list
```
2、增加服务
```bash
chkconfig --add httpd
```
3、再次查看
```bash
chkconfig --list
```
4、删除服务
```bash
chkconfig --del httpd
```
## 二、Ubuntu系统和Debian系统
1、建立 `rc-local.service` 文件
```bash
sudo vim /etc/systemd/system/rc-local.service
```
2、编辑服务文件 `/etc/systemd/system/rc-local.service`
```
[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
[Install]
WantedBy=multi-user.target
```
3、创建文件 `rc.local`
```bash
sudo vim /etc/rc.local
```
4、编辑 `rc.local`
```bash
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
sudo ./root/Desktop/start.sh
```
5、赋予 `rc.local` 执行的权限
```bash
sudo chmod +x /etc/rc.local
```
6、设置服务开机自启动,并启动服务
```bash
# 设置开机自启动
sudo systemctl enable rc-local
# 启动服务
sudo systemctl start rc-local.service
# 检查状态
sudo systemctl status rc-local.service
```
## 三、服务管理器配置
systemd service是一种以 .service 结尾的配置文件,是一个专用于 Linux 操作系统的系统与服务管理器。简单来说,用于后台以守护精灵(daemon)的形式运行程序。systemd 服务的内容主要分为三个部分,控制单元 [Unit] 的定义、服务 [Service] 的定义、以及 [Install] 安装部分。放在用户目录 **/usr/lib/systemd/system (Centos和Ubuntu)** 或者 **/etc/systemd/system (Ubuntu)**。
```bash
[Unit]
Description=httpd #当前配置文件的描述信息
After=network.target #表示当前服务是在那个服务后面启动,一般定义为网络服务启动后启动
[Service]
Type=forking #定义启动类型
ExecStart=/usr/local/apache/bin/apachectl start #定义启动进程时执行的命令。
ExecReload=/usr/local/apache/bin/apachectl restart #重启服务时执行的命令
ExecStop=/usr/local/apache/bin/apachectl stop #定义关闭进程时执行的命令。
PrivateTmp=true #是否分配独立空间
[Install]
WantedBy=multi-user.target #表示多用户命令行状态
```
Unit 字段:这个字段主要给出服务描述、启动顺序和依赖关系。
```bash
Description字段给出当前服务的简单描述。
Documentation字段给出文档位置。
After字段表示在什么服务之后启动。
Before字段表示在什么服务之前启动。
After和Before字段只涉及启动顺序,不涉及依赖关系。
Wants字段表示该服务和某服务存在某种弱依赖关系,即某服务停止运行或退出不影响该服务继续运行。
Requires字段则表示”强依赖”关系,即某服务停止运行或退出,改服务也必须停止运行。
Wants字段与Requires字段只涉及依赖关系,与启动顺序无关,默认情况下是同时启动的。
```
Server 字段:这个字段主要给出服务的启动行为,如何启动、重启、停止。
```bash
Type字段:定义启动类型。它可以设置的值如下:
- simple(默认值):ExecStart字段启动的进程为主进程
- forking:ExecStart字段将以fork()方式启动,此时父进程将会退出,子进程将成为主进程
- oneshot:类似于simple,但只执行一次,Systemd 会等它执行完,才启动其他服务
- dbus:类似于simple,但会等待 D-Bus 信号后启动
- notify:类似于simple,启动结束后会发出通知信号,然后 Systemd 再启动其他服务
- idle:类似于simple,但是要等到其他任务都执行完,才会启动该服务。一种使用场合是为让该服务的输出,不与其他服务的输出相混
ExecStart字段:定义启动进程时执行的命令,就是手动启动时执行的命令。
ExecReload字段:重启服务时执行的命令。
ExecStop字段:停止服务时执行的命令。
ExecStartPre字段:启动服务之前执行的命令。
ExecStartPost字段:启动服务之后执行的命令。
ExecStopPost字段:停止服务之后执行的命令。
KillMode字段:定义 Systemd 如何停止 sshd 服务。它可以设置的值如下:
- control-group(默认值):当前控制组里面的所有子进程,都会被杀掉
- process:只杀主进程
- mixed:主进程将收到 SIGTERM 信号,子进程收到 SIGKILL 信号
- none:没有进程会被杀掉,只是执行服务的 stop 命令
Restart字段:定义了 sshd 退出后,Systemd 的重启方式。它可以设置的值如下:
- no(默认值):退出后不会重启
- on-success:只有正常退出时(退出状态码为0),才会重启
- on-failure:非正常退出时(退出状态码非0),包括被信号终止和超时,才会重启
- on-abnormal:只有被信号终止和超时,才会重启
- on-abort:只有在收到没有捕捉到的信号终止时,才会重启
- on-watchdog:超时退出,才会重启
- always:不管是什么退出原因,总是重启
#对于守护进程,推荐设为on-failure。对于那些允许发生错误退出的服务,可以设为on-abnormal。
RestartSec字段:表示 Systemd 重启服务之前,需要等待的秒数。
User字段可以设置服务的用户名 #一般User=%i
WorkingDirectory字段指定服务的安装目录
```
Install 字段:该字段定义如何安装这个配置文件,即怎样做到开机自启。
```bash
WantedBy字段:表示该服务所在的 Target。
Target的含义是服务组,表示一组服务。WantedBy=multi-user.target指的是服务所在的Target是multi-user.target
Systemd 有默认的启动 Target。就是multi-user.target,在这个组里的所有服务,都将开机启动。
查看 multi-user.target 包含的所有服务
systemctl list-dependencies multi-user.target
```
写好之后记得刷新后台守护进程。
```bash
#刷新后台守护进程
sudo systemctl daemon-reload
```