2016-05-31 25 views
0

我做了這個簡單的小腳本來嘗試和目錄內重新啓動所有服務中的所有服務bash腳本:試圖做出重新啓動某個目錄

 
    #!/bin/bash 
    systemctl daemon-reload 
    for service in /etc/systemd/system/multi-user.target.wants/* 
    do 
     systemctl restart $service 
    done; 

當我試圖運行此我得到的以下錯誤:

 
    Failed to restart etc-systemd-system-multi\x2duser.target.wants-openvswitch.service.mount: Unit etc-systemd-system-multi\x2duser.target.wants-openvswitch.service.mount failed to load: No such file or directory 

回答

1

您需要從$service變量

systemctl restart "$(basename "$service")" 
提取

或者你可以使用${service##*/}這將刪除了直到幷包括最後的斜線:

systemctl restart "${service##*/}" 
+0

感謝您的快速反應! –

1

您也可以改變工作目錄,以避免基名或參數擴展:

#!/bin/bash 
systemctl daemon-reload 
cd /etc/systemd/system/multi-user.target.wants 

for service in *; do 
    systemctl restart "$service" 
done