systemd 服务文件始终启动和停止

systemd 服务文件始终启动和停止

为 botpress 创建了一个新的 systemd 服务如果我这样做

systemctl 启动 botpress.service

它启动所有 botpress 程序,然后立即运行 ExecStop 程序。如果我删除 ExecStop=.......,Systemctl 可以正常启动。

被调用的脚本运行正常。

cat /etc/systemd/system/botpress.service 
[Unit]
Description=botpess
After=docker.service

[Service]
Type=simple

TimeoutStartSec=5s
ExecStartPre=/bin/bash -c 'echo ">>>>>>>>>>> pre-starting botpress <<<<<<<<<<<<<<<"'
ExecStart=/etc/systemd/system/botpress.initd start
ExecStartPost=/bin/bash -c 'echo ">>>>>>>>>>> post-starting botpress <<<<<<<<<<<<<<<"'


ExecStop=/etc/systemd/system/botpress.initd stop
ExecStopPost=/bin/bash -c 'echo ">>>>>>>>>>> post-stopping botpress <<<<<<<<<<<<<<<"'

[Install]
WantedBy=multi-user.target

我可以将 botpress.initd 脚本放入 /etc/init.d 并让 systemctl 停止和启动,但不会在重启时重新启动。我想要做的只是调用一个简单的脚本,它可以根据需要启动和停止并在重启时启用

cat botpress.initd 
#! /bin/bash -x 
### BEGIN INIT INFO
# Provides:          my-service-name
# Required-Start:    $all
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:
# Short-Description: your description here
### END INIT INFO

BOTPRESS_COMPOSE_HOME="/home/pcooke/tmp/botpress/examples/docker-compose/"
COMPOSE_YAML="docker-compose-pro-nginx.yaml"
# Default-Start: 2 3 4 5
#OPTIONS="--verbose"

start() {
  echo "starting botpress"
  /usr/local/bin/docker-compose ${OPTIONS} -f ${COMPOSE_YAML} up -d  --build
}

stop() {
  echo "stopping botpress"
  /usr/local/bin/docker-compose ${OPTIONS} -f ${COMPOSE_YAML} down
}


cd ${BOTPRESS_COMPOSE_HOME}

case "$1" in
        start)
            start
            ;;

        stop)
            stop
            ;;
        restart)
            stop
            start
            ;;
        *)
            echo $"Usage: $0 {start|stop|restart}"
            exit 1

esac
root@pcooke-XPS-13-9370:/etc/systemd/system# journalctl -xe
Apr 07 21:20:50 pcooke-XPS-13-9370 botpress.initd[4029]: [263B blob data]
Apr 07 21:20:50 pcooke-XPS-13-9370 botpress.initd[4029]: Removing botpress-server ...
Apr 07 21:20:50 pcooke-XPS-13-9370 botpress.initd[4029]: Removing redis           ...
Apr 07 21:20:50 pcooke-XPS-13-9370 botpress.initd[4029]: Removing botpress-lang   ...
Apr 07 21:20:50 pcooke-XPS-13-9370 botpress.initd[4029]: Removing postgres        ...
Apr 07 21:20:50 pcooke-XPS-13-9370 botpress.initd[4029]: [274B blob data]
Apr 07 21:20:50 pcooke-XPS-13-9370 avahi-daemon[817]: Interface br-e6bd415385a8.IPv6 no longer relevant for mDNS.
Apr 07 21:20:50 pcooke-XPS-13-9370 avahi-daemon[817]: Leaving mDNS multicast group on interface br-e6bd415385a8.IPv6 with address fe80::42:9fff:fe45:b457.
Apr 07 21:20:50 pcooke-XPS-13-9370 avahi-daemon[817]: Interface br-e6bd415385a8.IPv4 no longer relevant for mDNS.
Apr 07 21:20:50 pcooke-XPS-13-9370 avahi-daemon[817]: Leaving mDNS multicast group on interface br-e6bd415385a8.IPv4 with address 172.27.0.1.
Apr 07 21:20:50 pcooke-XPS-13-9370 avahi-daemon[817]: Withdrawing address record for fe80::42:9fff:fe45:b457 on br-e6bd415385a8.
Apr 07 21:20:50 pcooke-XPS-13-9370 avahi-daemon[817]: Withdrawing address record for 172.27.0.1 on br-e6bd415385a8.
Apr 07 21:20:50 pcooke-XPS-13-9370 NetworkManager[849]: <info>  [1586319650.5374] device (br-e6bd415385a8): state change: activated -> unmanaged (reason 'unmanaged', sys-iface-state: 'removed')
Apr 07 21:20:50 pcooke-XPS-13-9370 dbus-daemon[824]: [system] Activating via systemd: service name='org.freedesktop.nm_dispatcher' unit='dbus-org.freedesktop.nm-dispatcher.service' requested by ':1.10' (u
Apr 07 21:20:50 pcooke-XPS-13-9370 gnome-shell[2943]: Removing a network device that was not added
Apr 07 21:20:50 pcooke-XPS-13-9370 systemd[1]: Starting Network Manager Script Dispatcher Service...
-- Subject: Unit NetworkManager-dispatcher.service has begun start-up
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
-- 
-- Unit NetworkManager-dispatcher.service has begun starting up.
Apr 07 21:20:50 pcooke-XPS-13-9370 gnome-shell[1161]: Removing a network device that was not added
Apr 07 21:20:50 pcooke-XPS-13-9370 dbus-daemon[824]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher'
Apr 07 21:20:50 pcooke-XPS-13-9370 systemd[1]: Started Network Manager Script Dispatcher Service.
-- Subject: Unit NetworkManager-dispatcher.service has finished start-up
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
-- 
-- Unit NetworkManager-dispatcher.service has finished starting up.
-- 
-- The start-up result is RESULT.
Apr 07 21:20:50 pcooke-XPS-13-9370 nm-dispatcher[4557]: req:1 'down' [br-e6bd415385a8]: new request (2 scripts)
Apr 07 21:20:50 pcooke-XPS-13-9370 nm-dispatcher[4557]: req:1 'down' [br-e6bd415385a8]: start running ordered scripts...
Apr 07 21:20:50 pcooke-XPS-13-9370 NetworkManager[849]: <info>  [1586319650.5626] devices removed (path: /sys/devices/virtual/net/br-e6bd415385a8, iface: br-e6bd415385a8)
Apr 07 21:20:50 pcooke-XPS-13-9370 systemd-resolved[641]: Server returned error NXDOMAIN, mitigating potential DNS violation DVE-2018-0001, retrying transaction with reduced feature level UDP.
Apr 07 21:20:50 pcooke-XPS-13-9370 bash[4641]: >>>>>>>>>>> post-stopping botpress <<<<<<<<<<<<<<<
Apr 07 21:20:51 pcooke-XPS-13-9370 systemd-resolved[641]: Server returned error NXDOMAIN, mitigating potential DNS violation DVE-2018-0001, retrying transaction with reduced feature level UDP.
Apr 07 21:20:51 pcooke-XPS-13-9370 systemd-resolved[641]: Server returned error NXDOMAIN, mitigating potential DNS violation DVE-2018-0001, retrying transaction with reduced feature level UDP.
Apr 07 21:20:51 pcooke-XPS-13-9370 systemd-resolved[641]: Server returned error NXDOMAIN, mitigating potential DNS violation DVE-2018-0001, retrying transaction with reduced feature level UDP.
Apr 07 21:20:51 pcooke-XPS-13-9370 systemd-resolved[641]: Server returned error NXDOMAIN, mitigating potential DNS violation DVE-2018-0001, retrying transaction with reduced feature level UDP.
Apr 07 21:20:51 pcooke-XPS-13-9370 systemd-resolved[641]: Server returned error NXDOMAIN, mitigating potential DNS violation DVE-2018-0001, retrying transaction with reduced feature level UDP.
Apr 07 21:20:51 pcooke-XPS-13-9370 systemd-resolved[641]: Server returned error NXDOMAIN, mitigating potential DNS violation DVE-2018-0001, retrying transaction with reduced feature level UDP.
Apr 07 21:20:53 pcooke-XPS-13-9370 systemd-resolved[641]: Server returned error NXDOMAIN, mitigating potential DNS violation DVE-2018-0001, retrying transaction with reduced fe
/////

将类型改为 onshot 并重启后

root@pcoo/ke-XPS-13-9370:/etc/systemd/system# systemctl start botpress
Job for botpress.service failed because a timeout was exceeded.
See "systemctl status botpress.service" and "journalctl -xe" for details.
 systemctl status botpress
● botpress.service - botpess
   Loaded: loaded (/etc/systemd/system/botpress.service; disabled; vendor preset: enabled)
   Active: failed (Result: timeout) since Wed 2020-04-08 19:14:33 PDT; 5min ago
  Process: 11561 ExecStopPost=/bin/bash -c echo ">>>>>>>>>>> post-stopping botpress <<<<<<<<<<<<<<<" (code=exited, status=0/SUCCESS)
  Process: 11026 ExecStart=/etc/systemd/system/botpress.initd start (code=killed, signal=TERM)
  Process: 11025 ExecStartPre=/bin/bash -c echo ">>>>>>>>>>> pre-starting botpress <<<<<<<<<<<<<<<" (code=exited, status=0/SUCCESS)
 Main PID: 11026 (code=killed, signal=TERM)

Apr 08 19:14:30 pcooke-XPS-13-9370 botpress.initd[11026]: Successfully tagged docker-compose_nginx:latest
Apr 08 19:14:30 pcooke-XPS-13-9370 botpress.initd[11026]: Creating postgres ...
Apr 08 19:14:30 pcooke-XPS-13-9370 botpress.initd[11026]: Creating redis    ...
Apr 08 19:14:30 pcooke-XPS-13-9370 botpress.initd[11026]: Creating botpress-lang ...
Apr 08 19:14:33 pcooke-XPS-13-9370 systemd[1]: botpress.service: Start operation timed out. Terminating.
Apr 08 19:14:33 pcooke-XPS-13-9370 systemd[1]: botpress.service: Main process exited, code=killed, status=15/TERM
Apr 08 19:14:33 pcooke-XPS-13-9370 botpress.initd[11026]: [90B blob data]
Apr 08 19:14:33 pcooke-XPS-13-9370 bash[11561]: >>>>>>>>>>> post-stopping botpress <<<<<<<<<<<<<<<
Apr 08 19:14:33 pcooke-XPS-13-9370 systemd[1]: botpress.service: Failed with result 'timeout'.
Apr 08 19:14:33 pcooke-XPS-13-9370 systemd[1]: Failed to start botpess.

新 journalctl-xe

journalctl -xe
Apr 08 19:14:32 pcooke-XPS-13-9370 systemd-resolved[641]: Server returned error NXDOMAIN, mitigating potential DNS violation DVE-2018-0001, retrying transaction with reduced feature level UDP.
Apr 08 19:14:32 pcooke-XPS-13-9370 systemd-resolved[641]: Server returned error NXDOMAIN, mitigating potential DNS violation DVE-2018-0001, retrying transaction with reduced feature level UDP.
Apr 08 19:14:32 pcooke-XPS-13-9370 kernel: eth0: renamed from vethfbd729e
Apr 08 19:14:32 pcooke-XPS-13-9370 NetworkManager[849]: <info>  [1586398472.3620] devices removed (path: /sys/devices/virtual/net/vethfbd729e, iface: vethfbd729e)
Apr 08 19:14:32 pcooke-XPS-13-9370 kernel: IPv6: ADDRCONF(NETDEV_CHANGE): vethaec5c13: link becomes ready
Apr 08 19:14:32 pcooke-XPS-13-9370 kernel: br-83455e030a4d: port 1(vethaec5c13) entered blocking state
Apr 08 19:14:32 pcooke-XPS-13-9370 kernel: br-83455e030a4d: port 1(vethaec5c13) entered forwarding state
Apr 08 19:14:32 pcooke-XPS-13-9370 NetworkManager[849]: <info>  [1586398472.3647] device (vethaec5c13): carrier: link connected
Apr 08 19:14:32 pcooke-XPS-13-9370 gnome-shell[2943]: Removing a network device that was not added
Apr 08 19:14:32 pcooke-XPS-13-9370 gnome-shell[1161]: Removing a network device that was not added
Apr 08 19:14:32 pcooke-XPS-13-9370 avahi-daemon[817]: Joining mDNS multicast group on interface br-83455e030a4d.IPv6 with address fe80::42:76ff:fe65:828.
Apr 08 19:14:32 pcooke-XPS-13-9370 avahi-daemon[817]: New relevant interface br-83455e030a4d.IPv6 for mDNS.
Apr 08 19:14:32 pcooke-XPS-13-9370 avahi-daemon[817]: Registering new address record for fe80::42:76ff:fe65:828 on br-83455e030a4d.*.
Apr 08 19:14:33 pcooke-XPS-13-9370 avahi-daemon[817]: Joining mDNS multicast group on interface veth9ba683a.IPv6 with address fe80::877:c1ff:fe8d:4ccf.
Apr 08 19:14:33 pcooke-XPS-13-9370 avahi-daemon[817]: New relevant interface veth9ba683a.IPv6 for mDNS.
Apr 08 19:14:33 pcooke-XPS-13-9370 avahi-daemon[817]: Registering new address record for fe80::877:c1ff:fe8d:4ccf on veth9ba683a.*.
Apr 08 19:14:33 pcooke-XPS-13-9370 kernel: eth0: renamed from vethbde55a8
Apr 08 19:14:33 pcooke-XPS-13-9370 NetworkManager[849]: <info>  [1586398473.3297] devices removed (path: /sys/devices/virtual/net/vethbde55a8, iface: vethbde55a8)
Apr 08 19:14:33 pcooke-XPS-13-9370 kernel: IPv6: ADDRCONF(NETDEV_CHANGE): veth1583c41: link becomes ready
Apr 08 19:14:33 pcooke-XPS-13-9370 kernel: br-83455e030a4d: port 3(veth1583c41) entered blocking state
Apr 08 19:14:33 pcooke-XPS-13-9370 kernel: br-83455e030a4d: port 3(veth1583c41) entered forwarding state
Apr 08 19:14:33 pcooke-XPS-13-9370 NetworkManager[849]: <info>  [1586398473.3324] device (veth1583c41): carrier: link connected
Apr 08 19:14:33 pcooke-XPS-13-9370 gnome-shell[1161]: Removing a network device that was not added
Apr 08 19:14:33 pcooke-XPS-13-9370 gnome-shell[2943]: Removing a network device that was not added
Apr 08 19:14:33 pcooke-XPS-13-9370 systemd[1]: botpress.service: Start operation timed out. Terminating.
Apr 08 19:14:33 pcooke-XPS-13-9370 systemd[1]: botpress.service: Main process exited, code=killed, status=15/TERM
Apr 08 19:14:33 pcooke-XPS-13-9370 botpress.initd[11026]: [90B blob data]
Apr 08 19:14:33 pcooke-XPS-13-9370 bash[11561]: >>>>>>>>>>> post-stopping botpress <<<<<<<<<<<<<<<
Apr 08 19:14:33 pcooke-XPS-13-9370 systemd[1]: botpress.service: Failed with result 'timeout'.
Apr 08 19:14:33 pcooke-XPS-13-9370 systemd[1]: Failed to start botpess.
-- Subject: Unit botpress.service has failed
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
-- 
-- Unit botpress.service has failed.
-- 
-- The result is RESULT.
Apr 08 19:14:34 pcooke-XPS-13-9370 avahi-daemon[817]: Joining mDNS multicast group on interface vethaec5c13.IPv6 with address fe80::60f4:dfff:fe0a:ff72.
Apr 08 19:14:34 pcooke-XPS-13-9370 avahi-daemon[817]: New relevant interface vethaec5c13.IPv6 for mDNS.
Apr 08 19:14:34 pcooke-XPS-13-9370 avahi-daemon[817]: Registering new address record for fe80::60f4:dfff:fe0a:ff72 on vethaec5c13.*.
Apr 08 19:14:34 pcooke-XPS-13-9370 avahi-daemon[817]: Joining mDNS multicast group on interface veth1583c41.IPv6 with address fe80::d4ce:bdff:fedc:6e74.
Apr 08 19:14:34 pcooke-XPS-13-9370 avahi-daemon[817]: New relevant interface veth1583c41.IPv6 for mDNS.
Apr 08 19:14:34 pcooke-XPS-13-9370 avahi-daemon[817]: Registering new address record for fe80::d4ce:bdff:fedc:6e74 on veth1583c41.*.
Apr 08 19:14:37 pcooke-XPS-13-9370 systemd-resolved[641]: Server returned error NXDOMAIN, mitigating potential DNS violation DVE-2018-0001, retrying transaction with reduced feature level UDP.
Apr 08 19:14:37 pcooke-XPS-13-9370 systemd-resolved[641]: Server returned error NXDOMAIN, mitigating potential DNS violation DVE-2018-0001, retrying transaction with reduced feature level UDP.
Apr 08 19:14:37 pcooke-XPS-13-9370 systemd-resolved[641]: Server returned error NXDOMAIN, mitigating potential DNS violation DVE-2018-0001, retrying transaction with reduced feature level UDP.
Apr 08 19:14:37 pcooke-XPS-13-9370 systemd-resolved[641]: Server returned error NXDOMAIN, mitigating potential DNS violation DVE-2018-0001, retrying transaction with reduced feature level UDP.
Apr 08 19:14:37 pcooke-XPS-13-9370 systemd-resolved[641]: Server returned error NXDOMAIN, mitigating potential DNS violation DVE-2018-0001, retrying transaction with reduced feature level UDP.
Apr 08 19:14:37 pcooke-XPS-13-9370 systemd-resolved[641]: Server returned error NXDOMAIN, mitigating potential DNS violation DVE-2018-0001, retrying transaction with reduced feature level UDP.
Apr 08 19:17:01 pcooke-XPS-13-9370 CRON[11653]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 08 19:17:01 pcooke-XPS-13-9370 CRON[11654]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Apr 08 19:17:01 pcooke-XPS-13-9370 CRON[11653]: pam_unix(cron:session): session closed for user root

有趣的是:启用 botpress 并重启后,除了 nginx 容器之外的所有容器都启动了

 systemctl status botpress
● botpress.service - botpess
   Loaded: loaded (/etc/systemd/system/botpress.service; enabled; vendor preset: enabled)
   Active: failed (Result: timeout) since Wed 2020-04-08 19:27:24 PDT; 2min 52s ago
  Process: 2490 ExecStopPost=/bin/bash -c echo ">>>>>>>>>>> post-stopping botpress <<<<<<<<<<<<<<<" (code=exited, status=0/SUCCESS)
  Process: 1677 ExecStart=/etc/systemd/system/botpress.initd start (code=killed, signal=TERM)
  Process: 1676 ExecStartPre=/bin/bash -c echo ">>>>>>>>>>> pre-starting botpress <<<<<<<<<<<<<<<" (code=exited, status=0/SUCCESS)
 Main PID: 1677 (code=killed, signal=TERM)

Apr 08 19:27:20 pcooke-XPS-13-9370 botpress.initd[1677]: Successfully tagged docker-compose_nginx:latest
Apr 08 19:27:20 pcooke-XPS-13-9370 botpress.initd[1677]: Starting botpress-lang ...
Apr 08 19:27:20 pcooke-XPS-13-9370 botpress.initd[1677]: Starting redis         ...
Apr 08 19:27:20 pcooke-XPS-13-9370 botpress.initd[1677]: Starting postgres      ...
Apr 08 19:27:23 pcooke-XPS-13-9370 botpress.initd[1677]: [163B blob data]
Apr 08 19:27:24 pcooke-XPS-13-9370 systemd[1]: botpress.service: Start operation timed out. Terminating.
Apr 08 19:27:24 pcooke-XPS-13-9370 systemd[1]: botpress.service: Main process exited, code=killed, status=15/TERM
Apr 08 19:27:24 pcooke-XPS-13-9370 bash[2490]: >>>>>>>>>>> post-stopping botpress <<<<<<<<<<<<<<<
Apr 08 19:27:24 pcooke-XPS-13-9370 systemd[1]: botpress.service: Failed with result 'timeout'.
Apr 08 19:27:24 pcooke-XPS-13-9370 systemd[1]: Failed to start botpess.

当我只有 ExecStart 时,我没有得到标记 NGINX 的提示。这是我的 compse 文件。

cat docker-compose-pro-nginx.yaml 
version: '3.7'

services:
  botpress:
    build:
      context: ./botpress
      dockerfile: Dockerfile
    image: botpress-server
    container_name: botpress-server
    command: bash -c "export AUTO_MIGRATE=true; ./bp --auto-update"
    expose:
      - 3000
    environment:
      - DATABASE_URL=postgres://postgres:secretpw@postgres:5435/botpress_db
      - REDIS_URL=redis://redis:6379?password=redisPassword
      - BP_MODULE_NLU_DUCKLINGURL=http://botpress_lang:8000
      - BP_MODULE_NLU_LANGUAGESOURCES=[{"endpoint":"http://botpress_lang:3100"}]
      - EXTERNAL_URL=https://botpressdev.sf.frb.org
      - CLUSTER_ENABLED=true
      - PRO_ENABLED=true
      - BP_PRODUCTION=true
      - BPFS_STORAGE=database
      - BP_LICENSE_KEY=123456789
    depends_on:
      - botpress_lang
      - postgres
      - redis
    volumes:
      - /opt/botpress-data/botpress:/botpress/data

  botpress_lang:
    build:
      context: ./botpress
      dockerfile: Dockerfile
    image: botpress-lang
    container_name: botpress-lang
    command: bash -c "./duckling -p 8000 & ./bp lang --langDir /botpress/lang --port 3100"
    expose:
      - 3100
      - 8000
    volumes:
      - /opt/botpress-data/language:/botpress/lang

  postgres:
    image: postgres:11.2-alpine
    expose:
      - 5435
    container_name: postgres
    environment:
      PGPORT: 5435
      POSTGRES_DB: botpress_db
      POSTGRES_PASSWORD: secretpw
      POSTGRES_USER: postgres
    volumes:
      - /opt/botpress-data/postgres/data:/var/lib/postgresql/data

  redis:
    image: redis:5.0.5-alpine
    expose:
      - 6379
    container_name: redis
    command: redis-server --requirepass redisPassword
    volumes:
      - redisdata:/data

  nginx:
    container_name: nginx
    build: ./nginx
    ports:
      - 80:80
    command: nginx -g 'daemon off';
    depends_on:
      - botpress

volumes:
  redisdata:

答案1

听起来好像是一个“一次性”命令。

这意味着您使用 systemd 启动进程,然后一旦进程启动并运行,systemd 就会接管它并在后台运行它。进程 ID 只是启动它时需要的,而不是让它保持运行。

为了测试,请运行并在此处显示结果: systemctl status botpress.service- 如果这不是包的实际名称,请修改“botpress”。

要在启动时将该进程作为 systemd 单元启动,请sytemctl enable botpress.service在下次重新启动之前的任何时间运行。现在就可以了。

答案2

好吧,我感觉自己就像一个没有经验的刚毕业的大学生。

与供应商交谈后,我所需要做的就是将“restart: always”添加到我的撰写文件中,然后让 docker 完成剩下的工作。

    services:
      botpress:
        build:
          context: ./botpress
          dockerfile: Dockerfile
        image: botpress-serve
        restart: always
        container_name: botpress-server
        command: bash -c "export AUTO_MIGRATE=true; ./bp --auto-update"

相关内容