linux下开机启动脚本的方法

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 INFO
 
case "$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的权限运行。

  如果程序已经启动但没有正确运行,将程序中使用的相对路径改为绝对路径。

  

 

posted on 2013-10-18 11:07  buptpatriot  阅读(355)  评论(0编辑  收藏  举报

导航