栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Ubuntu添加开机自启动程序

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Ubuntu添加开机自启动程序

前几天学会了如何通过串口来通信树莓派与STM32,并成功点灯,最近想着如何实现自动启动通信程序,查找了许多资料,在此记录一下错误解决与实现。

一、 新建脚本,将脚本添加到启动脚本(推荐,可控,而且非常清楚) 1.1 在/etc/init.d/ 下新建脚本xxx 或者 xxx.sh 
#!/bin/bash
 
# command content
 
exit 0
1.2 设置文件权限
chmod 755 xxx

报错:chmod: changing permissions of 'xxx': Operation not permitted
解决:输入指令

sudo su    //更改权限
1.3 将脚本添加到启动脚本
update-rc.d xxx defaults 90

(移除开机脚本:update-rc.d -f xxx remove)

报错:insserv: warning: script 'hahaha.sh' missing LSB tags and overrides

解决:找到/etc/init.d/XXX文件,在该文件的 #!/bin/bash 行后加入如下内容:

保存后,重新运行即可。

### BEGIN INIT INFO
# Provides:          XXX
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: start XXX
# Description:       start XXX
### END INIT INFO
3 适用于Ubuntu 和 Centos 
参考:systemctl命令_海贼懒懒-CSDN博客

在/etc/init.d编写脚本命令后,比如命名为test,然后直接运行  systemctl enable test  或 systemctl enable test.service

4 查看开机启动项
systemd-analyze blame
二、rc-local方法 1、建立rc-local.service文件
sudo vi /etc/systemd/system/rc-local.service

2、将下列内容复制进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

3、编辑文件rc.local
sudo vi /etc/rc.local

4、将下列内容复制进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 "看到这行字,说明添加自启动脚本成功。" > /usr/local/test.log

/bin/bash /home/rikirobot/startup/scripts/python_up.sh >> /usr/local/print.log 2>&1

exit 0

5、给rc.local加上权限
sudo chmod +x /etc/rc.local

6、启用服务
sudo chmod +x /etc/rc.local

7、启动服务并检查状态
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service

8、重启并检查test.log文件
cat /usr/local/test.log 


如果能看到内容,说明设置成功,你就可以通过编辑rc.local文件来设置启动脚本了

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/333710.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号