测试条件,nuc980开发板。
一、开机启动脚本
为什么需要开机自启动脚本?我们编写完程序之后,下载到开发板,往往需要程序开机自行启动,为了实现开机自启动,则需要编写相关的脚本。
二、设置开机启动的步骤
开机自启动的步骤如下:
①文件夹 /etc/init.d 下面创建脚本,取名Sxx*(图1)
②编写脚本内容,保存,指令如下:
vi Sxx* //创建并打开Sxx*,了解vi的相关指令编译,保存即可
③赋权限,指令如下:
chmod 777 Sxx*
三、测试及测试结果
测试指示灯:
#!/bin/sh
#sleep 10
/MyDemo/led
会阻塞窗口,后台运行的方法 使用 &
#!/bin/sh
#sleep 10
/MyDemo/led &
或者根据官方模板编写,官方模板如下:
#!/bin/sh
#
# Start logging
#
SYSLOGD_ARGS=-n
KLOGD_ARGS=-n
[ -r /etc/default/logging ] && . /etc/default/logging
start() {
printf "Starting logging: "
start-stop-daemon -b -S -q -m -p /var/run/syslogd.pid --exec /sbin/syslogd -- $SYSLOGD_ARGS
start-stop-daemon -b -S -q -m -p /var/run/klogd.pid --exec /sbin/klogd -- $KLOGD_ARGS
echo "OK"
}
stop() {
printf "Stopping logging: "
start-stop-daemon -K -q -p /var/run/syslogd.pid
start-stop-daemon -K -q -p /var/run/klogd.pid
echo "OK"
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit $?
修改后如下,指示灯的执行程序目录为 /MyDemo/led 。
#!/bin/sh
#
# Start led
#
LED_ARGS=-n
start() {
printf "Starting led: "
start-stop-daemon -b -S -q -m -p /var/run/led.pid --exec /MyDemo/led -- $LED_ARGS
echo "OK"
}
stop() {
printf "Stopping led: "
start-stop-daemon -K -q -p /var/run/led.pid
echo "OK"
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit $?
按照前面步骤保存,赋权限。可以手动测试,指令
./Sxx*
可以看到指示灯闪烁。重启之后指示灯自动运行,自启动设置完成。
四、相关知识
busybox init启动后读取/etc/inittab文件,内容如下:
$ cat /etc/inittab
# /etc/inittab
#
# Copyright (C) 2001 Erik Andersen <andersen@codepoet.org>
#
# Note: BusyBox init doesn't support runlevels. The runlevels field is
# completely ignored by BusyBox init. If you want runlevels, use
# sysvinit.
#
# Format for each entry: <id>:<runlevels>:<action>:<process>
#
# id == tty to run on, or empty for /dev/console
# runlevels == ignored
# action == one of sysinit, respawn, askfirst, wait, and once
# process == program to run
# Startup the system 系统启动
::sysinit:/bin/mount -t proc proc /proc
::sysinit:/bin/mount -o remount,rw /
::sysinit:/bin/mkdir -p /dev/pts
::sysinit:/bin/mkdir -p /dev/shm
::sysinit:/bin/mount -a
::sysinit:/bin/hostname -F /etc/hostname
# now run any rc scripts 开机之后读取/etc/init.d文件下rcS脚本
::sysinit:/etc/init.d/rcS
# /bin/sh invocations on selected ttys
#
# Note below that we prefix the shell commands with a "-" to indicate to the
# shell that it is supposed to be a login shell. Normally this is handled by
# login, but since we are bypassing login in this case, BusyBox lets you do
# this yourself...
#
# Start an "askfirst" shell on the console (whatever that may be)
::once:-/bin/sh
# Put a getty on the serial port
#ttyS0::respawn:/sbin/getty -L ttyS0 115200 vt100 # GENERIC_SERIAL
# Stuff to do for the 3-finger salute
#::ctrlaltdel:/sbin/reboot
# Stuff to do before rebooting 重启前关闭下面相关的可执行脚本
::shutdown:/etc/init.d/rcK
::shutdown:/sbin/swapoff -a
::shutdown:/bin/umount -a -r
$ cat rcS /etc/init.d/rcS脚本内容 开机读取可执行脚本并开始运行
#!/bin/sh
# Start all init scripts in /etc/init.d
# executing them in numerical order.
#
for i in /etc/init.d/S??* ;do
# Ignore dangling symlinks (if any).
[ ! -f "$i" ] && continue
case "$i" in
*.sh)
# Source shell script for speed.
(
trap - INT QUIT TSTP
set start
. $i
)
;;
*)
# No sh extension, so fork subprocess.
$i start
;;
esac
done
$ cat rcK /etc/init.d/rcK脚本内容 重启前读取可执行脚本并停止运行
#!/bin/sh
# Stop all init scripts in /etc/init.d
# executing them in reversed numerical order.
#
for i in $(ls -r /etc/init.d/S??*) ;do
# Ignore dangling symlinks (if any).
[ ! -f "$i" ] && continue
case "$i" in
*.sh)
# Source shell script for speed.
(
trap - INT QUIT TSTP
set stop
. $i
)
;;
*)
# No sh extension, so fork subprocess.
$i stop
;;
esac
done
欢迎关注公众号:嵌入式学习与实践
参考:
https://blog.csdn.net/a15236617777/article/details/131009127?spm=1001.2014.3001.5502