Similar commands with find are not outputting the same

1

I'm trying to run a find command on a directory which contains unit files:

find /home/pi/units -type f -name "*.service"

which returns

/home/pi/units/manager.service

I then want to pipe that output into a find command to enable everything in that directory. I have tried a couple of different ways, but none work:

$ sudo systemctl start $(find /home/pi/units -type f -name "*.service")
Failed to start home-pi-units-manager.service.mount: Unit home-pi-units-manager.service.mount not found.

$ find /home/pi/units -type f -name "*.service" | xargs sudo systemctl start
Failed to start home-pi-units-manager.service.mount: Unit home-pi-units-manager.service.mount not found.

$ find /home/pi/units -type f -exec sudo systemctl start {} \;
Failed to start home-pi-units-manager.service.mount: Unit home-pi-units-manager.service.mount not found.

I want it to run:

sudo systemctl enable /home/pi/units/manager.service
sudo systemctl start /home/pi/units/manager.service

Why is it trying to run this home-pi-units-manager.service.mount when find outputs /home/pi/units/manager.service

Devin

Posted 2019-09-14T22:57:12.230

Reputation: 13

Your find commands are not the problem; they are finding the .service files you want, but the actual starting/enabling of these services is not working. You should rephrase the question to inquire as to why manager.service is referencing a service it can't find. – ajgringo619 – 2019-09-14T23:05:14.497

Answers

2

Why do you want it to run /home/pi/units/manager.service? That's not the name of the service! That's the path to the service file. You need to remove the path in the service name to have only the service left in the output. The name of your service apparently is → manager.service

Just like Devin said, find is working completely fine. It's systemD doing the character translation as you can read e.g. here → freedesktop.org - systemd-documentation

The escaping algorithm operates as follows: given a string, any "/" character is replaced by "-", and all other characters which are not ASCII alphanumerics or "_" are replaced by C-style "\x2d" escapes. In addition, "." is replaced with such a C-style escape when it would appear as the first character in the escaped string.

The more important part for you to note however is, don't run a systemctl command using the full path to the service file. You merely use the actual service name, like systemctl start manager.service
Therefore, just remove the path from your find output and it should work.

In Example:
$ sudo systemctl start $(find /home/pi/units -type f -name "*.service" | awk -F/ '{print $NF}')

..and plz rephrase the question. It is misleading indeed!

tobi

Posted 2019-09-14T22:57:12.230

Reputation: 136

0

That is what '-exec' is for my friend. Is this your card?

sudo find /home/pi/units -type f -name "*.service" -exec sudo systemctl start {} \;

Of course you could also use '-printf' and pipe it to a shell (sh):

sudo find /home/pi/units -type f -name "*.service" -printf 'sudo systemctl start %p\n' | sh

Edit: Also, be aware of sudo's authentication requirements when scripting. If you use sudo blank like that, and you haven't entered your sudo password yet in that session, it will fail (and probably not tell you why).

AenAllAin

Posted 2019-09-14T22:57:12.230

Reputation: 44