I want to list all services that starts with the letters lc-. Then I want to restart them.
How can I attchive this? I was thinking something like:
systemctl -l | grep ?? | systemctl restart ???
I want to list all services that starts with the letters lc-. Then I want to restart them.
How can I attchive this? I was thinking something like:
systemctl -l | grep ?? | systemctl restart ???
You can list the unit files with..
-E
means you are going to use an extended regex expression.-o
means you want only the matching parts.'^lc-.*\.service'
is the regular expression to get only those services that start with 'lc-'.Then to restart all of them you can use wildcards in your command
systemctl
can filter by unit type and unit state on its own, not to mention it can print the output as a JSON file, which allows extremely robust parsing, so you could print only running services as a JSON file:filtering out elements whose
unit
key's value doesn't start withlc-
, printing just the unit's name:passing the output to
xargs -r
+sudo systemctl restart
(the-r
switch will preventxargs
from runningsudo systemctl restart
in case no running service matching the criteria was found; note that valid unit names can't include whitespace per systemd's specifications so piping the output toxargs -r
directly is safe):Putting everything toghether: