Linux 設置程序開機自動啓動

目錄

 

1. 基礎說明

2. 編輯啓動文件

3. 給啓動文件添加權限

4. 設置軟連接


1. 基礎說明

將程序設置爲開機啓動的方法不止一種,這裏記錄的,應該是最常用的一種。

root用戶執行命令:ll /etc/rc.d/

可以看到有下面這些文件:

[centos@ip-172-31-42-26 init.d]$ ll /etc/rc.d/
總用量 16K
drwxr-xr-x. 10 root root  127 10/30 22:57 .
drwxr-xr-x. 85 root root 8.0K 06/02 03:42 ..
drwxr-xr-x.  2 root root   70 06/02 03:10 init.d
drwxr-xr-x.  2 root root   45 10/30 22:57 rc0.d
drwxr-xr-x.  2 root root   45 10/30 22:57 rc1.d
drwxr-xr-x.  2 root root   45 10/30 22:57 rc2.d
drwxr-xr-x.  2 root root   45 10/30 22:57 rc3.d
drwxr-xr-x.  2 root root   45 10/30 22:57 rc4.d
drwxr-xr-x.  2 root root   45 10/30 22:57 rc5.d
drwxr-xr-x.  2 root root   45 10/30 22:57 rc6.d
-rw-r--r--.  1 root root  473 04/25 17:19 rc.local

其中 rc0~6.d 中的 0~6 代表運行級別:

0:系統停機(關機)模式,系統默認運行級別不能設置爲0,否則不能正常啓動,一開機就自動關機。
1:單用戶模式,root權限,用於系統維護,禁止遠程登陸,就像Windows下的安全模式登錄。
2:多用戶模式,沒有NFS網絡支持。
3:完整的多用戶文本模式,有NFS,登陸後進入控制檯命令行模式。
4:系統未使用,保留一般不用,在一些特殊情況下可以用它來做一些事情。例如在筆記本電腦的電池用盡時,可以切換到這個模式來做一些設置。
5:圖形化模式,登陸後進入圖形GUI模式或GNOME、KDE圖形化界面,如X Window系統。
6:重啓模式,默認運行級別不能設爲6,否則不能正常啓動,就會一直開機重啓開機重啓。

設置程序開機啓動,要做的事情就是:

  • 在 init.d 目錄下創建一個啓動腳本(將程序啓動命令放在裏面)
  • 然後增加它的可執行權限
  • 最後設置 rcxx.d 到啓動腳本的軟連接

2. 編輯啓動文件

啓動文件示例如下,編輯之後保存(我設置的名字是:ssserverctl):

#!/bin/bash

### BEGIN INIT INFO
# Provides:          Neucrack
# Required-Start:    $remote_fs $syslog $network $named
# Required-Stop:     $remote_fs $syslog $network 
# Should-Start:      $network $portmap
# Should-Stop:       $network $portmap
# X-Start-Before:    nis
# X-Stop-After:      nis
# Default-Start:     3 5
# Default-Stop:      0 1 2 4 6
# X-Interactive:     true
# Short-Description: ss auto start script
# Description:       shadowsocks auto start script,
#                    This file should be used to construct scripts to be
#                    placed in /etc/init.d.
### END INIT INFO

case "$1" in
    start)
        `nohup ssserver -c /opt/config/shadowsocks.json > ~/backup/temp/connect.log 2>&1 &`
        ;;
    stop)
        `nohup ps aux | grep ssserver | awk '{print $2}' | xargs kill -9 2>&1 &`
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
        ;;
esac

需要注意的是,啓動命令不要是阻塞式的,避免腳本執行之後等待輸入。

3. 給啓動文件添加權限

執行命令:chmod +x ssserverctl

[root@izj6ca57bbyivzigu89mfoz:init.d]$ ll
總用量 64
-rwxr-xr-x 1 root root   972 8月  25 23:36 ssserverctl

設置完成之後,可以執行下面的命令來檢查腳本是否有效(start/stop對應腳本里面的參數):

/etc/init.d/ssserverctl start

/etc/init.d/ssserverctl stop

4. 設置軟連接

設置軟連接的時候,要注意命名規則。

查看已有的軟連接:ll /etc/rc.d/rc3.d/

[root@izj6ca57bbyivzigu89mfoz:init.d]$ ll /etc/rc.d/rc3.d/
總用量 0
lrwxrwxrwx. 1 root root 20 8月  18 2017 K50netconsole -> ../init.d/netconsole
lrwxrwxrwx. 1 root root 17 8月  18 2017 S10network -> ../init.d/network
lrwxrwxrwx  1 root root 22 8月  24 2017 S15staragentctl -> ../init.d/staragentctl
lrwxrwxrwx  1 root root 15 8月  13 19:42 S50aegis -> ../init.d/aegis
lrwxrwxrwx  1 root root 16 8月  13 19:42 S64mysqld -> ../init.d/mysqld
lrwxrwxrwx  1 root root 19 8月  24 2017 S85apachectl -> ../init.d/apachectl
lrwxrwxrwx  1 root root 17 8月  24 2017 S85php-fpm -> ../init.d/php-fpm
lrwxrwxrwx  1 root root 23 8月  25 23:42 S91ssserverctl -> /etc/init.d/ssserverctl

可以發現在啓動腳本前面都加了 “K數字”,或者 “S數字”

其中 K 表示 Kill 某個程序,S 表示 Start 某個程序

後面緊跟着的數字,表示啓動/停止某個程序的順序,數字越小的越先啓動(數字的具體值可以自己根據情況自行設定)

我這裏設置軟連接用的命令是:

ln -s /etc/init.d/ssserverctl /etc/rc3.d/S91ssserverctl

如果要設置程序關機自動停止,可以新增一個軟連接:

ln -s /etc/init.d/ssserverctl /etc/rc0.d/K91ssserverctl

如此設置之後,開機重啓,檢查程序是否可以開機自動啓動。

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