ddollar / foreman

Manage Procfile-based applications
http://ddollar.github.com/foreman
MIT License
6.04k stars 633 forks source link

Trailing `@` in systemd service filename causes an error #704

Open njpearman opened 6 years ago

njpearman commented 6 years ago

I've been using Foreman to export a systemd service on a remote server for a Rails app and experienced the following error when trying to run the service that was created:

$ sudo systemctl status myapp-web\@
Failed to get properties: Unit name myapp-web@.service is not valid.

I'm using this service name because myapp-web@.service is the service file that is created by the foreman export systemd command, although there are other related files for the service that do not include a trailing @. The full list of files and directories created are:

myapp.target
myapp-web@.service
myapp-web.target
myapp-web.target.wants/myapp-web@5000.service

Running systemctl enable myapp-web doesn't work, returning Failed to execute operation: No such file or directory.

But when I rename the service file and remove the @, I can use systemctl status and all other systemctl commands for my new service successfully.

I had a look through the source code to try to figure this out and the following line adds a trailing @ to the file name. Is this necessary for the main service file?

https://github.com/ddollar/foreman/blob/025de2a7218414e9c0ad964aef62221fcc440153/lib/foreman/export/systemd.rb#L23

I'm just starting with Foreman and also inexperienced with Ubuntu services so I might be doing something wrong with service startup or how I'm using Foreman. I was also going backwards and forwards to change service file names and try different locations while I was looking at this so my steps to reproducing this problem are flaky. However, it does seem odd to me that the service file includes a trailing @ and am wondering whether it is needed.

andrewmcodes commented 5 years ago

@njpearman Is this still an issue?

njpearman commented 5 years ago

Hi,

I don't find myself creating many new services to be honest so I've not retried :) Although this is well-timed, as I'm about to put a Foreman in place for a new app. I'll try it out and see what happens.

As I mentioned, I got around the problem I encountered by renaming the service files to remove the @ before trying to use it. I'll default to that if the generated filename still doesn't work. I'll let you know either way.

Can you comment on why the trailing @ is included in the generated filename?

njpearman commented 5 years ago

I've just retried and I get the same problem as I originally posted. After I export the foreman to systemd, I cannot start up the service. I have tried the following permutations on the system name:

$ sudo systemctl status myapp-web\@
Failed to get properties: Unit name myapp-web@.service is not valid.

$ sudo systemctl status myapp-web@
Failed to get properties: Unit name myapp-web@.service is not valid.

$ sudo systemctl status myapp-web
● myapp-web.service
   Loaded: not-found (Reason: No such file or directory)
   Active: inactive (dead)

Once I rename the service file to remove the trailing @, the status command works.

My uneducated guess is that the file in foreman that I mentioned above has a typo and the @ should not be included for services that do not set an explicit port number.

roback commented 5 years ago

Had the same issue myself and it turns out the @ symbol has a special meaning in systemd, so ending the name with @ isn't a bug in foreman. There's some info in the manpage for systemd.unit:

Unit files can be parameterized by a single argument called the "instance name". The unit is then constructed based on a "template file" which serves as the definition of multiple services or other units. A template unit must have a single "@" at the end of the name (right before the type suffix). - https://www.freedesktop.org/software/systemd/man/systemd.unit.html#Description

To stop/start/restart etc. the service you should use the name myapp.target. For example sudo systemctl start appname.target. The foreman documentation has a few examples as well: https://ddollar.github.io/foreman/#SYSTEMD-EXPORT