添加應用到service
創建腳本 /etc/init.d/shadowsocks 文件(其實也是 /etc/rc.d/init.d/shadowsocks 文件):
$ sudo vim /etc/init.d/shadowsocks
添加以下內容:
#!/bin/sh # # shadowsocks start/restart/stop shadowsocks # # chkconfig: 2345 85 15 # description: start shadowsocks/ssserver at boot time start(){ ssserver -c /etc/shadowsocks/config.json -d start } stop(){ ssserver -c /etc/shadowsocks/config.json -d stop } restart(){ ssserver -c /etc/shadowsocks/config.json -d restart } case "$1" in start) start ;; stop) stop ;; restart) restart ;; *) echo "Usage: $0 {start|restart|stop}" exit 1 ;; esac
- 前面的幾行,看起來像註釋,特別chkconfig那一行,不可刪除,否則無法設置開機啓動,會提示錯誤:service shadowsocks does not support chkconfig
- chkconfig: 2345 85 15 中,2345代表在設置在那個level中是on的。如果一個都不想on,那就寫一個橫線"-",比如:chkconfig: - 85 15。後面兩個數字代表S和K的默認排序號
然後增加這個文件的可執行權限:
$ sudo chmod +x /etc/init.d/shadowsocks
這樣就可以在 shell 中直接運行下面的命令開啓程序了(重啓和停止同理):
$ sudo service shadowsocks start
設置開機啓動
在上面腳本沒有問題(也就是保留了前面10行,並且語法正確)的情況下,通過下面的命令,就可以設置程序自動啓動了:
$ sudo chkconfig shadowsocks on
這樣程序就會自動啓動了。