1.準備好要隨機啟動的程序,例如 /root/test.sh 。確保其可執行。
2.在目錄 /etc/init.d/ 下編寫控制腳本 test 。
#!/bin/sh ### BEGIN INIT INFO # Provides: test # Required-Start: $remote_fs # Required-Stop: $remote_fs # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Start or stop the test script. ### END INIT INFOcase "$1" in start)start-stop-daemon --start --background --exec /root/test.sh ;; stop)start-stop-daemon --stop --name test.sh esac
3.使新添加的腳本文件可執行。chmod 755 /etc/init.d/test 。
4.現在可以使用start/stop來開啟/關閉服務了。
開啟服務:service test start
關閉服務:service test stop
5.使程序(服務)可以隨機啟動。update-rc.d test defaults 99 。
6.在debian6中使用update-rc.d會報錯,如下:
update-rc.d: using dependency based boot sequencing
...
7.可以使用 insserv 命令來代替 update-rc.d 。
nsserv -v -d /etc/init.d/test
8.重啟,OK.
注:隨機啟動的程序(服務)以root的權限運行。
如果程序已經啟動但沒有正確運行,將程序中使用的相對路徑改為絕對路徑。
?