/etc/rc.local&&/etc/rc.d/init.d


Linux开机之后会执行/etc/rc.local文件中的脚本。
所以我们可以直接在/etc/rc.local中添加启动脚本

/etc/init.d/rc.local的内容

#! /bin/sh
### BEGIN INIT INFO
# Provides:          rc.local
# Required-Start:    $all
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:
# Short-Description: Run /etc/rc.local if it exist
### END INIT INFO


PATH=/sbin:/usr/sbin:/bin:/usr/bin

. /lib/init/vars.sh
. /lib/lsb/init-functions

do_start() {
    if [ -x /etc/rc.local ]; then
            [ "$VERBOSE" != no ] && log_begin_msg "Running local boot scripts (/etc/rc.local)"
        /etc/rc.local
        ES=$?
        [ "$VERBOSE" != no ] && log_end_msg $ES
        return $ES
    fi
}

case "$1" in
    start)
    do_start
        ;;
    restart|reload|force-reload)
        echo "Error: argument '$1' not supported" >&2
        exit 3
        ;;
    stop)
        ;;
    *)
        echo "Usage: $0 start|stop" >&2
        exit 3
        ;;
esac

从注释可以看出该脚本运行在2 3 4 5的启动级别,只能处理start的参数,然后执行start,如果有/etc/rc.local文件的话则执行/etc/rc.local。如果要把开机启动的程序放/etc/init.d/rc.local文件里,记住千万别一股脑写文件最后面就行了,因为在case语句块里脚本就会退出。

/etc/rc.local内容

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

exit 0

这个脚本里面基本没有内容,就是写个模板让你放开机自启动程序的。把你的程序写在exit 0行的前面就行了。

所以要添加开机启动项,只需在/etc/rc.local文件中添加就行了。


启动级别:
0   关机
1   单用户
2-5  多用户图形界面
6   重启
  
对应每个启动级别,/etc/目录下都对应一个像/etc/rc5.d/这样的目录,下面是一些脚本,这些脚本基本都是对应/etc/init.d/目录下的软链接,命名里面的数字代表优先级,启动时这些脚本都会执行一遍。
可以看到“/etc/rc.d/init.d”下有很多的文件,每个文件都是可以看到内容的,其实都是一些shell脚本。
系统服务的启动就是通过“/etc/rc.d/init.d”中的脚本文件实现的。我们也可以写一个自己的脚本放在这里。
脚本文件的内容也很简单,类似于这个样子(例如起个名字叫做“hahad”):

. /etc/init.d/functions
start() {
        echo "Starting my process "
        cd /opt
        ./haha.sh
}

stop() {
        killall haha.sh
        echo "Stopped"
}

写了脚本文件之后事情还没有完,继续完成以下几个步骤:
chmod +x hahad #增加执行权限
chkconfig --add hahad #把hahad添加到系统服务列表
chkconfig hahad on #设定hahad的开关(on/off)
chkconfig --list hahad #就可以看到已经注册了hahad的服务

你可能感兴趣的:(/etc/rc.local&&/etc/rc.d/init.d)