【导读】
Linux系统在启动的过程中,会先启动内核,通过内核初始化所有的设备。在初始化完这些设备后,会执行一个叫“rc.local”的文件,它是进入系统前最后执行的一个文件,主要用于定义一些需要在进入系统前,所需要启动的脚本程序。所以通过编辑该文件可以实现自启动的功能。
具体操作
该文件存放于根目录下的 /etc/ 文件夹中,通过编辑该文件,在文件中 exit 0 语句的前面加入执行语句,即可实现自启动。
例如:开机时,附带启动/home/目录下的 test.py 文件,操作如下:
#!/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.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
python3 /home/test.py
exit 0
在文件中,加入 python3 /home/test.py 即可!
PS:在定义中,需要指定执行的文件解析器,由于这里启动的是 .py 文件,所以需要调用 python3 解析器来执行该文件。