http://www.linuxidc.com/Linux/2011-12/48922.htm
Linux中設置服務自啟動的三種方式
update-rc.d: warning: /etc/init.d/test missing LSB information
update-rc.d: see <http://wiki.debian.org/LSBInitScripts>
Adding system startup for /etc/init.d/test ...
/etc/rc0.d/K95test -> ../init.d/test
/etc/rc1.d/K95test -> ../init.d/test
/etc/rc6.d/K95test -> ../init.d/test
/etc/rc2.d/S95test -> ../init.d/test
/etc/rc3.d/S95test -> ../init.d/test
/etc/rc4.d/S95test -> ../init.d/test
/etc/rc5.d/S95test -> ../init.d/test
Removing any system startup links for /etc/init.d/test ...
/etc/rc0.d/K95test
/etc/rc1.d/K95test
/etc/rc2.d/S95test
/etc/rc3.d/S95test
/etc/rc4.d/S95test
/etc/rc5.d/S95test
/etc/rc6.d/K95test
linux 添加開機啟動
-恢復內容開始---
編輯 vim /etc/init.d/rc.local 文件(Ubuntu)
但是大多數都是把命令寫到/etc/rc.d/rc.local或者 /etc/rc.local里(centos),這樣雖然能夠實現隨機運行,但是並不夠靈活。不能像mysql,apache等服務一樣能夠使用service命令或者調 用init.d下的腳本啟動、關閉或者重啟進程。例如,
service mysql restart
service apache2 stop
或者
/etc/init.d/mysql restart
/etc/init.d/apache2 stop
編寫一個啟動控制腳本,以proxy為例,建立/etc/init.d/proxy文本文件,輸入下面的內容:
#!/bin/sh
case "$1" in
start)
start-stop-daemon --start --background --exec /root/proxy.py
;;
stop)
start-stop-daemon --stop --name proxy.py
esac
這是一個簡單的shell腳本,case .. in是用來根據調用參數進行不同的操作,start-stop-daemon是一個可以管理daemon進程的程序,要查看它的詳細說明,可以運行man start-stop-daemon。start的時候,使用--exec指定要執行的文件,stop的時候,使用--name根據進程名字來使用 killall結束匹配的進程。
接着,設置腳本文件屬性,設置可執行標記。
root@localhost:~# chmod 755 /etc/init.d/proxy
這樣子,就可以使用service命令來啟動和關閉進程了,例如啟動進程如下:
root@localhost:~# service proxy start root@localhost:~# ps aux|grep proxy root 353 1.4 1.9 8644 5212 ? S 09:50 0:00 /usr/bin/python /root/proxy.py root 355 0.0 0.2 1900 596 pts/0 S+ 09:50 0:00 grep --color=auto proxy
關閉進程,
root@localhost:~# service proxy stop root@localhost:~# ps aux |grep proxy root 365 0.0 0.2 1900 592 pts/0 S+ 09:51 0:00 grep --color=auto proxy
到這里,一個Linux服務的進程控制腳本已經寫好了,但是要實現隨機啟動,還需要一個步驟。
Linux開機的時候,不是直接運行/etc/init.d下的所有腳本的,而是根據不同的runlevel來執行/etc/rc$runlevel.d 下的腳本。這里的runlevel是用以區別系統的運行方式(例如單用戶的runlevel,多媒體桌面的runlevel,服務器的runlevel都 不同)。
在Ubuntu里,可以使用update-rc.d來把/etc/init.d/proxy安裝到各個runlevel中。更多關於update-rc.d的說明,請參見man update-rc.d。
root@localhost:~# update-rc.d proxy defaults 99 update-rc.d: warning: /etc/init.d/proxy missing LSB information update-rc.d: see <http://wiki.debian.org/LSBInitScripts> Adding system startup for /etc/init.d/proxy ... /etc/rc0.d/K99proxy -> ../init.d/proxy /etc/rc1.d/K99proxy -> ../init.d/proxy /etc/rc6.d/K99proxy -> ../init.d/proxy /etc/rc2.d/S99proxy -> ../init.d/proxy /etc/rc3.d/S99proxy -> ../init.d/proxy /etc/rc4.d/S99proxy -> ../init.d/proxy /etc/rc5.d/S99proxy -> ../init.d/proxy
update-rc.d后面有三個參數,分別是/etc/init.d下的腳本名字,默認安裝方式,運行的優先級。優先級的數字越大,表示越遲運行,這里我們把自己寫的服務放在最后運行。
如果要卸載隨機啟動的服務,執行
update-rc.d -f proxy remove
在update-rc.d安裝的時候提示了警告信息,是因為我們寫的/etc/init.d/proxy太簡陋了,連LSB的信息也沒有提供。
update-rc.d: warning: /etc/init.d/proxy missing LSB information
update-rc.d: see <http://wiki.debian.org/LSBInitScripts>
只需要做一些小改動,就可以避免那個警告了。如下:
#!/bin/sh
### BEGIN INIT INFO
# Provides: proxy
# 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 HTTP Proxy.
### END INIT INFO
case "$1" in
start)
start-stop-daemon --start --background --exec /root/proxy.py
;;
stop)
start-stop-daemon --stop --name proxy.py
esac
到此,一個最簡單的隨機啟動服務寫好了,看起來文章挺長的,但其實也就幾個命令而已。
在下次開機啟動的時候,proxy.py就會以root用戶身份被自動運行。
現在,你應該知道怎么編寫屬於自己的service命令了吧,編寫一個腳本,然后把它放在/etc/init.d這個目錄底下,你就可以用service +腳本名字 運行它。如果是要開機自動啟動那就得用chkconfig命令了。
注意:
A、service這個命令往往是即時生效,不用開關機,但是重啟后服務會回到默認狀態。
B、chkconfig是用於把服務加到開機自動啟動列表里,只要啟動它,就能自動啟動,重啟后永久生效
即:chkconfig --add COMMAND
chkconfig COMMAND on/off 重啟后永久生效
完
---恢復內容結束---
- 簡單
- 正規
- chkconfig
-
用start-stop-daemon啟動Nginx
編輯 vim /etc/init.d/rc.local 文件
但是大多數都是把命令寫到/etc/rc.d/rc.local或者 /etc/rc.local里,這樣雖然能夠實現隨機運行,但是並不夠靈活。不能像mysql,apache等服務一樣能夠使用service命令或者調 用init.d下的腳本啟動、關閉或者重啟進程。例如,
service mysql restart
service apache2 stop
或者
/etc/init.d/mysql restart
/etc/init.d/apache2 stop
編寫一個啟動控制腳本,以proxy為例,建立/etc/init.d/proxy文本文件,輸入下面的內容:
#!/bin/sh
case "$1" in
start)
start-stop-daemon --start --background --exec /root/proxy.py
;;
stop)
start-stop-daemon --stop --name proxy.py
esac
這是一個簡單的shell腳本,case .. in是用來根據調用參數進行不同的操作,start-stop-daemon是一個可以管理daemon進程的程序,要查看它的詳細說明,可以運行man start-stop-daemon。start的時候,使用--exec指定要執行的文件,stop的時候,使用--name根據進程名字來使用 killall結束匹配的進程。
接着,設置腳本文件屬性,設置可執行標記。
root@localhost:~# chmod 755 /etc/init.d/proxy
這樣子,就可以使用service命令來啟動和關閉進程了,例如啟動進程如下:
root@localhost:~# service proxy start root@localhost:~# ps aux|grep proxy root 353 1.4 1.9 8644 5212 ? S 09:50 0:00 /usr/bin/python /root/proxy.py root 355 0.0 0.2 1900 596 pts/0 S+ 09:50 0:00 grep --color=auto proxy
關閉進程,
root@localhost:~# service proxy stop root@localhost:~# ps aux |grep proxy root 365 0.0 0.2 1900 592 pts/0 S+ 09:51 0:00 grep --color=auto proxy
到這里,一個Linux服務的進程控制腳本已經寫好了,但是要實現隨機啟動,還需要一個步驟。
Linux開機的時候,不是直接運行/etc/init.d下的所有腳本的,而是根據不同的runlevel來執行/etc/rc$runlevel.d 下的腳本。這里的runlevel是用以區別系統的運行方式(例如單用戶的runlevel,多媒體桌面的runlevel,服務器的runlevel都 不同)。
在Ubuntu里,可以使用update-rc.d來把/etc/init.d/proxy安裝到各個runlevel中。更多關於update-rc.d的說明,請參見man update-rc.d。
root@localhost:~# update-rc.d proxy defaults 99 update-rc.d: warning: /etc/init.d/proxy missing LSB information update-rc.d: see <http://wiki.debian.org/LSBInitScripts> Adding system startup for /etc/init.d/proxy ... /etc/rc0.d/K99proxy -> ../init.d/proxy /etc/rc1.d/K99proxy -> ../init.d/proxy /etc/rc6.d/K99proxy -> ../init.d/proxy /etc/rc2.d/S99proxy -> ../init.d/proxy /etc/rc3.d/S99proxy -> ../init.d/proxy /etc/rc4.d/S99proxy -> ../init.d/proxy /etc/rc5.d/S99proxy -> ../init.d/proxy
update-rc.d后面有三個參數,分別是/etc/init.d下的腳本名字,默認安裝方式,運行的優先級。優先級的數字越大,表示越遲運行,這里我們把自己寫的服務放在最后運行。
如果要卸載隨機啟動的服務,執行
update-rc.d -f proxy remove
在update-rc.d安裝的時候提示了警告信息,是因為我們寫的/etc/init.d/proxy太簡陋了,連LSB的信息也沒有提供。
update-rc.d: warning: /etc/init.d/proxy missing LSB information
update-rc.d: see <http://wiki.debian.org/LSBInitScripts>
只需要做一些小改動,就可以避免那個警告了。如下:
#!/bin/sh
### BEGIN INIT INFO
# Provides: proxy
# 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 HTTP Proxy.
### END INIT INFO
case "$1" in
start)
start-stop-daemon --start --background --exec /root/proxy.py
;;
stop)
start-stop-daemon --stop --name proxy.py
esac
到此,一個最簡單的隨機啟動服務寫好了,看起來文章挺長的,但其實也就幾個命令而已。
在下次開機啟動的時候,proxy.py就會以root用戶身份被自動運行。
現在,你應該知道怎么編寫屬於自己的service命令了吧,編寫一個腳本,然后把它放在/etc/init.d這個目錄底下,你就可以用service +腳本名字 運行它。如果是要開機自動啟動那就得用chkconfig命令了。
注意:
A、service這個命令往往是即時生效,不用開關機,但是重啟后服務會回到默認狀態。
B、chkconfig是用於把服務加到開機自動啟動列表里,只要啟動它,就能自動啟動,重啟后永久生效
即:chkconfig --add COMMAND
chkconfig COMMAND on/off 重啟后永久生效
#! /bin/sh
### BEGIN INIT INFO
# Provides: webiopi
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: WebIOPi initscript
# Description: WebIOPi initscript
### END INIT INFO
# Author: trouch <trouch@trouch.com>
WEBIOPI_HOME=/var/www/webiopi
WEBIOPI_PORT=80
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="WebIOPi"
NAME=webiopi
DAEMON=/usr/bin/python
DAEMON_ARGS="$WEBIOPI_HOME/webiopi.py $WEBIOPI_PORT"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0
# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions
#
# Function that starts the daemon/service
#
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --background --make-pidfile -- \
$DAEMON_ARGS \
|| return 2
# Add code here, if necessary, that waits for the process to be ready
# to handle requests from services started subsequently which depend
# on this one. As a last resort, sleep for some time.
}
#
# Function that stops the daemon/service
#
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
start-stop-daemon --stop --quiet --pidfile $PIDFILE --name $NAME
RETVAL="$?"
[ "$RETVAL" = 2 ] && return 2
# Wait for children to finish too if this is a daemon that forks
# and if the daemon is only ever run from this initscript.
# If the above conditions are not satisfied then add some other code
# that waits for the process to drop all resources that could be
# needed by services started subsequently. A last resort is to
# sleep for some time.
start-stop-daemon --stop --quiet --exec $DAEMON
[ "$?" = 2 ] && return 2
# Many daemons don't delete their pidfiles when they exit.
rm -f $PIDFILE
return "$RETVAL"
}
#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
#
# If the daemon can reload its configuration without
# restarting (for example, when it is sent a SIGHUP),
# then implement that here.
#
start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
return 0
}
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
status)
status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
;;
#reload|force-reload)
#
# If do_reload() is not implemented then leave this commented out
# and leave 'force-reload' as an alias for 'restart'.
#
#log_daemon_msg "Reloading $DESC" "$NAME"
#do_reload
#log_end_msg $?
#;;
restart|force-reload)
#
# If the "reload" option is implemented then remove the
# 'force-reload' alias
#
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
exit 3
;;
esac
:
#!/bin/sh
NAME=webiopi
DAEMON=/usr/bin/python
DAEMON_ARGS="$WEBIOPI_HOME/webiopi.py $WEBIOPI_PORT"
PIDFILE=/var/run/$NAME.pid
case "$1" in
start)
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --background --make-pidfile -- $DAEMON_ARGS
;;
stop)
start-stop-daemon --stop --quiet --pidfile $PIDFILE --name $NAME
esac
sudo update-rc.d webiopi defaults
完

