我已经制作了这个简单的小脚本来尝试重新启动目录中的所有服务:
#!/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
答案 0 :(得分:1)
您需要从basename
变量
$service
systemctl restart "$(basename "$service")"
或者你可以使用${service##*/}
来删除直到并包括最后一个斜杠:
systemctl restart "${service##*/}"
答案 1 :(得分:1)
您也可以更改工作目录以避免基本名称或参数扩展:
#!/bin/bash
systemctl daemon-reload
cd /etc/systemd/system/multi-user.target.wants
for service in *; do
systemctl restart "$service"
done