个人在这些方面完全是一个很老的小白,和container里面进程启动的方式不一样,当前项目需要支持在非container环境下启动独立进程。
Unit file学习上比较喜欢的网址
https://www.cnblogs.com/usmile/p/13065594.html
当前项目比较有意思的是 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
来一堆解释
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