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/