M. Barbieri M. Barbieri - 1 year ago 75
Linux Question

Trying to make a bash script that restarts all services within a certain directory

I have made this simple little script to try and restart all services within a directory:

systemctl daemon-reload
for service in /etc/systemd/system/multi-user.target.wants/*
systemctl restart $service

When I try to run this I get the following error:

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

Answer Source

You need to extract the basename from the $servicevariable

systemctl restart $(basename $service)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download