linux 添加开机启动

http://www.cnblogs.com/wangkangluo1/archive/2012/04/20/2459036.html

 

##

顺便说一下,记录一下,之前无做无密登陆之时,发现一个问题,不管怎么在/root/.ssh/authorized_keys加入来源机器的KEY都 不生效,后来一发现,原来这个目录下的所有文件被改了chown,汗汗汗!! 还看了半天没看出来;

##

linux 添加开机启动

 

---恢复内容开始---


 

编辑  vim /etc/init.d/rc.local 文件

 


 

参考:创建一个最简单的Linux随机启动服务

但是大多数都是把命令写到/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    重启后永久生效


 

---恢复内容结束---

  • 简单
  • 正规
  • chkconfig
  • 用start-stop-daemon启动Nginx


 

编辑  vim /etc/init.d/rc.local 文件

 


 

参考:创建一个最简单的Linux随机启动服务

但是大多数都是把命令写到/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    重启后永久生效


 webiopi:

复制代码
#! /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 <[email protected]>
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

猜你喜欢

转载自gelongmei.iteye.com/blog/2359485