openwrt下自建启动脚本的方法

首先进入etc/rc.d目录层,该层有许多启动脚本,查看一个稍微简单的

#!/bin/sh /etc/rc.common
START=95
STOP=10
boot() {
return 0
}
reload() {
/usr/lib/ddns/dynamic_dns_updater.sh -- reload
return 0
}
restart() {
/usr/lib/ddns/dynamic_dns_updater.sh -- stop
sleep 1
/usr/lib/ddns/dynamic_dns_updater.sh -- start
}
start() {
/usr/lib/ddns/dynamic_dns_updater.sh -- start
}
stop() {
/usr/lib/ddns/dynamic_dns_updater.sh -- stop
return 0
}

可以看到有start(),stop(),boot()等函数体,当然还有其他函数体
一般来说start或者boot是必须的
各个函数体的作用一般如下:

start   # 启动服务
stop    # 停止服务
restart # 重启服务
reload  # 重新载入配置文件, 如果失败则重启
enable  # 启用开机自启动
disable  # 禁用开机自启动
当存在boot()时, 系统启动时会调用boot()而不是start()

下面介绍如何自建启动脚本:

1.cd /etc/init.d    # 进入init.d目录

2. 创建一个你的脚本,例如echo_hello

3. 在echo_hello文件中加入以下内容:

#!bin/sh /etc/rc.common  
 
START=88	   #设置启动顺序

start(){        
        echo “hello” >/dev/console/	#输出打印信息hello到console
        echo hello >/tmp/hello			#在tmp目录下生成hello文件
}

4、chmod 0755 /etc/init.d/test    # 给权限

5. cd /etc/rc.d    # 进入rc.d目录

6. ln -s /etc/init.d/echo_hello /etc/rc.d/S88echo_hello    # 建立启动软链接创建启动脚本

完成之后,reboot重启板子,重启时,在执行到88的脚本时,便会打印出hello信息,并且生成/tmp/hello,重启完成查看tmp目录下是否生成hello文件即可验证。

启动顺序:
START=XX 和 STOP=XX 是用来决定脚本启动时的顺序. (例如上面我建立的脚本start=88,就意味着启动顺序在87之后在89之前)
初始化脚本可以作为/etc/init.d/下文件的软链放置在/etc/rc.d/. enable 和 disable 可以自动帮你创建对应的带序号的软链.

附上查看服务的启动状态的方法

root@:/# for F in /etc/init.d/* ; do $line enabled && echo $line on || echo
 $line **disabled**; done
/etc/init.d/acd on
/etc/init.d/alsa on
/etc/init.d/boot on
/etc/init.d/cron on
/etc/init.d/ddns on
/etc/init.d/dhcrelay4 on
/etc/init.d/dhcrelay6 on
/etc/init.d/dnsmasq on
/etc/init.d/done on
/etc/init.d/dropbear on
/etc/init.d/estserver on
/etc/init.d/firewall on
/etc/init.d/hyd on
/etc/init.d/hyfi-bridging on
/etc/init.d/inetd **disabled**
/etc/init.d/lacpd on
/etc/init.d/lbd on
/etc/init.d/led on
/etc/init.d/lighttpd on
/etc/init.d/lldpd **disabled**
/etc/init.d/log on
/etc/init.d/macsec **disabled**
/etc/init.d/mcproxy on
/etc/init.d/mcsd on
/etc/init.d/netbios on
/etc/init.d/network on
/etc/init.d/odhcpd **disabled**
/etc/init.d/powerctl on
/etc/init.d/pppoe-relay **disabled**
/etc/init.d/pppoe-server on
/etc/init.d/qca-edma on
/etc/init.d/qca-hostapd on
/etc/init.d/qca-nss-drv on
/etc/init.d/qca-nss-ecm on
/etc/rc.common: /etc/init.d/qca-ssdk: line 110: syntax error: unexpected "}"
/etc/init.d/qca-ssdk **disabled**
/etc/init.d/qca-wpa-supplicant on
/etc/init.d/qcmbr **disabled**
/etc/init.d/qrfs **disabled**
/etc/init.d/repacd on
/etc/init.d/rngd on
/etc/init.d/rpcd on
/etc/init.d/shortcut-fe on
/etc/init.d/skb_recycler on
/etc/init.d/ssid_steering **disabled**
/etc/init.d/sysctl on
/etc/init.d/sysfixtime on
/etc/init.d/sysntpd on
/etc/init.d/sysstat on
/etc/init.d/system on
/etc/init.d/telnet on
/etc/init.d/thermal on
/etc/init.d/umount **disabled**
/etc/init.d/wsplcd on
/etc/init.d/xd on
root@:/# 

你可能感兴趣的:(openwrt)