systemd 服务的自动安装脚本
以下脚本将安装 systemd 服务文件到 /etc/systemd/system/ 并启用它(即启动时启动)并立即启动它。
install_systemd_service.sh
#!/bin/bash
# 此脚本安装并启用/启动 systemd 服务
export NAME=MyService
# 创建服务文件
cat >/etc/systemd/system/${NAME}.service <<EOF
# 在此添加 SYSTEMD 服务文件内容
EOF
# 启用并启动服务
systemctl enable --now ${NAME}为了为你的 systemd 服务修改脚本,将 MyService 替换为你所需的服务名称在
install_systemd_service_set_name.sh
export NAME=MyService并在以下位置插入你的 .service 文件内容
install_systemd_service_placeholder.sh
# 在此添加 SYSTEMD 服务文件内容完整示例
以下完整示例安装名为 MyService 的 systemd 服务,该服务运行 /usr/bin/python3 myscript.py:
install_systemd_service_example.sh
#!/bin/bash
# 此脚本安装并启用/启动 systemd 服务
export NAME=MyService
# 创建服务文件
cat >/etc/systemd/system/${NAME}.service <<EOF
[Unit]
Description=MyService
[Service]
Type=oneshot
ExecStart=/usr/bin/python3 myscript.py
WorkingDirectory=/opt/myservice
[Install]
WantedBy=multi-user.target
EOF
# 启用并启动服务
systemctl enable --now ${NAME}Check out similar posts by category:
Systemd
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow