首页 > 解决方案 > 一次重启多个服务的Shell脚本

问题描述

需要使用 shell 脚本一次重新启动多个服务。我们现在手动进入每个文件夹(因为不同服务的文件位于不同的路径中)并使用命令重新启动它们。例如:

cd /opt/apps/service1 and run the command ./**** -start

对于 service2:转到文件夹cd /opt/apps/service2

并执行命令./**** - start

标签: linuxshellunix

解决方案


这个演示 bash 脚本应该适合你

#!/bin/bash

services=(
    # path, restart cmd
    "/deploy/service1, ./svr1 restart"
    "/deploy/service2, ./super_svr -s"
    "/deploy/service3, python ./svr3 restart"
)


for ((i = 0; i < ${#services[@]}; i++))
do
    entry="${services[$i]}"
    path=`echo $entry | cut -d',' -f1`
    cmd=`echo $entry | cut -d',' -f2`
    pushd $path
    $cmd
    popd
done

推荐阅读