Systemctl服务开始与Bash脚本

huangapple go评论88阅读模式
英文:

Systemctl Services start with Bash Script

问题

我有一个启动脚本,检查服务是否处于活动/非活动状态,并在必要时启动它:

  1. !/usr/bin/sh
  2. # 设置捕获错误的陷阱
  3. trap 'catch $? $LINENO' ERR
  4. function catch() {
  5. echo "发生错误,行号:$2,退出状态:$1"
  6. exit 1
  7. }
  8. # 设置脚本在错误发生时立即退出
  9. #set -e
  10. # 定义服务列表
  11. SERVICES=(service1 service2 service3)
  12. for service in $SERVICES
  13. do
  14. echo "检查 $service 状态"
  15. STATUS="$(systemctl is-active $service)"
  16. echo "状态为 ${STATUS}"
  17. RUNNING="$(systemctl show -p SubState $service | cut -d'=' -f2)"
  18. echo "运行状态为 $RUNNING"
  19. if [ "${STATUS}" = "active" ]; then
  20. echo "$service 服务处于活动状态"
  21. if [ "${RUNNING}" = "running" ]; then
  22. echo "$service 服务正在运行"
  23. else
  24. echo "$service 服务未运行"
  25. fi
  26. else
  27. echo "对于 $service 返回 ${STATUS}。正在启动..."
  28. systemctl start $service
  29. echo "$service 已启动"
  30. fi
  31. done

运行 'sh start.sh' 时,我得到 '发生错误,行号:19,退出状态:3'。这是什么原因?我没有看到任何语法错误。

英文:

I have a start script that checks if the service is active/inactive and starts it if necessary:

  1. !/usr/bin/sh
  2. # Set up the trap to catch errors
  3. trap 'catch $? $LINENO' ERR
  4. function catch() {
  5. echo "An error occurred on line $2 with exit status $1"
  6. exit 1
  7. }
  8. # Set the script to exit immediately on errors
  9. #set -e
  10. # define the services list
  11. SERVICES=(service1 service2 service3)
  12. for service in $SERVICES
  13. do
  14. echo "Checking $service status"
  15. STATUS="$(systemctl is-active $service)"
  16. echo "status is ${STATUS}"
  17. RUNNING="$(systemctl show -p SubState $service | cut -d'=' -f2)"
  18. echo "Running status is $RUNNING"
  19. if [ "${STATUS}" = "active" ]; then
  20. echo "$service Service is Active"
  21. if [ "${RUNNING}" = "running" ]; then
  22. echo "$service Service is Running"
  23. else
  24. echo "$service Service Not Running"
  25. fi
  26. else
  27. echo "Returned ${STATUS} for $service. Starting it... "
  28. systemctl start $service
  29. echo "$service started"
  30. fi
  31. done

When running 'sh start.sh' I get 'An error occurred on line 19 with exit status 3'
What is the cause for it ? I do not see any syntax errors.

答案1

得分: 1

Systemctl 手册页:如果没有活跃单元,则 is-active 可以返回非零的退出代码。

  1. is-active 模式...
  2. 检查指定的单元是否有任何活跃(即运行中)。如果至少有一个单元活跃,则返回退出代码 0
  3. 否则返回非零代码。除非指定 --quiet,否则还会将当前单元状态输出到标准输出。

https://www.freedesktop.org/software/systemd/man/systemctl.html#Exit%20status

英文:

Systemctl man page: is-active can return an exit code non-zero if no active

  1. is-active PATTERN...
  2. Check whether any of the specified units are active (i.e.
  3. running). Returns an exit code 0 if at least one is active,
  4. or non-zero otherwise. Unless --quiet is specified, this will
  5. also print the current unit state to standard output.

https://www.freedesktop.org/software/systemd/man/systemctl.html#Exit%20status

huangapple
  • 本文由 发表于 2023年4月19日 23:58:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056585.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定