RedHat 的启动/关闭脚本

RedHat 的启动/关闭脚本

我正在尝试为我的应用程序创建启动/关闭脚本,但我没有这方面的经验,所以我想从屏幕开始(我将其称为“test_screen”)。

首先,我想我只会在 /etc/init.d 中创建脚本。我从这里得到了灵感——https://unix.stackexchange.com/a/20361/29677

模拟我的应用程序的基本想法是使用

  • screen -d -m -S test_screen启动
  • screen -S test_screen -X quit用于关机
  • 以及screen –list状态(某种)

但是当我尝试时/etc/init.d/test_screen start我得到了

Reloading systemd:                                         [  OK  ]
Starting test_screen (via systemctl):  Failed to start test_screen.service: Unit not found.
                                                       [FAILED]

所以看来我必须创建单位。

我尝试过https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/sect-Managing_Services_with_systemd-Unit_Files.html,所以我的单位文件位于/etc/systemd/system

# cat test_screen.service
[Unit]
Description=Testing `screen` service

[Service]
Type=simple
ExecStart=/bin/screen -d -m -S test_screen
ExecStop=/bin/screen -S test_screen -X quit
Environment=
Restart=always

[Install]
WantedBy=default.target

第一个问题是,我应该有/etc/init.d/test_screen start还是ExecStart没有?肯定不会是一班人...

无论如何,它没有运行。在/var/log/messages我看到

Sep 19 10:54:58 somehostname systemd: Started Testing `screen` service.
Sep 19 10:54:58 somehostname systemd: Starting Testing `screen` service...
Sep 19 10:54:58 somehostname systemd: test_screen.service holdoff time over, scheduling restart.
Sep 19 10:54:58 somehostname systemd: Started Testing `screen` service.
Sep 19 10:54:58 somehostname systemd: Starting Testing `screen` service...
Sep 19 10:54:58 somehostname systemd: test_screen.service holdoff time over, scheduling restart.
Sep 19 10:54:58 somehostname systemd: Started Testing `screen` service.
Sep 19 10:54:58 somehostname systemd: Starting Testing `screen` service...
Sep 19 10:54:59 somehostname systemd: test_screen.service holdoff time over, scheduling restart.
Sep 19 10:54:59 somehostname systemd: Started Testing `screen` service.
Sep 19 10:54:59 somehostname systemd: Starting Testing `screen` service...
Sep 19 10:54:59 somehostname systemd: test_screen.service holdoff time over, scheduling restart.
Sep 19 10:54:59 somehostname systemd: Started Testing `screen` service.
Sep 19 10:54:59 somehostname systemd: Starting Testing `screen` service...
Sep 19 10:54:59 somehostname systemd: test_screen.service holdoff time over, scheduling restart.
Sep 19 10:54:59 somehostname systemd: start request repeated too quickly for test_screen.service
Sep 19 10:54:59 somehostname systemd: Failed to start Testing `screen` service.
Sep 19 10:54:59 somehostname systemd: Unit test_screen.service entered failed state.
Sep 19 10:54:59 somehostname systemd: test_screen.service failed.

如何找到进入失败状态的原因?欢迎所有建议。

答案1

看来,我解决了它screen......

首先我删除了该Restart=always参数。

为了使其工作,我还必须添加RemainAfterExit=True.

对于旧的初始化脚本,代码的位置很清楚。我真的不知道放置其余代码的合适位置。我应该简单地从 调用我的脚本吗ExecStart=


我在学习过程中的额外发现。

地位

有一个很好的问题(我的意思是我也感兴趣),如何实现状态检查 -systemd 自定义状态消息?

简短的回答是,它(以某种方式)开箱即用=您不需要太在意。

因此,对于我的 test_screen 服务,我可以调用systemctl status test_screen.service,我会得到

● test_screen.service - Testing `screen` service
   Loaded: loaded (/etc/systemd/system/test_screen.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2017-09-20 12:48:34 CEST; 1s ago
  Process: 36633 ExecStart=/bin/screen -d -m -S test_screen (code=exited, status=0/SUCCESS)
 Main PID: 36634 (screen)
   CGroup: /system.slice/test_screen.service
           ├─36634 /bin/SCREEN -d -m -S test_screen
           └─36635 /bin/sh

Sep 20 12:48:34 somehostname systemd[1]: Starting Testing `screen` service...
Sep 20 12:48:34 somehostname systemd[1]: Started Testing `screen` service.

...奇怪的是,当我停止它时,我的状态失败了:

● test_screen.service - Testing `screen` service
   Loaded: loaded (/etc/systemd/system/test_screen.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Wed 2017-09-20 12:51:00 CEST; 1s ago
  Process: 36805 ExecStop=/bin/screen -S test_screen -X quit (code=exited, status=0/SUCCESS)
  Process: 36633 ExecStart=/bin/screen -d -m -S test_screen (code=exited, status=0/SUCCESS)
 Main PID: 36634 (code=exited, status=1/FAILURE)

Sep 20 12:48:34 somehostname systemd[1]: Starting Testing `screen` service...
Sep 20 12:48:34 somehostname systemd[1]: Started Testing `screen` service.
Sep 20 12:51:00 somehostname systemd[1]: Stopping Testing `screen` service...
Sep 20 12:51:00 somehostname systemd[1]: test_screen.service: main process exited, code=exited, status=1/FAILURE
Sep 20 12:51:00 somehostname systemd[1]: Stopped Testing `screen` service.
Sep 20 12:51:00 somehostname systemd[1]: Unit test_screen.service entered failed state.
Sep 20 12:51:00 somehostname systemd[1]: test_screen.service failed.

克服我必须拥有的问题RemainAfterExit=True(因为我尝试了该建议Type=forking并注释掉了该RemainAfterExit选项)。

列表单元文件

我遇到了一个问题,我现在无法重现 - 当我尝试时,systemctl start响应类似于“没有单元文件”,所以我想知道是否需要以某种方式注册它。

不,你不需要。你可以执行systemctl list-unit-files --type=service,你应该在那里看到你的单位。我的问题是,该ExecStart参数错误。当我现在尝试同样的操作时,我更容易理解消息:

$ systemctl start test_screen.service
Job for test_screen.service failed because the control process exited with error code. See "systemctl status test_screen.service" and "journalctl -xe" for details.

$ systemctl status test_screen.service
● test_screen.service - Testing `screen` service
   Loaded: loaded (/etc/systemd/system/test_screen.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Wed 2017-09-20 12:55:53 CEST; 1min 3s ago
  Process: 37344 ExecStart=/bin/screen2 -d -m -S test_screen (code=exited, status=203/EXEC)
 Main PID: 36634 (code=exited, status=1/FAILURE)

Sep 20 12:55:53 somehostname systemd[1]: Starting Testing `screen` service...
Sep 20 12:55:53 somehostname systemd[37344]: Failed at step EXEC spawning /bin/screen2: No such file or directory
Sep 20 12:55:53 somehostname systemd[1]: test_screen.service: control process exited, code=exited status=203
Sep 20 12:55:53 somehostname systemd[1]: Failed to start Testing `screen` service.
Sep 20 12:55:53 somehostname systemd[1]: Unit test_screen.service entered failed state.
Sep 20 12:55:53 somehostname systemd[1]: test_screen.service failed.

执行脚本

我尝试修改我的 test_screen 来调用脚本

$ cat /etc/systemd/system/test_screen_script.service
[Unit]
Description=Testing `screen` service

[Service]
Type=forking
ExecStart=/root/test_screen_start.sh
ExecStop=/root/test_screen_stop.sh
#Environment=
#Restart=always
RemainAfterExit=True

[Install]
WantedBy=default.target

而脚本只是为之前的调用而包装

$ cat /root/test_screen_start.sh
/bin/screen -d -m -S test_screen
$ cat /root/test_screen_stop.sh
/bin/screen -S test_screen -X quit

当我这样做时,它没有启动:

$ systemctl start test_screen_script
Job for test_screen_script.service failed because the control process exited with error code. See "systemctl status test_screen_script.service" and "journalctl -xe" for details.

$ systemctl status test_screen_script.service
● test_screen_script.service - Testing `screen` service
   Loaded: loaded (/etc/systemd/system/test_screen_script.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Wed 2017-09-20 15:47:59 CEST; 8s ago
  Process: 63582 ExecStart=/root/test_screen_start.sh (code=exited, status=203/EXEC)
 Main PID: 60698 (code=exited, status=0/SUCCESS)

Sep 20 15:47:59 somehostname systemd[1]: Starting Testing `screen` service...
Sep 20 15:47:59 somehostname systemd[63582]: Failed at step EXEC spawning /root/test_screen_start.sh: Exec format error
Sep 20 15:47:59 somehostname systemd[1]: test_screen_script.service: control process exited, code=exited status=203
Sep 20 15:47:59 somehostname systemd[1]: Failed to start Testing `screen` service.
Sep 20 15:47:59 somehostname systemd[1]: Unit test_screen_script.service entered failed state.
Sep 20 15:47:59 somehostname systemd[1]: test_screen_script.service failed.

最好有人能描述一下原因。解决这个问题的方法是添加#!/bin/bash.

参考

相关内容