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=minio

EnvironmentFile=/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

相关推荐
赤龙ERP11 分钟前
ERP周报:连接器开闸、国产GPU进场与智能工厂253家
数据库·人工智能·大模型·rpa·erp·企业数字化·builderpattern
lusklusklusk18 分钟前
SQL Server补丁版本号为什么“日期新反而版本低”?——GDR、CU、CU+GDR 版本号规则与升级路径详解
数据库
叼烟扛炮34 分钟前
[LangChain] 学习笔记(02):LCEL 链式调用与动态模型配置实战
服务器·开发语言·网络·笔记·学习·langchain
key_3_feng35 分钟前
评估与对齐:Agent 自我进化的护栏
前端·数据库·agent
TangGeeA36 分钟前
数据库、SQL 与 DB AI
数据库·人工智能·sql
IvorySQL36 分钟前
开源数据库进校园| 11 月 3 日,IvorySQL 校园行走进浙大宁波校区:从 PostgreSQL 扩展生态到 AI 实战
数据库·postgresql·开源
福建佰胜张工1 小时前
深度解析 Allen-Bradley 150-B135NBDA 智能电机控制器:选型、参数与工业应用
运维·数据库·单片机·嵌入式硬件·安全·自动化
`Levie1 小时前
MySQL-数据库服务日志管理与数据闪回
数据库·mysql
香蕉大香蕉(06敬畏市场)1 小时前
MySQL 单点详解(01)
java·数据库·mysql·面试·博客·实习
LaughingZhu1 小时前
Product Hunt 热榜 2026-10-30:Empacer、Scape、Floot in ChatGPT
java·前端·数据库·人工智能·chatgpt·notion