你的位置:首页 > 互联网IT

Systemctl,进程守护,自动启动,自启,进程保护,进程守护管理器,进程管理,Systemctl-让Linux程序在后台保持运行

系统:Linux

软件:Systemctl

功能:进程守护,进程保护,后台保持运行

本地下载:

必应搜索:”Systemctl“



简介

前言

在 Linux 系统中,我们经常需要一些服务或应用程序在系统启动后自动运行。在自建RustDesk服务器这篇文章中,霜狐讲述了三种让程序在后台持续运行的方法



自建Rustdesk注册/中继服务器

准备工作 Rustdesk是一款开源的远程桌面软件,支持安卓/Windows/Mac/Linux/IOS全平台使用,还可以搭建自己的中继服务器。本文将带大家……

而使用Systemd是一个不错的选择,不过配置起来略显麻烦,所以单独写成一篇教程。


SystemctlSystemd

Systemctl是 Systemd 的主命令,用于操作Systemd。Systemd 是一个系统和服务管理器,它被设计用来作为操作系统的初始化系统(init system)。它为操作系统提供了一个统一的方式去启动、停止以及管理各种服务。


Systemctl 基础命令

查看所有服务的状态:


systemctl list-units --type=service

启动服务:


systemctl start <service-name>

停止服务:


systemctl stop <service-name>

重启服务:


systemctl restart <service-name>

查看服务状态:


systemctl status <service-name>

启用服务(开机启动):


systemctl enable <service-name>

禁用服务:


systemctl disable <service-name>

创建服务

要让一个程序在后台持续运行,我们需要在/etc/systemd/system/目录下创建一个.service文件,并编写服务单元文件.


假设脚本名为myapp.py,位于/home/user/myapp目录下。


创建一个.service文件


nano /etc/systemd/system/myapp.service

添加以下内容,然后Ctrl+X,Y保存。


[Unit]

Description=My Application Service

After=network.target


[Service]

Type=forking

ExecStart=/home/user/myapp/myapp


[Install]

WantedBy=multi-user.target

Description: 服务描述。


After: 该服务要在什么服务启动后启动,这里指定服务在网络服务启动之后启动。


Type,定义启动时的进程行为。


ExecStart: 启动当前服务的命令。


WantedBy: 指定服务启动级别。


更多

启动服务

加载新的服务


systemctl daemon-reload

启动服务


systemctl start myapp.service

查看服务状态


systemctl status myapp.service

开启服务开机自启


systemctl enable myapp.service


QQ截图20241009191821.jpg Systemctl,进程守护,自动启动,自启,进程保护,进程守护管理器,进程管理,Systemctl-让Linux程序在后台保持运行 互联网IT


参考来源:https://www.frostfox.cn/blog/solution/2024/systemctl.html




  • 发表评论
  • 查看评论
【暂无评论!】

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。