linux 进程开机自启

linux 进程开机自启

在Linux系统中,要让一个进程在开机时自动启动,可以使用systemd服务。

以下是创建自启动服务的步骤和示例代码:

创建一个新的systemd服务单元文件。

文件通常位于/etc/systemd/system/目录下,以.service作为文件扩展名。例如,创建一个名为my_service.service的文件:

sudo nano /etc/systemd/system/my_service.service

在该文件中添加以下内容:

**[Unit]
Description=My Custom Service
After=network.target

Service

Type=simple
ExecStart=/path/to/your/application

Install

WantedBy=multi-user.target**

将/path/to/your/application替换为你的应用程序的完整路径。

重新加载systemd管理器配置。sudo systemctl daemon-reload

启用服务以在开机时自动启动。sudo systemctl enable my_service.service

(可选)立即启动服务而不需要重启。sudo systemctl start my_service.service

确保你的应用程序具有执行权限。如果没有,使用chmod命令添加执行权限:

sudo chmod +x /path/to/your/application

现在,每次系统启动时,my_service.service服务都会自动运行指定的应用程序。

bash 复制代码
:~/Documents/IsstaGao/pwr_mngr$
:~/Documents/IsstaGao/pwr_mngr$ vim /etc/systemd/system/my_service.service
:~/Documents/IsstaGao/pwr_mngr$ sudo vim /etc/systemd/system/issta_service.service
[sudo] password for mogo:
:~/Documents/IsstaGao/pwr_mngr$ sudo vim /etc/systemd/system/issta_service.service
:~/Documents/IsstaGao/pwr_mngr$ pwd
/data/mogo/Documents/IsstaGao/pwr_mngr
:~/Documents/IsstaGao/pwr_mngr$ sudo vim /etc/systemd/system/issta_service.service
:~/Documents/IsstaGao/pwr_mngr$ sudo systemctl daemon-reload
:~/Documents/IsstaGao/pwr_mngr$ sudo systemctl enable issta_service.service
Created symlink /etc/systemd/system/multi-user.target.wants/issta_service.service → /etc/systemd/system/issta_service.service.
:~/Documents/IsstaGao/pwr_mngr$ sudo systemctl start issta_service.service
:~/Documents/IsstaGao/pwr_mngr$ ps -aux | grep client
root        8843  0.0  0.0  10432   540 ?        Ssl  16:15   0:00 /data/mogo/Documents/IsstaGao/pwr_mngr/client -s 192.168.1.102 -p 9039
mogo        8857  0.0  0.0   8572   652 pts/0    S+   16:16   0:00 grep --color=auto client
相关推荐
摇滚侠6 分钟前
CentOS 7 Linux 离线安装 Docker:离线安装包、依赖文件、安装步骤
linux·docker·centos
小鸡脚来咯9 分钟前
怎么配置主机名映射?
linux·windows·macos
Zzqqads9 分钟前
vnc连接问题:Timed out waiting for a response from the computer
服务器
咖啡の猫14 分钟前
TypeScript基本类型
linux·ubuntu·typescript
玖疯子17 分钟前
JavaScript性能优化实战的技术文章大纲
服务器
DeeplyMind17 分钟前
第4章:DebugFS 安全性和并发控制
linux·驱动开发·debugfs
CS Beginner26 分钟前
【Linux】快速配置wifi和SSH服务
linux·运维·ssh
become__better26 分钟前
判断ceph osd 节点磁盘异常
linux·运维·ceph
JavaWizard-M30 分钟前
centos7官方下载链接
linux·centos
我在人间贩卖青春1 小时前
Unix和Linux简史及标准化
linux·unix