debian9.6添加開機執行自己的腳本

方法很簡單,在/etc/下建一個rc.local文件就行,以下就是理由。

---------------------------------

1,前言

        玩過早一點的linux的人會發現在/etc或者在/etc/rc.d目錄下有一個rc.local文件,現在沒了。某種程度上應該算有的,事情的原因起因於一個叫systemd的東西,這個sytemd取代了(system v)init的父進程身份,說是改良了init的一些缺點。至於兩者的區別,可參閱(https://www.ibm.com/developerworks/cn/linux/1407_liuming_init3/index.html),寫的很詳細。

2,查看

        注意那句註釋語句(if /etc/rc.local is executable)。

guoyanzhang@bogon:~$ cat /lib/systemd/system/rc.local.service 
#  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

3,新建

      從上面可以看到新建一個rc.local,就可以添加自己要開機執行的文件了。

guoyanzhang@bogon:~$ sudo cat /etc/rc.local 
[sudo] guoyanzhang 的密碼:
#!/bin/bash
cd /home/guoyanzhang/os
mkdir rclocal

4,補充

自systemd取代init之後,都是以服務的方式來執行腳本。

start:

guoyanzhang@bogon:~$ systemctl start rc-local
guoyanzhang@bogon:~$ systemctl status rc-local
● rc-local.service - /etc/rc.local Compatibility
   Loaded: loaded (/lib/systemd/system/rc-local.service; enabled-runtime; vendor preset: enabled)
  Drop-In: /lib/systemd/system/rc-local.service.d
           └─debian.conf
   Active: active (exited) since Sat 2019-01-19 23:15:30 CST; 26min ago
     Docs: man:systemd-rc-local-generator(8)
  Process: 877 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)

stop:

guoyanzhang@bogon:~$ systemctl stop rc-local
guoyanzhang@bogon:~$ systemctl status rc-local
● rc-local.service - /etc/rc.local Compatibility
   Loaded: loaded (/lib/systemd/system/rc-local.service; enabled-runtime; vendor preset: enabled)
  Drop-In: /lib/systemd/system/rc-local.service.d
           └─debian.conf
   Active: inactive (dead) since Sat 2019-01-19 23:42:45 CST; 11s ago
     Docs: man:systemd-rc-local-generator(8)
  Process: 877 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)

參考1:https://www.cnblogs.com/flymeng/p/7901062.html

參考2:http://blog.51cto.com/cakong/1618364

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章