linux设置程序在开机时自启动

​常见的有三种方式:
1、/etc/rc.local文件中添加自启动命令

对于某些没有使用systemd的Linux发行版,可以在/etc/rc.local文件中添加自启动命令。请确保该文件具有可执行权限。例如,在/etc/rc.local文件中添加以下内容:

bash 复制代码
/path/to/your/program

然后,重启系统以使更改生效。
2、使用cron

可以使用cron的@reboot事件来在系统启动时运行程序。

首先,打开当前用户的crontab配置文件:crontab -e

然后,在文件末尾添加以下内容:

bash 复制代码
@reboot /path/to/your/program

保存并退出编辑器。下次系统启动时,指定的程序将自动运行。
3、使用systemd(官方推荐)

对于使用systemd作为初始化系统的Linux发行版(如:CentOS 7、Ubuntu 16.04及更高版本等),可以通过创建一个systemd服务来实现自启动。步骤如下:

step1、创建一个名为your_service.service的文件,如:/etc/systemd/system/your_service.service,并编辑它:

bash 复制代码
[Unit]
Description=Your Service Description
After=network.target
[Service]
ExecStart=/path/to/your/program
Restart=always
[Install]
WantedBy=multi-user.target

step2、为your_service.service文件设置正确的权限:

bash 复制代码
sudo chmod 644 /etc/systemd/system/your_service.service

step3、重新加载systemd配置:

bash 复制代码
sudo systemctl daemon-reload

step4、启用服务:

bash 复制代码
sudo systemctl enable your_service.service

其它使用指令:

1、禁用服务(如果需要):

bash 复制代码
sudo systemctl disable your_service.service

2、启动服务:

bash 复制代码
sudo systemctl start your_service.service

3、停止服务:

bash 复制代码
sudo systemctl stop your_service.service

4、查看服务状态:

bash 复制代码
sudo systemctl status your_service.service
相关推荐
_下雨天.3 小时前
LVS负载均衡
服务器·负载均衡·lvs
小成202303202655 小时前
Linux高级02
linux·开发语言
mounter6255 小时前
【硬核前沿】CXL 深度解析:重塑数据中心架构的“高速公路”,Linux 内核如何应对挑战?-- CXL 协议详解与 LSF/MM 最新动态
linux·服务器·网络·架构·kernel
++==6 小时前
Linux 进程间通信与线程同步技术详解:IPC 机制、线程 API、同步工具与经典同步问题
linux
特长腿特长6 小时前
centos、ubantu系列机的用户和用户组的结构是什么?具体怎么配置?用户组权限怎么使用?这篇文章持续更新,帮助你复习linux的基础知识
linux·运维·centos
zzzyyy5386 小时前
Linux环境变量
linux·运维·服务器
pluvium276 小时前
记对 xonsh shell 的使用, 脚本编写, 迁移及调优
linux·python·shell·xonsh
无级程序员6 小时前
centos7 安装 llvm-toolset-7-clang出错的问题解决
linux·centos
kebeiovo7 小时前
atomic原子操作实现无锁队列
服务器·c++
赛博云推-Twitter热门霸屏工具7 小时前
Twitter运营完整流程:从0到引流获客全流程拆解(2026)
运维·安全·自动化·媒体·twitter