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
    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

$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 systemd --user enable yourservice && systemd --user start yourservice (note, run as your user not root). See this for more info on user service files.

  • :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. – Michael Daffin 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
  • You need to run it as your user if you require $HOME to be configured correctly (and also to stop root owned files appearing in your home directory). Note that if you use the user service file method you don't need to copy the script to /usr/local/bin, that is just for system scripts. – Michael Daffin Jul 5 '16 at 15:55
  • :Do you mean run it as sudo systemd --user start service? – George Jul 5 '16 at 16:01

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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