Project

General

Profile

How to Enable a systemd Unit Template for Boot Execution

Added by Ben Atkinson almost 8 years ago

Hello,

I have a server to launch at system boot time using the Yocto compatible FS from ALPHA Release 2016.01 on DBRIC hardware. The server uses a template unit file, and I am having trouble getting it enabled. We use the template to supply a full path name of a sqlite database file the server uses. Here is the template unit file in /lib/systemd/system/TDI_DataServer@.service:

[Unit]
Description=NDC Technologies Data Server application (with DB=%I)
After=syslog.target
[Service]
Type=simple
ExecStart=/opt/ndc/DataServer --DEBUG --databaseFile=%I
Restart=always
RestartSec=2
[Install]
WantedBy=multi-user.target

The database file is in /var/lib/ndc/DataServer.DB, so I created a symbolic link in /lib/systemd/system:
-> TDI_DataServer@.service

When I run:

$ systemctl start 

The server launches with the correct parameter, finds the database, and executes. So I tried to enable the service template so that it will run on system boot:

$ systemctl enable 
Failed to execute operation: No such file or directory

I've seen some posts online that this should work, but there was a bug in systemd preventing it from succeeding.

Are you aware of this bug?
Is there a fix that can be applied to the Yocto FS?

Since that failed, I tried enabling the template file itself:

$ systemctl enable TDI_DataServer@
ln -s '/lib/systemd/system/TDI_DataServer@.service' '/etc/systemd/system/multi-user.target.wants/TDI_DataServer@.service'

On the surface, the system seems happy, but at boot time, systemd does not execute the launch of TDI_DataServer correctly. It seems to supply the "multi-user" parameter (maybe from the WantedBy target in the unit file?) to the unit file.

Here is the systemctl status:

$ systemctl status -l TDI*
- NDC Technologies Data Server application (with DB=multi/user)
Loaded: loaded (/lib/systemd/system/TDI_DataServer@.service; enabled)
Active: activating (auto-restart) (Result: signal) since Sat 2000-02-05 20:41:05 UTC; 1s ago
Process: 2951 ExecStart=/opt/ndc/DataServer --DEBUG --databaseFile=%I (code=killed, signal=ABRT)
Main PID: 2951 (code=killed, signal=ABRT)
Feb 05 20:41:05 mityomapl138 DataServer[2951]: [949783265:2047] NOTICE:  per-conn mem: 136 + 2140 headers + protocol rx buf
Feb 05 20:41:05 mityomapl138 DataServer[2951]: [949783265:2056] NOTICE: Listening on port 4243
Feb 05 20:41:05 mityomapl138 systemd[1]: : main process exited, code=killed, status=6/ABRT
Feb 05 20:41:05 mityomapl138 systemd[1]: Unit entered failed state.
Feb 05 20:41:05 mityomapl138 DataServer[2951]: 2000:02:05 20:41:05: WSServer connected on socket tcp://localhost:5556
Feb 05 20:41:05 mityomapl138 DataServer[2951]: 2000:02:05 20:41:05: bound to socket inproc://clients
Feb 05 20:41:05 mityomapl138 DataServer[2951]: 2000:02:05 20:41:05: DatabaseWorker using database multi/user
Feb 05 20:41:05 mityomapl138 DataServer[2951]: unable to open database file
Feb 05 20:41:05 mityomapl138 DataServer[2951]: terminate called after throwing an instance of 'SQLite::Exception'
Feb 05 20:41:05 mityomapl138 DataServer[2951]: what(): unable to open database file
● TDI_DataServer.service
Loaded: loaded (/etc/init.d/TDI_DataServer)
Active: inactive (dead)

Am I missing something? As I said earlier, I've seen some posts about a systemd bug for enabling unit template files, but I'm not sure if it applies here.

Thanks for your help.

Regards,

Ben


Replies (10)

RE: How to Enable a systemd Unit Template for Boot Execution - Added by Jonathan Cormier almost 8 years ago

I'm not familiar with the template system for SystemD. Would using environment variables meet your needs?

http://www.alaux.net/articles/systemd-unit-files-and-environment-variables/

RE: How to Enable a systemd Unit Template for Boot Execution - Added by Bob Duke almost 8 years ago

Ben, if you could share the posts you saw regarding the bug, that may be worth investigating.

We have not seen this bug here, but we haven't tried to do the same thing.

RE: How to Enable a systemd Unit Template for Boot Execution - Added by Ben Atkinson almost 8 years ago

I found this reference to a similar RedHat bug:

https://bugzilla.redhat.com/show_bug.cgi?id=1206007

The error message reported is slightly different, but the net result of the symlink not being created in /etc/systemd/system/multi-user.target.wants is the same.

By the way...

If I put my own symlink in the directory:

/etc/systemd/system/multi-user.target.wants

as:

 -> /lib/systemd/system/TDI_DataServer@.service

...everything works fine. It's just that "systemctl enable" is supposed to do that for me.

RE: How to Enable a systemd Unit Template for Boot Execution - Added by Bob Duke almost 8 years ago

Ben, can you post the output of "systemctrl --version"

RE: How to Enable a systemd Unit Template for Boot Execution - Added by Ben Atkinson almost 8 years ago

Here is the version:

$ systemctl --version
systemd 211
+PAM +LIBWRAP -AUDIT -SELINUX +IMA +SYSVINIT -LIBCRYPTSETUP -GCRYPT +ACL +XZ -SECCOMP -APPARMOR

RE: How to Enable a systemd Unit Template for Boot Execution - Added by Bob Duke almost 8 years ago

According to the bugzilla post, that bug should be fixed in 211.

As a workaround, can you use an environment variable approach that was suggested by Jon in an earlier post?

RE: How to Enable a systemd Unit Template for Boot Execution - Added by Ben Atkinson almost 8 years ago

Bob,

We are looking at workarounds getting the enable to function. Do you have a Yocto version number associated with the ALPHA Release 2016.01 filesystem we are using?

Regards,

Ben

RE: How to Enable a systemd Unit Template for Boot Execution - Added by Ben Atkinson almost 8 years ago

Bob,

So I was able to home in on the issue. It centers on the parameter being passed to the unit file. As stated before, this one fails:

systemctl enable 

However, if I change our database name from "DataServer.DB" to "DataServer.db" (i.e. all lowercase after the dot,) and I run the following:

systemctl enable 

... everything works just fine. Systemctl creates the link in /etc/systemd/system/multi-user.taret.wants, and on the next boot, our DataServer service is happy.

So it looks like the bug centers on the parsing of the parameter by the %I in the unit file. The above workaround is satisfactory for us, but the folks at Yocto may be interested in this.

Regards,

Ben

RE: How to Enable a systemd Unit Template for Boot Execution - Added by Bob Duke almost 8 years ago

Ben,

Thank you for the follow-up on this bug. I'm glad you got it working. I can't find any reference to case-sensitivity in the docs, so I create an internal issue for us to test this with our final Yocto release.

-Bob

    (1-10/10)
    Go to top
    Add picture from clipboard (Maximum size: 1 GB)