Ubuntu systemd rc.local 如何设置开机启动

转载:http://iqotom.com/?p=965

前言

Ubuntu18.04不再使用initd管理系统,改用systemd,现在已成为大多数发行版的标准配置。

以前Linux启动一直采用init进程,例如:

1
2
sudo /etc/init.d/apache2 start
service apache2 start

这种方法有两个缺点:

  1. 一是启动时间长。init进程是串行启动,只有前一个进程启动完,才会启动下一个进程
  2. 二是启动脚本复杂。init进程只是执行启动脚本,不管其他事情。脚本需要自己处理各种情况,这往往使得脚本变得很长

Systemd 就是为了解决这些问题而诞生的。它的设计目标是,为系统的启动和管理提供一套完整的解决方案。

根据 Linux 惯例,字母d是守护进程(daemon)的缩写。 Systemd 这个名字的含义,就是它要守护整个系统。

Systemd 的优点是功能强大,但是缺点也很明显,体系庞大,非常复杂。事实上,现在还有很多人反对使用 。

这里记录下,跟以前一样在/etc/rc.local设置开机启动程序,加载驱动

Setp.1

systemd默认读取/etc/systemd/system下的配置文件,该目录下的文件会链接/lib/systemd/system/下的文件。一般系统安装完/lib/systemd/system/下会有rc-local.service文件,即我们需要的配置文件。

链接过来:

1
2
3
4
cd /lib/systemd/system
ln -fs /lib/systemd/system/rc-local.service /etc/systemd/system/rc-local.service
cd /etc/systemd/system
cat rc-local.service

rc-local.service内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#  SPDX-License-Identifier: LGPL-2.1+
#
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.

# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.
[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=yes
GuessMainPID=no

[Unit] 区块:启动顺序与依赖关系。

[Service] 区块:启动行为,如何启动,启动类型。

Setp.2

  1. 创建/etc/rc.local文件

    1
    touch /etc/rc.local
  2. 赋予可执行权限

    1
    chmod 755 rc.local
  3. 编辑rc.local,添加需要开机启动的任务

    1
    2
    3
    #!/bin/bash

    echo "test rc!!!" > /var/test_rc.log
  4. reboot重启系统,查看/var目录已经成了test_rc.log

------ 本文结束------