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

本文详细介绍了如何在ArchLinux版本的树莓派上实现开机自启动程序或执行脚本,包括使用systemd管理守护进程、添加服务文件、创建软链接等步骤,并提供了实际操作示例。

树莓派开机自启动程序(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

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

评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值