嵌入式Linux--开机自启动(/etc/rc.local)、环境变量(/etc/profile)

发布时间 2023-04-21 18:36:02作者: DoubleLi

 

 

开机自启动

/etc/rc.d/rc.local 用于添加开机启动命令
/etc/rc.local是/etc/rc.d/rc.local的软连接
软连接相当于windows的快捷键

嵌入式Linux只有:

  • 文件:/etc/rc.local
  • 目录:/etc/rc0.d
  • 目录:/etc/rc1.d
  • 目录:/etc/rc2.d
  • 目录:/etc/rc3.d
  • 目录:/etc/rc4.d
  • 目录:/etc/rc5.d
  • 目录:/etc/rc6.d
  • 目录:/etc/rcS.d
vi /etc/rc.local

我的这个文件:


#!/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.


echo 30000 >  /proc/sys/vm/min_free_kbytes
echo "0" > /sys/class/graphics/fb0/blank
source /etc/profile
/opt/qt5.5.1/apps/QDesktop/QDesktop >/dev/null 2>&1 & 
exit 0

从这里可以看到:

  • 第一行不知道啥意思
  • 第二行也不知道啥意思
  • 第三行:就执行生效一下环境配置文件/etc/profile
  • 第四行:应该是加载桌面
  • 第五行:退出

环境变量

/etc/profile 文件中设置的变量是全局变量

  • .bashrc文件(在用户的家目录下)则只对当前用户有用。
  • ~/.bashrc~/.bash_file 是当前用户目录下的配置信息。

修改后用 source 命令更新。

vi /etc/profile

修改完毕后,使用source命令进行生效环境变量的配置。

source /etc/profile

重新上电后需要重新source一下才能生效。

/etc/profile中有一段代码的解释:

if [ -d /etc/profile.d ]; then      # 判断/etc/profile.d是不是一个目录
  for i in /etc/profile.d/*.sh ; do # 若为目录,则进到该目录下,取出每个shell程序
    if [ -f $i -a -r $i ]; then     # 如果该shell可以执行
      . $i                          # 就执行它
    fi
  done
  unset i
fi

其实就是加载/etc/profile.d中的shell脚本。

总结

  • 开机自启动:/etc/rc.local – 包含加载 source /etc/profile
  • 加载/etc/profile – 又可以加载/etc/profile