AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1065129
Accepted
DenisZ
DenisZ
Asked: 2021-05-31 05:08:57 +0800 CST2021-05-31 05:08:57 +0800 CST 2021-05-31 05:08:57 +0800 CST

如何在 systemd 中创建自定义服务?

  • 772

在多台机器上运行 Fedora 33/34。我成功激活rc-local.service了它工作正常。我有几个需要运行的其他脚本,/etc/init.d/并且我创建了与 类似的服务文件rc-local.service,但我无法启用它,而 rc-local.service 可以毫无问题地启用/禁用/盯着/停止。
我知道,我有一个选项(我目前使用)从rc.local文件中调用这些脚本,但宁愿将它们作为服务单独调用。所以我的服务文件如下所示:
#cat net-scr1.service

[Unit]
Description=Network check service
ConditionFileIsExecutable=/etc/init.d/net-scr1
After=network.target 

[Service]
Type=forking
ExecStart=/etc/init.d/net-scr1 start
TimeoutSec=3
ExecStop=/etc/init.d/net-scr1 stop
RemainAfterExit=yes
GuessMainPID=no
StandardOutput=tty

[Install]
 WantedBy=multi-user.target

systemctl enable net-scr1

Synchronizing state of net-scr1.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable net-scr1
service net-scr1 does not support chkconfig

我将脚本移出init.d考虑可能 systemd 假设它是一个旧sysv脚本,但没有改变。我的脚本中应该有什么才能正常工作?
我的脚本看起来像:

#!/bin/bash
[email protected]
[email protected]
dv0=p1p1
ipdv0=`ifconfig $dv0 | grep "inet " | awk '{print $2}'`
if [ "$1" = "stop" ]; then
  cat $msg | mail $EMFR -s "$srv-shut_down-ip-$ipdv0" $eml
fi
if [ "$1" = "start" ]; then
  cat $msg | mail $EMFR -s "$srv-booted_up-ip-$ipdv0" $eml  

fi

我应该包含旧 sysv 系统中的函数还是其他什么?谢谢

scripting bash systemd systemd-service
  • 2 2 个回答
  • 264 Views

2 个回答

  • Voted
  1. Haim Cohen
    2021-05-31T05:17:40+08:002021-05-31T05:17:40+08:00

    看到这个

    1. cd /etc/systemd/系统
    2. 创建一个名为 your-service.service 的文件并包含以下内容:
    [Unit]
    Description=<description about this service>
    
    [Service]
    User=<user e.g. root>
    WorkingDirectory=<directory_of_script e.g. /root>
    ExecStart=<script which needs to be executed>
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
    
    • 0
  2. Best Answer
    DenisZ
    2021-05-31T06:32:16+08:002021-05-31T06:32:16+08:00

    我找到了解决方案。我在脚本顶部包含以下 3 行:
    head /etc/init.d/net-scr1

    # net-scr1          Start/Stop the net script1
    # chkconfig: 345 90 10
    # description: net-scr1 sends alert on reboot
    

    现在systemctl enable net-scr1 工作正常

    • 0

相关问题

  • 知道任何适用于 Windows 的快速可编写脚本的 ftp 客户端吗?[关闭]

  • 如果同一个任务已经在运行,如何防止计划任务运行?

  • 需要从 Batch For 循环中排除特定结果

  • Bash 脚本:要求脚本以 root 身份运行(或使用 sudo)

  • 从命令行删除旧的(非引导)Windows Vista 目录

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve