项目中碰到的比较有意思的systemd Unit File

个人在这些方面完全是一个很老的小白,和container里面进程启动的方式不一样,当前项目需要支持在非container环境下启动独立进程。

Unit file学习上比较喜欢的网址

https://www.cnblogs.com/usmile/p/13065594.html

systemd.unit

当前项目比较有意思的是 your_ownd.service.in文件

[Unit]
Description = your own server

[Service]
Type = notify
User = xyz
Group = xyz
WatchdogSec = 60
Environment = YOUR_ENV1 =your_own
Environment = YOUR_ENV2=/tmp/your_own
Environment = YOUR_ENV3_PID_PATH=/tmp/your_own-server.pid
ExecStart = @bindir@/your_ownd
ExecStopPost = /bin/sh -c 'rm -rf /tmp/your_own /tmp/your_own-server.pid ; echo "your_ownd post cleanup finished."'
[Install]
RequiredBy = xyz-ready.target

由于是初学,有意思的点觉得有3

1 Environment 可以指定多个

2 sh命令的执行

3 当前为service.in文件,@bindir@ 最终提供给集成方时需要替换为定制后的路径

第3点,经过专家指导,其实背景知识不少,刚好当前的项目是基于automake tool

来一堆解释

daemon

Install systemd service using autotools - Stack Overflow

Installing systemd Service Files

At the build installation time (e.g. make install during package build), packages are recommended to install their systemd unit files in the directory returned by pkg-config systemd --variable=systemdsystemunitdir (for system services) or pkg-config systemd --variable=systemduserunitdir (for user services). This will make the services available in the system on explicit request but not activate them automatically during boot. Optionally, during package installation (e.g. rpm -i by the administrator), symlinks should be created in the systemd configuration directories via the enable command of the systemctl(1) tool to activate them automatically on boot.

Packages using autoconf(1) are recommended to use a configure script excerpt like the following to determine the unit installation path during source configuration:

PKG_PROG_PKG_CONFIG
AC_ARG_WITH([systemdsystemunitdir],
     [AS_HELP_STRING([--with-systemdsystemunitdir=DIR], [Directory for systemd service files])],,
     [with_systemdsystemunitdir=auto])
AS_IF([test "x$with_systemdsystemunitdir" = "xyes" -o "x$with_systemdsystemunitdir" = "xauto"], [
     def_systemdsystemunitdir=$($PKG_CONFIG --variable=systemdsystemunitdir systemd)

     AS_IF([test "x$def_systemdsystemunitdir" = "x"],
   [AS_IF([test "x$with_systemdsystemunitdir" = "xyes"],
    [AC_MSG_ERROR([systemd support requested but pkg-config unable to query systemd package])])
    with_systemdsystemunitdir=no],
   [with_systemdsystemunitdir="$def_systemdsystemunitdir"])])
AS_IF([test "x$with_systemdsystemunitdir" != "xno"],
      [AC_SUBST([systemdsystemunitdir], [$with_systemdsystemunitdir])])
AM_CONDITIONAL([HAVE_SYSTEMD], [test "x$with_systemdsystemunitdir" != "xno"])

注: 是在configure.ac文件中添加上面的一段,然后主要关注 HAVE_SYSTEMD 这个宏

This snippet allows automatic installation of the unit files on systemd machines, and optionally allows their installation even on machines lacking systemd. (Modification of this snippet for the user unit directory is left as an exercise for the reader.)

Additionally, to ensure that make distcheck continues to work, it is recommended to add the following to the top-level Makefile.am file in automake(1)-based projects:

AM_DISTCHECK_CONFIGURE_FLAGS = \
  --with-systemdsystemunitdir=$$dc_install_base/$(systemdsystemunitdir)

Finally, unit files should be installed in the system with an automake excerpt like the following:

if HAVE_SYSTEMD
systemdsystemunit_DATA = \
  foobar.socket \
  foobar.service
endif

注:上面这两段放在makefile里面

我们自己的项目中稍稍多了一个流程就是替换路径

if HAVE_SYSTEMD
AM_DISTCHECK_CONFIGURE_FLAGS = \
  --with-systemdsystemunitdir=$$dc_install_base/$(systemdsystemunitdir)

systemdsystemunit_DATA = your_ownd.service

your_ownd.service: $(top_srcdir)/systemd/your_ownd.service.in
	sed -e 's![@]bindir[@]!$(bindir)!g' \
         $< > $@
endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值