nuc980 study notes 7-Set auto-start at boot

Test conditions, nuc980 development board.

1. Boot startup script

Why do I need an auto-start script at boot? After we write the program and download it to the development board, we often need the program to start automatically at boot. In order to achieve automatic startup at boot, we need to write relevant scripts.

2. Set the steps for booting up

The steps for auto-starting are as follows:

①Create a script under the folder /etc/init.d and name it Sxx* (Figure 1)

debb3809d3ab42854cf203d73480ec64.png

②Write the script content and save it. The instructions are as follows:

vi Sxx*  //创建并打开Sxx*,了解vi的相关指令编译,保存即可

③To grant permissions, the instructions are as follows:

chmod 777 Sxx*

3. Testing and test results

Test indicator light:

#!/bin/sh


#sleep 10
/MyDemo/led 




会阻塞窗口,后台运行的方法 使用 &


#!/bin/sh


#sleep 10
/MyDemo/led &

Or write according to the official template, which is as follows:

#!/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 $?

After modification, the execution program directory of the indicator light is /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 $?

Follow the previous steps to save and grant permissions. Can be tested manually, instructions

./Sxx*

You can see the indicator light flashing. After restarting, the indicator light will run automatically, and the self-startup setting is completed.

4. Relevant knowledge

After busybox init starts, it reads the /etc/inittab file. The content is as follows:

$ cat /etc/inittab
# /etc/inittab
#
# Copyright (C) 2001 Erik Andersen <[email protected]>
#
# 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

Welcome to follow the public account: Embedded Learning and Practice

reference:

https://blog.csdn.net/a15236617777/article/details/131009127?spm=1001.2014.3001.5502

Guess you like

Origin blog.csdn.net/weixin_46158019/article/details/133191599