首页 > 解决方案 > 使用 systemctl 作为服务运行时优雅退出 bash 脚本

问题描述

我已经在 call 中设置了一个/etc/systemd/system/服务my_script.service。之后我运行:

sudo systemctl daemon-reload
sudo systemctl start my_script.service
sudo systemctl stop my_script.service

我希望脚本优雅地“死去”。

当我从 CLI 运行脚本时,我有一个trap ctrl_c INT函数在我按 ctrl+c 停止脚本时运行。

如何使用 systemctl 获得相同的功能?

标签: bashubuntusystemctl

解决方案


陷阱TERM以及INTTERM是 的非交互式模拟INT;交互式按下Ctrl-C触发器INT,而非交互式进程终止通常使用TERM.


推荐阅读