ubuntu18.04 不再使用initd管理系统,改用systemd.

然而systemd很难用,改变太大,跟之前的完全不同。

使用systemd设置开机启动
为了像以前一样,在/etc/rc.local中设置开机启动程序,需要以下几步:

1、实现原理

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

/lib/systemd/system/:

可以看到 target 的定义:

查看target: ll | grep target

-rw-r--r--  1 root root   919 1月  28  2018 basic.target
drwxr-xr-x 2 root root 4096 7月 25 11:05 basic.target.wants/
-rw-r--r-- 1 root root 419 1月 28 2018 bluetooth.target
-rw-r--r-- 1 root root 465 1月 28 2018 cryptsetup-pre.target
-rw-r--r-- 1 root root 412 1月 28 2018 cryptsetup.target
lrwxrwxrwx 1 root root 13 10月 6 14:16 ctrl-alt-del.target -> reboot.target
lrwxrwxrwx 1 root root 16 10月 6 14:16 default.target -> graphical.target
-rw-r--r-- 1 root root 471 1月 28 2018 emergency.target
-rw-r--r-- 1 root root 541 1月 28 2018 exit.target
-rw-r--r-- 1 root root 480 1月 28 2018 final.target
-rw-r--r-- 1 root root 506 1月 28 2018 getty-pre.target
-rw-r--r-- 1 root root 500 1月 28 2018 getty.target
drwxr-xr-x 2 root root 4096 7月 25 11:04 getty.target.wants/
-rw-r--r-- 1 root root 598 1月 28 2018 graphical.target
drwxr-xr-x 2 root root 4096 7月 25 11:04 graphical.target.wants/
-rw-r--r-- 1 root root 527 1月 28 2018 halt.target
drwxr-xr-x 2 root root 4096 7月 25 11:05 halt.target.wants/
-rw-r--r-- 1 root root 509 1月 28 2018 hibernate.target
-rw-r--r-- 1 root root 530 1月 28 2018 hybrid-sleep.target
-rw-r--r-- 1 root root 593 1月 28 2018 initrd-fs.target
-rw-r--r-- 1 root root 561 1月 28 2018 initrd-root-device.target
-rw-r--r-- 1 root root 566 1月 28 2018 initrd-root-fs.target
-rw-r--r-- 1 root root 754 1月 28 2018 initrd-switch-root.target
drwxr-xr-x 2 root root 4096 7月 25 11:05 initrd-switch-root.target.wants/
-rw-r--r-- 1 root root 763 1月 28 2018 initrd.target
-rw-r--r-- 1 root root 541 1月 28 2018 kexec.target
drwxr-xr-x 2 root root 4096 7月 25 11:05 kexec.target.wants/
-rw-r--r-- 1 root root 435 1月 28 2018 local-fs-pre.target
-rw-r--r-- 1 root root 547 1月 28 2018 local-fs.target
drwxr-xr-x 2 root root 4096 7月 25 11:04 local-fs.target.wants/
-rw-r--r-- 1 root root 532 1月 28 2018 multi-user.target
drwxr-xr-x 2 root root 4096 7月 25 11:05 multi-user.target.wants/
-rw-r--r-- 1 root root 505 1月 28 2018 network-online.target
-rw-r--r-- 1 root root 502 1月 28 2018 network-pre.target
-rw-r--r-- 1 root root 521 1月 28 2018 network.target
-rw-r--r-- 1 root root 554 1月 28 2018 nss-lookup.target
-rw-r--r-- 1 root root 513 1月 28 2018 nss-user-lookup.target
-rw-r--r-- 1 root root 394 1月 28 2018 paths.target
-rw-r--r-- 1 root root 592 1月 28 2018 poweroff.target
drwxr-xr-x 2 root root 4096 7月 25 11:05 poweroff.target.wants/
-rw-r--r-- 1 root root 417 1月 28 2018 printer.target
-rw-r--r-- 1 root root 583 1月 28 2018 reboot.target
drwxr-xr-x 2 root root 4096 7月 25 11:05 reboot.target.wants/
-rw-r--r-- 1 root root 549 1月 28 2018 remote-cryptsetup.target
-rw-r--r-- 1 root root 436 1月 28 2018 remote-fs-pre.target
-rw-r--r-- 1 root root 522 1月 28 2018 remote-fs.target
-rw-r--r-- 1 root root 492 1月 28 2018 rescue.target
drwxr-xr-x 2 root root 4096 7月 25 11:04 rescue.target.wants/
-rw-r--r-- 1 root root 540 1月 28 2018 rpcbind.target
lrwxrwxrwx 1 root root 15 10月 6 14:16 runlevel0.target -> poweroff.target
lrwxrwxrwx 1 root root 13 10月 6 14:16 runlevel1.target -> rescue.target
drwxr-xr-x 2 root root 4096 4月 21 00:55 runlevel1.target.wants/
lrwxrwxrwx 1 root root 17 10月 6 14:16 runlevel2.target -> multi-user.target
drwxr-xr-x 2 root root 4096 4月 21 00:55 runlevel2.target.wants/
lrwxrwxrwx 1 root root 17 10月 6 14:16 runlevel3.target -> multi-user.target
drwxr-xr-x 2 root root 4096 4月 21 00:55 runlevel3.target.wants/
lrwxrwxrwx 1 root root 17 10月 6 14:16 runlevel4.target -> multi-user.target
drwxr-xr-x 2 root root 4096 4月 21 00:55 runlevel4.target.wants/
lrwxrwxrwx 1 root root 16 10月 6 14:16 runlevel5.target -> graphical.target
drwxr-xr-x 2 root root 4096 4月 21 00:55 runlevel5.target.wants/
lrwxrwxrwx 1 root root 13 10月 6 14:16 runlevel6.target -> reboot.target
-rw-r--r-- 1 root root 442 1月 28 2018 shutdown.target
-rw-r--r-- 1 root root 402 1月 28 2018 sigpwr.target
-rw-r--r-- 1 root root 460 1月 28 2018 sleep.target
-rw-r--r-- 1 root root 449 1月 28 2018 slices.target
-rw-r--r-- 1 root root 420 1月 28 2018 smartcard.target
-rw-r--r-- 1 root root 396 1月 28 2018 sockets.target
drwxr-xr-x 2 root root 4096 7月 25 11:04 sockets.target.wants/
-rw-r--r-- 1 root root 420 1月 28 2018 sound.target
-rw-r--r-- 1 root root 49 12月 15 2017 spice-vdagentd.target
-rw-r--r-- 1 root root 503 1月 28 2018 suspend.target
-rw-r--r-- 1 root root 554 7月 21 01:13 suspend-then-hibernate.target
-rw-r--r-- 1 root root 393 1月 28 2018 swap.target
-rw-r--r-- 1 root root 558 1月 28 2018 sysinit.target
drwxr-xr-x 2 root root 4096 7月 25 11:05 sysinit.target.wants/
-rw-r--r-- 1 root root 592 1月 28 2018 system-update.target
drwxr-xr-x 2 root root 4096 7月 25 11:06 system-update.target.wants/
-rw-r--r-- 1 root root 445 1月 28 2018 timers.target
drwxr-xr-x 2 root root 4096 7月 25 11:04 timers.target.wants/
-rw-r--r-- 1 root root 435 1月 28 2018 time-sync.target
-rw-r--r-- 1 root root 457 1月 28 2018 umount.target

target 用于指定 什么时候启动 我们自己自定的软件。

1)将 /lib/systemd/system/rc-local.service 链接到 /etc/systemd/system/ 目录下面来:

ln -fs /lib/systemd/system/rc-local.service /etc/systemd/system/rc-local.service  

查看文件内容:

cd /etc/systemd/system/
cat rc-local.service

说明:

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

ConditionFileIsExecutable=/etc/rc.local
After=network.target

ConditionFileIsExecutable指定了执行的文件,

After 表示在 network.target 这个target后面进行执行。也就是网络启动完成之后,执行 /etc/rc.local 文件。

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

Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no

3) [Install] 区块,定义如何安装这个配置文件,即怎样做到开机启动。

[Install]
WantedBy=multi-user.target
Alias=rc-local.service

[Install] 块是我们自己编辑新增的。

2、创建/etc/rc.local文件

touch /etc/rc.local

3、赋可执行权限

chmod 755 /etc/rc.local

4、编辑rc.local,添加需要开机启动的任务  

#!/bin/bash

echo "test rc " > /var/test.log

# startup redis-server:
/usr/local/bin/redis-server /usr/redis/redis.conf # startup mysqld
/usr/local/mysql/bin/mysqld_safe --user=mysql & # startup tomcat
/usr/tomcat/apache-tomcat-7.0.90/bin/startup.sh
/usr/tomcat/apache-tomcat-7.0.90-02/bin/startup.sh

这里启动时自动启动: redis 、 mysqld、tomcat等。很粗暴....

5、执行reboot重启系统,然后查看test.log  

6、总结

其实就是利用 systemd 的启动原理, 通过 /etc/systemd/system/rc-local.service 文件来达到启动时执行 /etc/rc.local 文件的目的。

参考:https://www.centos.bz/2018/05/ubuntu-18-04-rc-local-systemd%E8%AE%BE%E7%BD%AE/

Ubuntu 18.04 启用 rc.local 设置开机启动的更多相关文章

  1. ubuntu 18.04 配置 rc.local

    ubuntu 18.04 配置 rc.local:https://blog.csdn.net/a912952381/article/details/81205095 Ubuntu /etc/rc.lo ...

  2. 转载:如何在Ubuntu 18.04上使用UFW设置防火墙

    https://blog.csdn.net/u013068789/article/details/82051943 介绍 UFW或Uncomplicated Firewall是iptables一个接口 ...

  3. Ubuntu 18.04 Numix主题安装设置

    Ubuntu 18.04 Numix主题安装设置 一.首先安装Numix主题 展现效果如下图 1.安装numix sudo add-apt-repository ppa:numix/ppa 2.安装主 ...

  4. Ubuntu 18.04 LTS IP 地址设置

    和之前的版本不太一样, Ubuntu 18.04 的 ip地址设置是用netplan管理的     配置文件在: /etc/netplan/50-cloud-init.yaml 示例文件如下: # T ...

  5. ubuntu16.04 下通过rc.d(rc.local)实现开机启动(未登录)anydesk

    先编辑anydesk-X.X.X/init/anydesk文件,将"DAEMON=//usr/bin$NAME"改成"DAEMON=/XXX/anydesk-5.1.1/ ...

  6. /etc/rc.d/rc.local 自定义开机启动程序

    /etc/rc.d/rc.local 用于用户自定义开机启动程序,可以往里写开机要执行的命令或脚本,线上的配置如下: [root@localhost ~]$ cat /etc/rc.d/rc.loca ...

  7. Mac下用Launchd实现使用rc.local执行开机启动命令

    其实原理很简单,使用Launchd创建一个开机启动的服务,然后这个服务关联/etc/rc.local文件,那么接下来操作rc.local就和Linux下一样的了. 当然,这种思路还可以直接使用在~/. ...

  8. Fedora 21 设置开机启动脚本

    sudo touch /etc/rc.d/rc.localsudo vim /etc/rc.d/rc.local 在/etc/rc.d/rc.local文件中写入, 然后使用:wq命令 保存并退出. ...

  9. 如何在Ubuntu 18.04上安装Go

    如何在Ubuntu 18.04上安装Go 谢鸢发表于云计算教程系列订阅98 介绍 课程准备 第1步 - 安装Go 第2步 - 设置Go路径 第3步 - 测试您的安装 结论 介绍 Go是Google开发 ...

随机推荐

  1. 微信小程序自定义TabBar

    项目中需要根据用户角色控制TabBar中各Item的显示和隐藏,然而小程序自带TabBar没有提供隐藏某个item的功能,只好自己动手写了一个. 1.wxml文件 <view class=&qu ...

  2. scrapy框架修改单个爬虫的配置,包括下载延时,下载超时设置

    在一个框架里面有多个爬虫时,每个爬虫的需求不相同,例如,延时的时间,所以可以在这里配置一下custom_settings = {},大括号里面写需要修改的配置,然后就能把settings里面的配置给覆 ...

  3. sql注入1

    一.函数 1.version() MYsql版本 2.user()    数据库用户名 3.database()   数据库名 4.@@datadir  数据库路径 5.@@version_compi ...

  4. 面试知识点准备-C++常见问题

    博客园写写格式简单的文章还行,格式一复杂就不行了,可能是我不会用吧,我有强迫症,有道云格式很好用,以后去有道写这种东西了 有道云笔记链接:http://note.youdao.com/noteshar ...

  5. Some notes in Stanford CS106A(2)

    1.Local variable(local) ex. int i = 0; factorial(i); the "i" outside the method factorial( ...

  6. Linux运维工程师应具备哪些技能?

      对于我们这些刚入门的运维小白来说,极强的好奇心总会驱使我们去涉猎各种技术,弄到最后很可能该学的知识半懵半解,知识体系混乱,学习毫无章法.因此,我们学习 时要有一个明确的目标和知识体系(也是我学习的 ...

  7. 终于开始我的java旅程了!

    首先今天先装了jdk1.7 ,找了半天,因为官网是都是让你装1.8的最新版本,地址如下: 所有jdk的历史版本: http://www.oracle.com/technetwork/java/java ...

  8. 赚钱快的app

    赚钱快的app目前强力推荐6款都是很不错的 宝石星球下载地址:http://www.baoshixingqiu.com/redPacket?key=548341 雪梨网APP下载地址 http://w ...

  9. vue打包上传oss

    今天把vue打包之后上传到oss,遇到了一点问题,现在解决了总结一下心得: OSS (Object Storage Service)名为对象存储,配合cdn使用达到静态文件托管加速,提升网站文件访问速 ...

  10. angularjs - 自定义指令(directive)

    自定义指令(directive) 使用 .directive 函数来添加自定义的指令. 要调用自定义指令,HTML 元素上需要添加自定义指令名. 例子:使用驼峰法来命名一个指令, demoDirect ...