60

I've put together a foo.service file for our foo service that runs as a daemon. The service runs fine when I run systemctl start foo (and stop) but systemtcl enable foo results in Failed to issue method call: Invalid argument. The unit file is placed in /etc/systemd/system/foo.service, and has permissions 0755. Setting systemd to debug and running enable gives

Looking for unit files in (highest priority first):`
    /etc/systemd/system
    /run/systemd/system
    /usr/local/lib/systemd/system
    /usr/lib/systemd/system
Looking for SysV init scripts in:
    /etc/rc.d/init.d
Looking for SysV rcN.d links in:
    /etc/rd.c
Failed to issue method call: Invalid argument

Googling around, it seems like systemctl isn't finding the .service file. Is there any way to verify that? If so, how can I fix that? Any other ideas about what might be wrong? Is there more debugging I can enable? The debug info given doesn't really help me narrow down the problem.

foo.service looks like:

[Unit]
Description=Blah Blah Blah

[Service]
ExecStart=/usr/bar/doof/foo
Type=simple
PIDFile=/var/run/foo.pid

[Install]
WantedBy=multi-user.target,graphical.target

EDIT: Yes, I did run systemctl daemon-reload.

2
  • 3
    Have you ran systemctl daemon-reload after adding or changing your foo.service file ?
    – nos
    Dec 28, 2015 at 22:26
  • Yes, I did run daemon-reload Dec 28, 2015 at 22:43

8 Answers 8

102

For people from Google:

  • Validate with sudo systemd-analyze verify NAME.service
  • When using a symlink, make sure it uses absolute path
  • Make sure the name is like /etc/systemd/system/*.service
  • Do sudo systemctl daemon-reload after changes
5
  • 6
    For me, I was doing soft link (ln -s ... ...). I did hard link (ln ... ...) and it worked Apr 15, 2019 at 11:35
  • 2
    @AnandUndavia You should be getting an error: *.service is not a symlink, ignoring. when using a hard link. Apr 23, 2019 at 11:25
  • sudo systemd-analyze verify xxx gave me Unknown operation 'verify' Apr 28, 2022 at 6:37
  • @PierredeLESPINAY As of v248 it's still in the manual, but if you find the cause / workaround please edit it into the answer!
    – Mark
    Apr 28, 2022 at 18:59
  • I got Unit foo.service not found when lookup the foo daemon, even when I specified the absolute path, would it be that it is recognised by the systemctl with another name? Sep 22, 2022 at 11:05
27

The issue in my case was the file was symlinked from another partition and systemd do not support that.

Unit files have to reside on a partition that is mounted at the moment the host PID 1 is invoked. i.e. either on the root partition or some other partition that the initrd premounts.

I'm answering to an old question because this is a top result while googling on the issue and might help someone

1
  • Thanks. The worst thing is that no error is ever printed referring to this! Nov 5 at 16:34
25

I found this question over google, searching for "systemctl unit not found"

In my case I generated a *.service file via podman and no matter what I did, systemctl wouldn't find the service file.

Solution was to check selinux and set the correct labels. Example:

/sbin/restorecon -v /etc/systemd/system/container_httpd.service
3
  • 2
    Thank you! This did it for me. Can you elaborate what exactly it does?
    – Niwla23
    Dec 25, 2022 at 13:09
  • good solution, indeed the podman generate is an issue
    – TecHunter
    May 8 at 11:10
  • @Niwla23 Selinux restricts systemd to only seeing files labelled systemd_unit_file_t. The service file in question was created elsewhere (usually generated by podman-generate) and then moved here. Since moving a file does not update its label, this file keeps its original (different) label that was assigned when it was created, and therefore cannot be seen by systemd. restorecon resets the file context according to the persistent rules at /etc/selinux/targeted/contexts/files, which "corrects" the label.
    – cyqsimon
    Jul 11 at 19:22
7

Make sure that if you are putting your units in the user folder .config/systemd/user/*.service that you are running the command as

systemd --user <command>

(note the --user)

3

Error is because of two target are specified to WantedBy. Just mention in this way:

[Install]
WantedBy=multi-user.target

I really don't know how we can specify two targets to it.

2
  • 5
    Multiple targets are space delimited, not comma delimited. Dec 14, 2016 at 5:50
  • WantedBy option may be used more than once, or a space-separated list of unit names may be given. So this answer is correct.
    – Pramod
    Dec 13, 2017 at 6:12
2

Had this issue earlier when adding my own units, turned out to be wrong/missing context for SELinux. Test again with setenforce permissive set and set proper context for SELinux with chcon when in enforcing mode.

1
  • This solved the problem for me on Oracle OCI VM instance.
    – Alexander
    Apr 3 at 7:37
0

I ran into this error when using Podman to generate systemd unit files.

The error occurred because selinux was blocking the file from being properly read. I found there were two solutions.

  1. Set your directory to the systemd service location for unit files with cd /etc/systemd/system. Then use podman generate to create the file directly here. This seems to be accepted by selinux without issue.

  2. Generate the systemd unit file with podman generate and copy the file to /etc/systemd/system. You then need to run /sbin/restorecon -v /etc/systemd/system/container-<your container name>.service to have selinux reset its security context for that file.

0

When you run into this you can try to get the file it refers to. For example, I had the same problem with ContainerD. All I had to do was go to another server and get the file "cat /lib/systemd/system/containerd.service". Somewhere on your file system, this file has to exist (maybe in your backups).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.