Linux Bash IF 服务正在运行

问题描述 投票:0回答:1

下面是一个简单的 Powershell 脚本,用于判断服务是否正在运行

$service = 'Serenade'
if ($service.Status -ne 'Running')
{Start-Service -Name $service}
Else
{write-host 'Service already running'}

如果服务正在运行,编写 Linux Bash 脚本的等价物是什么?

linux bash centos
1个回答
0
投票

这相当于 bash:

#!/bin/bash

service_name="Serenade"

if systemctl is-active --quiet "$service_name.service" ; then
  echo "$service_name running"
else
  systemctl start "$service_name"
fi

在执行之前,需要设置文件的可执行位:

chmod +x service_checker_file.sh

现在你可以执行它了:

./service_checker_file.sh
© www.soinside.com 2019 - 2024. All rights reserved.