현재 활성 서비스( telegraf
)가 컴퓨터에서 실행 중입니다. 서비스가 shell script
.
systemctl status telegraf
서비스 상태를 수동으로 확인하는 명령을 실행하고 있습니다 . 그리고 출력은,
telegraf.service - The plugin-driven server agent for reporting metrics into InfluxDB
Loaded: loaded (/usr/lib/systemd/system/telegraf.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2021-05-19 12:26:17 UTC; 3 weeks 5 days ago
Docs: https://github.com/influxdata/telegraf
Main PID: 1297 (telegraf)
Tasks: 9 (limit: 49778)
Memory: 106.3M
CGroup: /system.slice/telegraf.service
└─1297 /usr/bin/telegraf -config /etc/telegraf/telegraf.conf -config-directory /etc/telegraf/telegraf.d
가치를 포착하고 싶다활성(실행 중)내 스크립트에서는 쉘 스크립트에서 어떻게 수행합니까?
답변1
명령 의 출력을 구문 분석할 수 있습니다 systemctl status
.
그러나 서비스가 실행 중인지 여부에 대한 정보를 얻는 더 쉬운 방법은 의 is-active
하위 명령을 사용하는 것입니다 systemctl
. 예를 들어:
user@host:~$ systemctl is-active postfix
active
서비스가 활성화되지 않은 경우 반환되는 문자열은 입니다 inactive
.
따라서 스크립트에서 반환된 문자열(활성 또는 비활성)을 확인하거나 위에서 언급한 명령의 반환 코드를 확인할 수 있습니다. 서비스가 활성화된 경우 명령은 0으로 반환되고 그렇지 않으면 0이 아닌 반환 코드로 다시 실행됩니다.
안녕하세요 :)