gpt4 book ai didi

bash - Centos 7 创建服务以在无限循环上运行 shell 脚本

转载 作者:行者123 更新时间:2023-12-04 19:35:26 26 4
gpt4 key购买 nike

我有以下脚本:

whie true
do
#code
sleep 60
done
然后我想创建一个服务来启动机器并将这个脚本作为服务启动:
在/etc/systemd/system/my.service 创建了 my.service
[Unit]
Description=my Script

[Service]
Type=forking
ExecStart=/bin/script.sh

[Install]
WantedBy=multi-user.target

当我 systemctl start my.service 出现问题
它进入 while true 循环并卡在那里,我如何运行此服务并使其在后台运行?

最佳答案

根据 link 的 systemd 规范. Type=forking在您的情况下不是完全正确的启动方式

If set to forking, it is expected that the process configured withExecStart= will call fork() as part of its start-up. The parentprocess is expected to exit when start-up is complete and allcommunication channels are set up. The child continues to run as themain service process, and the service manager will consider the unitstarted when the parent process exits. This is the behavior oftraditional UNIX services. If this setting is used, it is recommendedto also use the PIDFile= option, so that systemd can reliably identifythe main process of the service. systemd will proceed with startingfollow-up units as soon as the parent process exits.

Type=simple可以是正确的。你可以试试

If set to simple (the default if ExecStart= is specified but neitherType= nor BusName= are), the service manager will consider the unitstarted immediately after the main service process has been forkedoff. It is expected that the process configured with ExecStart= is themain process of the service. In this mode, if the process offersfunctionality to other processes on the system, its communicationchannels should be installed before the service is started up (e.g.sockets set up by systemd, via socket activation), as the servicemanager will immediately proceed starting follow-up units, right aftercreating the main service process, and before executing the service'sbinary. Note that this means systemctl start command lines for simpleservices will report success even if the service's binary cannot beinvoked successfully (for example because the selected User= doesn'texist, or the service binary is missing).

关于bash - Centos 7 创建服务以在无限循环上运行 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68394018/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com