23

I have created a systemd service:

[Unit]
Description=My service

[Service]
Type=forking
ExecStart=/bin/sh $HOME/theFolder/run.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target
Alias=mine.service

I put it in the /etc/systemd/system folder and named it mine.service.

If I run sudo service mine start it gives me :

Failed to start mine.service.Unit mine.service not found

--- UPDATE -----

I run first sudo systemctl daemon-reload and now it doesn't give me any messages but the service still doesn't start.

7
  • 14
    Have you run systemctl daemon-reload after creating your unit file?
    – Wieland
    Jul 4 '16 at 20:38
  • @Wieland:Hmm..Now it doesn't show me any messages.But,it still doesn't start.
    – George
    Jul 4 '16 at 20:44
  • @don_crissti:The status says it is active ,but the script ( in ExecStart ) is not executed.
    – George
    Jul 4 '16 at 20:52
  • how it would know the proper $HOME? What you expect it to be for a system service?
    – Serge
    Jul 4 '16 at 20:59
  • @Serge:Hmm..So, how can I use $HOME?
    – George
    Jul 4 '16 at 21:01
16

$HOME points to the user's home directory who is running the script. Systemd services are started with root so it will likely trying to /root/theFolder/run.sh. Use absolute paths in service files.

You also have the forking option set. This is needed for program that background themselves, does your script do this? Most do not and if yours does not you should remove this option or systemd will be waiting for your script to finish to claim it as started.

Also system service files that point to scripts in your home directory are discouraged and possibly a security concern. Since they are run as root anyone that can modify them can potentially gain root access to your computer. It is far better to copy the script to /usr/local/bin and ensure it is owned and only writable by root to stop this. It is also a good idea to run the script as an unprivileged user using the User= and Group= options in the service file.

If you want to run it as your user it is better to put the service file in ~/.config/systemd/user/ and start/enable it with systemctl --user enable yourservice && systemctl --user start yourservice (note, run as your user not root). See this for more info on user service files.

12
  • :I put the script into /usr/local/bin and the service into /.config/systemd/user.If I try to enable the service ,it shows me "excess arguments".If I try to use systemctl it shows 'failed.. unit..',something like this.
    – George
    Jul 5 '16 at 8:25
  • Can you provide the full commands you tried and more detailed logs. Its hard to debug with fragments. Jul 5 '16 at 14:40
  • :Is there a solution to use the $HOME path?Because,if I put the script in /usr/local/bin ,it uses relative paths to $HOME.I saw this.And I tried to put HOME=$HOME in the first environment file.But the service couldn't start,no errors.
    – George
    Jul 5 '16 at 14:52
  • 1
    on ubuntu I had to use systemctl for this not systemd, using systemd I got an error "Excess Arguments" Oct 3 '19 at 19:33
  • 1
    @AlexanderMills You are right, the correct command in systemctl and systemd was just a typo in the answer. Oct 5 '19 at 13:16

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