概述
一、概念介绍
1.1 开机启动脚本存放目录
通常开机启动脚本都在/etc/init.d 目录,如下图:
/etc/init.d # ls -al
total 328
drwxr-xr-x 2 1023 1023 5792 Apr 12 2022 .
drwxr-xr-x 27 1023 1023 5800 Apr 12 2022 ..
-rwxr-xr-x 1 1023 1023 3340 Apr 12 2022 adbd
-rwxr-xr-x 1 1023 1023 250 Apr 12 2022 alignment.sh
-rwxr-xr-x 1 1023 1023 492 Apr 12 2022 banner.sh
-rwxr-xr-x 1 1023 1023 1997 Apr 12 2022 bootlogd
-rwxr-xr-x 1 1023 1023 1922 Apr 12 2022 bootmisc.sh
-rwxr-xr-x 1 1023 1023 189 Apr 12 2022 bsp_paths.sh
-rwxr-xr-x 1 1023 1023 681 Apr 12 2022 busybox-cron
-rwxr-xr-x 1 1023 1023 3637 Apr 12 2022 checkroot.sh
-rwxr-xr-x 1 1023 1023 203 Apr 12 2022 chgrp-diag
-rwxr-xr-x 1 1023 1023 982 Apr 12 2022 data-init
-rwxr-xr-x 1 1023 1023 2887 Apr 12 2022 dbus-1
-rwxr-xr-x 1 1023 1023 587 Apr 12 2022 devpts.sh
-rwxr-xr-x 1 1023 1023 353 Apr 12 2022 dmesg.sh
-rwxr-xr-x 1 1023 1023 933 Apr 12 2022 dnsmasq
-rwxr-xr-x 1 1023 1023 3474 Apr 12 2022 find_partitions.sh
-rwxr-xr-x 1 1023 1023 1732 Apr 12 2022 firmware-links.sh
lrwxrwxrwx 1 1023 1023 33 Apr 12 2022 functions -> /etc/init.d/functions.initscripts
-rw-r--r-- 1 1023 1023 2141 Apr 12 2022 functions.initscripts
-rwxr-xr-x 1 1023 1023 510 Apr 12 2022 halt
-rwxr-xr-x 1 1023 1023 580 Apr 12 2022 hostname.sh
-rwxr-xr-x 1 1023 1023 2541 Apr 12 2022 hwclock.sh
-rwxr-xr-x 1 1023 1023 579 Apr 12 2022 inetd.busybox
-rwxr-xr-x 1 1023 1023 295 Apr 12 2022 init_irsc_util
-rwxr-xr-x 1 1023 1023 348 Apr 12 2022 keymap.sh
-rwxr-xr-x 1 1023 1023 168 Apr 12 2022 modem-shutdown
-rwxr-xr-x 1 1023 1023 1223 Apr 12 2022 modutils.sh
-rwxr-xr-x 1 1023 1023 859 Apr 12 2022 mountall.sh
-rwxr-xr-x 1 1023 1023 1522 Apr 12 2022 mountnfs.sh
-rwxr-xr-x 1 1023 1023 845 Apr 12 2022 mssboot
-rwxr-xr-x 1 1023 1023 632 Apr 12 2022 netmgrd
-rwxr-xr-x 1 1023 1023 1956 Apr 12 2022 networking
-rwxr-xr-x 1 1023 1023 390 Apr 12 2022 notifymgr
-rwxr-xr-x 1 1023 1023 1931 Apr 12 2022 odm_upgrader
-rwxr-xr-x 1 1023 1023 6470 Apr 12 2022 populate-volatile.sh
-rwxr-xr-x 1 1023 1023 724 Apr 12 2022 port_bridge
-rwxr-xr-x 1 1023 1023 23056 Apr 12 2022 power_config
-rwxr-xr-x 1 1023 1023 1036 Apr 12 2022 psmd
-rwxr-xr-x 1 1023 1023 985 Apr 12 2022 qmi_shutdown_modemd
-rwxr-xr-x 1 1023 1023 4457 Apr 12 2022 rc
-rwxr-xr-x 1 1023 1023 525 Apr 12 2022 rcS
-rwxr-xr-x 1 1023 1023 968 Apr 12 2022 read-only-rootfs-hook.sh
-rwxr-xr-x 1 1023 1023 289 Apr 12 2022 reboot
-rwxr-xr-x 1 1023 1023 160 Apr 12 2022 reset_reboot_cookie
-rwxr-xr-x 1 1023 1023 585 Apr 12 2022 rmnologin.sh
-rwxr-xr-x 1 1023 1023 324 Apr 12 2022 save-rtc.sh
-rwxr-xr-x 1 1023 1023 438 Apr 12 2022 sendsigs
-rwxr-xr-x 1 1023 1023 67 Apr 12 2022 set_core_pattern.sh
-rwxr-xr-x 1 1023 1023 1141 Apr 12 2022 sfs_config
-rwxr-xr-x 1 1023 1023 1836 Apr 12 2022 shutdown
-rwxr-xr-x 1 1023 1023 578 Apr 12 2022 single
lrwxrwxrwx 1 1023 1023 8 Apr 12 2022 stop-bootlogd -> bootlogd
-rwxr-xr-x 1 1023 1023 782 Apr 12 2022 sysfs.sh
lrwxrwxrwx 1 1023 1023 26 Apr 12 2022 syslog -> /etc/init.d/syslog.busybox
-rwxr-xr-x 1 1023 1023 1703 Apr 12 2022 syslog.busybox
-rwxr-xr-x 1 1023 1023 659 Apr 12 2022 thermal-engine
-rwxr-xr-x 1 1023 1023 939 Apr 12 2022 umountfs
-rwxr-xr-x 1 1023 1023 711 Apr 12 2022 umountnfs.sh
-rwxr-xr-x 1 1023 1023 1473 Apr 12 2022 urandom
-rwxr-xr-x 1 1023 1023 5540 Apr 12 2022 usb
-rwxrwxr-x 1 1023 1023 2426 Apr 12 2022 wlan
/etc/init.d #
1.2、开机启动脚本配置目录
启动配置目录为/etc/rcX.d, 这里的 rcX.d 是指 rc0.d~rc6.d 文件夹,这些文件夹在 Linux 系统中存放于/etc 目录下,如下图:
/etc # ls -al | grep rc
drwxr-xr-x 2 1023 1023 2088 Apr 12 2022 rc0.d
drwxr-xr-x 2 1023 1023 1656 Apr 12 2022 rc1.d
drwxr-xr-x 2 1023 1023 1728 Apr 12 2022 rc2.d
drwxr-xr-x 2 1023 1023 1728 Apr 12 2022 rc3.d
drwxr-xr-x 2 1023 1023 1656 Apr 12 2022 rc4.d
drwxr-xr-x 2 1023 1023 1952 Apr 12 2022 rc5.d
drwxr-xr-x 2 1023 1023 2024 Apr 12 2022 rc6.d
drwxr-xr-x 2 1023 1023 1584 Apr 12 2022 rcS.d
1.3、rc启动级别
rc0.d~rc6.d 名称里面的数字代表的是 Linux 系统的启动级别。 Linux 通常会以一个启动级别来运行的。Linux 系统里面,我们可以通过 runlevel 这个命令来查看当前系统的启动级别。
启动级别所代表的含义如下:
0 停机(不能使用)
1 单用户模式
2 多用户模式,但不能使用 NFS 等
3 完整的多用户模式
4 系统保留(未使用)
5 图形化界面模式
6 重启模式(不能使用)
例如,获取到的当前运行的 Linux 系统的启动级别是 5, 如果想要在这个启动级别里面实现开机自动执行特定脚本,那么需要在/etc/rc5.d 中进行配置。 我们通过 ls /etc/rc5.d 可以看到/etc/rc5.d 下有很多链接文件,如下图:
/etc/rc5.d # ls -al
total 0
drwxr-xr-x 2 1023 1023 1952 Apr 12 2022 .
drwxr-xr-x 27 1023 1023 5800 Apr 12 2022 ..
lrwxrwxrwx 1 1023 1023 20 Apr 12 2022 S01networking -> ../init.d/networking
lrwxrwxrwx 1 1023 1023 29 Apr 12 2022 S01set_core_pattern.sh -> ../init.d/set_core_pattern.sh
lrwxrwxrwx 1 1023 1023 22 Apr 12 2022 S15bsp_paths.sh -> ../init.d/bsp_paths.sh
lrwxrwxrwx 1 1023 1023 20 Apr 12 2022 S15chgrp-diag -> ../init.d/chgrp-diag
lrwxrwxrwx 1 1023 1023 20 Apr 12 2022 S20hwclock.sh -> ../init.d/hwclock.sh
lrwxrwxrwx 1 1023 1023 16 Apr 12 2022 S20syslog -> ../init.d/syslog
lrwxrwxrwx 1 1023 1023 13 Apr 12 2022 S20usb -> ../init.d/usb
lrwxrwxrwx 1 1023 1023 24 Apr 12 2022 S29init_irsc_util -> ../init.d/init_irsc_util
lrwxrwxrwx 1 1023 1023 17 Apr 12 2022 S30mssboot -> ../init.d/mssboot
lrwxrwxrwx 1 1023 1023 24 Apr 12 2022 S35start_embms_le -> ../init.d/start_embms_le
lrwxrwxrwx 1 1023 1023 17 Apr 12 2022 S45netmgrd -> ../init.d/netmgrd
lrwxrwxrwx 1 1023 1023 29 Apr 12 2022 S55reset_reboot_cookie -> ../init.d/reset_reboot_cookie
lrwxrwxrwx 1 1023 1023 20 Apr 12 2022 S80sfs_config -> ../init.d/sfs_config
lrwxrwxrwx 1 1023 1023 33 Apr 12 2022 S90start_subsystem_ramdump -> ../init.d/start_subsystem_ramdump
lrwxrwxrwx 1 1023 1023 19 Apr 12 2022 S97data-init -> ../init.d/data-init
lrwxrwxrwx 1 1023 1023 16 Apr 12 2022 S98dbus-1 -> ../init.d/dbus-1
lrwxrwxrwx 1 1023 1023 22 Apr 12 2022 S99power_config -> ../init.d/power_config
lrwxrwxrwx 1 1023 1023 22 Apr 12 2022 S99rmnologin.sh -> ../init.d/rmnologin.sh
lrwxrwxrwx 1 1023 1023 23 Apr 12 2022 S99stop-bootlogd -> ../init.d/stop-bootlogd
/etc/rc5.d #
这些链接文件指向/etc/init.d 中的 shell 脚本或者其他可执行文件,它们的命名方式基本都是以“S”或者“K”开头,其后紧跟一个数字,数字后则是链接文件的名字。“S”传入的参数是“start” ,“K”传入的参数是“stop”, 紧跟后的数字是执行优先级, 数值越小, 优先级越高。
例如: 链接文件的命名为“S99test”,那么开机执行该链接时就等同于执行“/etc/init.d/test start”, 执行优先级为 99
链接文件的命名为“K99test”,那么开机执行该链接时就等同于执行“/etc/init.d/test stop”,执行优先级为99
二、应用自启动添加
2.1、添加启动脚本
进入 /etc/init.d/目录,
添加启动脚本, 以 start_test 为例。
如果脚本里面需要调用应用执行,可按照如下图所示格式填写:
#! /bin/sh
set -e
case "$1" in
start)
echo -n "Starting test: "
start-stop-daemon -S -b -a /usr/bin/test
echo "done"
;;
stop)
echo -n "Stopping test: "
start-stop-daemon -K -n test
echo "done"
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage test{ start | stop | restart }" >&2
exit 1
;;
esac
exit 0
2.2、获取运行级别
在系统中执行runlevel指令:
/ # runlevel
N 5
查看当前系统的启动级别, 如果获取的运行级别为 5, 则在/etc/rc5.d 中进行配置。
进入 /etc/rc5.d/目录,执行:ln -s /etc/init.d/start_test S99start_test,如果没有启动要求的话,建议将启动优先级设置大一些, 这里设置的启动优先级为 99,如下图:
/etc/rc5.d # ln -s ../init.d/start_test S99start_test
/etc/rc5.d # ls -al
total 0
drwxr-xr-x 2 1023 1023 2024 Jan 1 01:24 .
drwxr-xr-x 27 1023 1023 5800 Apr 12 2022 ..
lrwxrwxrwx 1 1023 1023 20 Apr 12 2022 S01networking -> ../init.d/networking
lrwxrwxrwx 1 1023 1023 29 Apr 12 2022 S01set_core_pattern.sh -> ../init.d/set_core_pattern.sh
lrwxrwxrwx 1 1023 1023 22 Apr 12 2022 S15bsp_paths.sh -> ../init.d/bsp_paths.sh
lrwxrwxrwx 1 1023 1023 20 Apr 12 2022 S15chgrp-diag -> ../init.d/chgrp-diag
lrwxrwxrwx 1 1023 1023 20 Apr 12 2022 S20hwclock.sh -> ../init.d/hwclock.sh
lrwxrwxrwx 1 1023 1023 16 Apr 12 2022 S20syslog -> ../init.d/syslog
lrwxrwxrwx 1 1023 1023 13 Apr 12 2022 S20usb -> ../init.d/usb
lrwxrwxrwx 1 1023 1023 24 Apr 12 2022 S29init_irsc_util -> ../init.d/init_irsc_util
lrwxrwxrwx 1 1023 1023 17 Apr 12 2022 S30mssboot -> ../init.d/mssboot
lrwxrwxrwx 1 1023 1023 24 Apr 12 2022 S35start_embms_le -> ../init.d/start_embms_le
lrwxrwxrwx 1 1023 1023 17 Apr 12 2022 S45netmgrd -> ../init.d/netmgrd
lrwxrwxrwx 1 1023 1023 29 Apr 12 2022 S55reset_reboot_cookie -> ../init.d/reset_reboot_cookie
lrwxrwxrwx 1 1023 1023 20 Apr 12 2022 S80sfs_config -> ../init.d/sfs_config
lrwxrwxrwx 1 1023 1023 33 Apr 12 2022 S90start_subsystem_ramdump -> ../init.d/start_subsystem_ramdump
lrwxrwxrwx 1 1023 1023 19 Apr 12 2022 S97data-init -> ../init.d/data-init
lrwxrwxrwx 1 1023 1023 16 Apr 12 2022 S98dbus-1 -> ../init.d/dbus-1
lrwxrwxrwx 1 1023 1023 22 Apr 12 2022 S99power_config -> ../init.d/power_config
lrwxrwxrwx 1 1023 1023 22 Apr 12 2022 S99rmnologin.sh -> ../init.d/rmnologin.sh
lrwxrwxrwx 1 root root 20 Jan 1 01:24 S99start_test -> ../init.d/start_test
lrwxrwxrwx 1 1023 1023 23 Apr 12 2022 S99stop-bootlogd -> ../init.d/stop-bootlogd
2.3、开机自启动验证
重启单板,进入文件系统执行 ps | grep “test”
如果看到 test 进程,说明 test 已经启动成功。
最后
以上就是有魅力大山为你收集整理的Linux添加开机自启动应用的全部内容,希望文章能够帮你解决Linux添加开机自启动应用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复