当前位置: 首页 > news >正文

linux开机自启动三种方式

方式一、 1:rc.local 文件

1、执行命令:编辑 “/etc/rc.local”

vi /ect/rc.local

2、然后在文件最后一行添加要执行程序的全路径。
例如,每次开机时要执行一个 hello.sh,这个脚本放在 / usr 下面,那就可以在 “/etc/rc.local” 中加一行 “/usr/./hello.sh”,或者 "cd /usr/ && ./hello.sh

在这里插入图片描述
注意,你的命令应该添加在:exit 0 之前

3、添加完保存后设置 rc.local 可执行权限
chmod +x /etc/rc.local

方式二 :在 / etc/init.d 目录下添加自启动脚本

linux 在 “/etc/rc.d/init.d” 下有很多的文件,每个文件都是可以看到内容的,其实都是一些 shell 脚本或者可执行二进制文件
Linux 开机的时候,会加载运行 / etc/init.d 目录下的程序,因此我们可以把想要自动运行的脚本放到这个目录下即可。系统服务的启动就是通过这种方式实现的。
PS:添加完后务必设置文件的可执行权限 chmod +x filename

方式三:制作 Linux 服务并设置开机自启动

1.在这个目录下创建服务:
/etc/systemd/system
文件名为 XXXX.service

2.对应XXXX.service内容:

[Unit]
Description=XXXX(你程序的名字)
After=network.target(在哪个服务之后)[Service]
Type=simple
ExecStart=/root/xxx/xxx/XXXX         #启动程序  对应路径或绝对路径
WorkingDirectory=/root/xxx/xxx       #程序工作目录(这个和上面这一项可自由搭配)
StandardOutput=null                  #日志相关
Restart=always                       #程序自动重启(守护线程)
RestartSec=10                        #程序重启间隔
StartLimitInterval=5                 #间隔内重启最大次数
User=root                            #使用者[Install]
WantedBy=multi-user.target

3.完成后,设置程序的启动或开机自启:
通用命令:
systemctl start WEI-iEdge.service (启动服务)
systemctl enable WEI-iEdge.service (启动服务的开机自启)

  1. #重新加载配置
    systemctl daemon-reload

示例一 设置minio为服务,并设置开机自启动

/usr/local/minio/etc/minio.conf

MINIO_VOLUMES="/usr/local/minio/data"#文件存储地址
MINIO_OPTS="-C /usr/local/minio/etc --address xxxxx:9000   --console-address xxxxx:8848"
MINIO_ACCESS_KEY="minioadmin" 
#用户名
MINIO_SECRET_KEY="xxxxxx" 
#密码
[Unit]
Description=MinIO
Documentation=https://docs.min.io
Wants=network-online.target
After=network-online.target
AssertFileIsExecutable=/usr/local/minio/bin/minio[Service]
# User and group
User=minio
Group=minioEnvironmentFile=/usr/local/minio/etc/minio.conf
ExecStart=/usr/local/minio/bin/minio server $MINIO_OPTS $MINIO_VOLUMES# Let systemd restart this service always
Restart=always
# Specifies the maximum file descriptor number that can be opened by this process
LimitNOFILE=65536# Disable timeout logic and wait until process is stopped
TimeoutStopSec=infinity
SendSIGKILL=no[Install]
WantedBy=multi-user.target

启动服务
#启动minio服务
systemctl start minio.service

#添加开机自启动
systemctl enable minio.service

#查看状态
systemctl status minio.service
#关闭服务:
systemctl stop minio
#重新加载配置
systemctl daemon-reload

示例二 ,设置niginx的服务为自启服务

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s quit
ExecReload=/usr/local/nginx/sbin/nginx -s reload
PrivateTmp=true[Install]
WantedBy=multi-user.target

启动服务
#启动minio服务
systemctl start nginx.service

#添加开机自启动
systemctl enable nginx.service

#查看状态
systemctl status nginx.service
#关闭服务:
systemctl stop minio
#重新加载配置
systemctl daemon-reload

在这里插入图片描述

http://www.lryc.cn/news/470845.html

相关文章:

  • AI创作者与人类创作者的协作模式
  • FPGA第 13 篇,使用 Xilinx Vivado 创建项目,点亮 LED 灯,Vivado 的基本使用(点亮ZYNQ-7010开发板的LED灯)
  • Kafka文档阅读笔记之基本操作
  • Golang | Leetcode Golang题解之第506题相对名次
  • 机器学习——元学习(Meta-learning)
  • 【TIMM库】是一个专门为PyTorch用户设计的图像模型库 python库
  • 【AIGC】从CoT到BoT:AGI推理能力提升24%的技术变革如何驱动ChatGPT未来发展
  • 若依部署上线遇到的问题
  • 一个vue3的待办列表组件
  • 深入分析梧桐数据库SQL查询之挖掘季度销售冠军
  • 「ZJUBCA秋季迎新见面会预告」
  • 钉钉消息推送工具类
  • Android Studio 导入/删除/新建库的模块(第三方项目) - Module
  • flowable 去掉自带的登录权限
  • 第T8周:猫狗识别
  • 第十七周:机器学习
  • 算法4之链表
  • 掌握未来技术:KVM虚拟化安装全攻略,开启高效云端之旅
  • 挖矿病毒的处理
  • JVM(HotSpot):GC之G1垃圾回收器
  • appium文本输入的多种形式
  • springboot095学生宿舍信息的系统--论文pf(论文+源码)_kaic
  • 使用SQL在PostGIS中创建各种空间数据
  • ArkTS 如何适配手机和平板,展示不同的 Tabs 页签
  • Docker下载途径
  • Windows: 如何实现CLIPTokenizer.from_pretrained`本地加载`stable-diffusion-2-1-base`
  • MySQL 9从入门到性能优化-慢查询日志
  • ARM学习(33)英飞凌(infineon)PSOC 6 板子学习
  • 华为原生鸿蒙操作系统的发布有何重大意义和影响:
  • API 接口:连接生活与商业的数字桥梁