Linux笔记-使用systemd管理进程

背景

程序需要长期运行,写一个进程管理程序啥的,有成本,利用systemd(system daemon)可以使得进程能够快速管理起来。

过程

① 首先构造一个shell。把需要加载的环境变量啥的,都写上去,然后进行调用,我这里以tcsh为了。

构造xxweb.sh

bash 复制代码
vim /home/xx/xxweb.sh

xxweb.sh内容

bash 复制代码
#!/bin/tcsh
source /home/xx/xx.cshrc.xxx

/home/xx/xxServer

切换root用户,赋予权限

bash 复制代码
chmod 777 /home/xxx/xxweb.sh

② 新增xxweb.service

bash 复制代码
vim /etc/systemd/system/xxweb.service

xxweb.service内容如下:

bash 复制代码
[Unit]
Description=xxweb
After=network.target

[Service]
ExecStart=/home/xx/xxweb.sh
Restart=always
RestartSec=5
User=xx
Environment=SYSTEM_LOG_LEVEL=debug

[Install]
WantedBy=default.target

重新加载systemd

bash 复制代码
systemctl daemon-reload

③ 启动

切换root用户

bash 复制代码
systemctl start xxweb.service

查询状态

bash 复制代码
systemctl status xxweb.service

其他细节

① 如果要使用bash需要修改shell文件的开头

bash 复制代码
#!/bin/bash

② 设置为开机启动,需要切换root用户然后

bash 复制代码
systemctl enable xxweb.service
相关推荐
NamoAmitabha1282 小时前
libpng12-0 1.2.54-1ubuntu1.1 (amd64 binary) in ubuntu xenial
linux·ubuntu20.4
Madison-No72 小时前
搭建项目测试环境
linux·运维·服务器·python
吴声子夜歌3 小时前
Shell编程实例——编写安全的shell脚本(二)
linux·运维·shell
悠悠121384 小时前
微服务通信别只看性能:REST 与 gRPC 的 6 个选型判断和一次 502 故障复盘
运维·服务器
xiaoye-duck5 小时前
《Linux 网络编程》深入理解 IP 协议(二):网段划分、私有 IP 与 NAT 地址转换
linux·网络·ip
zx_741484815 小时前
【Linux入门】Shell 函数、正则表达式与文本处理:cut 与 awk
linux·运维·正则表达式
zjxtxdy5 小时前
SPI通信协议
笔记·单片机
Zenova EdgeOS6 小时前
Linux dmesg 工业边缘实战:内核日志过滤、持久化与故障定位
linux·运维·边缘计算·工业边缘·dmesg·内核日志
金士顿6 小时前
System.Text.Json Source Generator 深度解析:为什么 Native AOT 不喜欢反射
linux·asp.net core·arm64·net·native aot
wdfk_prog7 小时前
ROS教程08:从 TransportTCP::connect() 追到 TCPROS Connection Header、序列化与 Socket 数据传输
运维·缓存·docker·容器·ros