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
相关推荐
马立杰1 小时前
H3CNE-33-BGP
运维·网络·h3cne
Mason Lin1 小时前
2025年1月22日(网络编程 udp)
网络·python·udp
字节全栈_rJF2 小时前
概述、 BGP AS 、BGP 邻居、 BGP 更新源 、BGP TTL 、BGP路由表、 BGP 同步
网络·智能路由器·php
EchoToMe2 小时前
电信传输基本理论/5G网络层次架构——超三万字详解:适用期末考试/考研/工作
网络·5g·架构
深度Linux2 小时前
Linux网络编程中的零拷贝:提升性能的秘密武器
linux·linux内核·零拷贝技术
doubt。2 小时前
8.攻防世界Web_php_wrong_nginx_config
网络·安全·web安全·网络安全
没有名字的小羊3 小时前
Cyber Security 101-Build Your Cyber Security Career-Security Principles(安全原则)
运维·网络·安全
m0_465215793 小时前
TCP & UDP Service Model
服务器·网络·tcp/ip
esmember3 小时前
电路研究9.2.6——合宙Air780EP中HTTP——HTTP GET 相关命令使用方法研究
网络·网络协议·http·at指令
xianwu5434 小时前
反向代理模块jmh
开发语言·网络·数据库·c++·mysql