M. Barbieri M. Barbieri - 6 months ago 9
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:


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


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

You need to extract the basename from the $servicevariable

systemctl restart $(basename $service)
Comments