1. systemd初始化进程
用systemd启动项管理,适合开机加载应用程序(目前Linux系统上的主流服务管理方式)
# systemd 的常用命令
systemctl start ×××.service //启动服务
systemctl stop ×××.service //停止服务
systemctl restart ×××.service //重启服务
systemctl enable ×××.service //开机使能服务
systemctl disable ×××.service //开机不启动服务
systemctl status ×××.service //查看服务状态
例:Qt在Linux下的开机自启动
1.先在 /etc/systemd/system/ 下创建以 ×××.service 结尾的文件脚本 (权限)
[Unit]
Description=run measure program.
After=multi-user.target
[Service]
Type=simple
ExecStart=/usr/bin/serialtcp
[Install]
WantedBy=multi-user.target
2.将启动程序serialtcp放在/usr/bin/(可执行程序)
{
note: a.使用cp命令时,cp serialtcp /usr/bin/,时,需使用参数 -f.
cp -f serialtcp /usr/bin/
(强制进行拷贝操作,如果目标文件不能打开,使用-f)
}
3.systemctl enable ×××.service 开机使能该服务
2. systemV初始化进程
用systemV启动项管理(老版Linux系统上的主流服务管理方式)
update-rc.d name defaults #开机自启动
update-rc.d -f name remove #删除自启动
- 在/etc/init.d/中,添加自己的script(app_boot)
#!/bin/sh
#/etc/init.d/testboot
### BEGIN INIT INFO
# Provides:testboot
# Required-Start:$remote_fs $syslog
# Required-Stop:$remote_fs $syslog
# Default-Start:2 3 4 5
# Default-Stop:0 1 6
# Short-Description: testboot
# Description: This service is used to start my applaction
### END INIT INFO
case "$1" in
start)
echo "start your app here."
/home/root/release1.6.2/wireless_ap.sh
;;
stop)
echo "stop your app here."
;;
*)
echo "Usage: service testboot start|stop"
exit 1
;;
esac
exit 0
- 设置脚本的启动权限
- update-rc.d app_boot defaults
3. ~/.config/autostart/
通过桌面启动(安装带有桌面系统的版本)
在 ~/.config/autostart/ 目录下添加后缀为 .desktop 的文件,加载脚本。例:
[Desktop Entry]
Name=Wi-Fi
Comment=wireless_ap script
Exec=sh /home/root/release1.6.2/wireless_ap.sh &
Terminal=false
MultipleArgs=false
Type=Application
Categories=Application;Development;
StartupNotify=true
4. /etc/profile
该配置文件在启动上电后会被加载,不适合放入一些大型的启动脚本来管理,只适合做一些环境变量配置等小任务(耗时短)
5. /etc/rc.d/
用户将自己的 shell script 放入 /etc/rc.d/rc.local 或者 /etc/rc.local 中
此外也可往 vi /etc/init.d/rcS 添加需要启动的shell命令