树莓派开机自启动程序(ArchLinux 版本)

树莓派开机自启动程序(ArchLinux 版本)

如何让树莓派开机后自动启动用户的程序或者执行脚本?

不同的Linux发行版有不同的自启动机制,如RedHat有 /etc/rc.local 文件,在里面写上要执行的命令就可以开机执行。 Arch Linux 采用的是守护进程的机制(daemon)。 在Arch Linux中, 守护进程是用systemd管理的. 用户用systemctl命令来管理. systemctl读取.service文件中包含怎么和什么时候启动相关的进程. Service的文件保存在/{etc,usr/lib,run}/systemd/system中. 看看systemd#Using units 有关怎么使用systemctl管理守护进程的完整信息.

开机时自动启动

在启动的时候添加,删除服务使用 systemctl enable|disable service_name命令

手动启动

在系统运行时启动,停止服务, 使用 systemctl start|stop service_name命令.

重启服务

为了重启服务, 使用 systemctl restart service_name命令.

查看运行状态

查看当前服务的运行状态, 使用 systemctl status service_name命令.

检查服务是否开机启动

检查服务是否开机启动,使用 systemctl is-enabled service_name; echo $?命令.

手动添加开机运行的服务

ln -sf /lib/systemd/system/ /etc/systemd/system/

demo:

1 将脚本写入/etc/rc.local

++++++++++++++++++++++++++++++++++++++++++++++++++++

#!/bin/bash

# this file defines the commands that will be executed at system startup

echo "exect the application /home/pi/hello" > /dev/ttyAMA0

./home/hello

++++++++++++++++++++++++++++++++++++++++

2 添加可执行权限

chmod +x /etc/rc.local

3 创建服务文件 /usr/lib/systemd/system/rc-local.service

++++++++++++++++++

[Unit] Description=/etc/rc.local Compatibility

ConditionPathExists=/etc/rc.local

[Service]

Type=forking

ExecStart=/etc/rc.local start

TimeoutSec=0

StandardOutput=tty

RemainAfterExit=yes

SysVStartPriority=99

[Install]

WantedBy=multi-user.target

+++++++++++++++++++++++++

4 添加软链接

cd /etc/systemd/system/multi-user.target.wants

ln -s /usr/lib/systemd/system/rc-local.service rc-local.service

5 启用服务

systemctl enable rc-local.service

6测试效果

重启

reboot

或者 直接启动服务

systemctl start rc-local.service

如果系统启动后,程序确实执行了,则表示自启动设置成功

猜你喜欢

转载自blog.csdn.net/renshengrumenglibing/article/details/8963271