Where to place user created systemd unit files

Where should I put systemd file for e.g. Nginx nginx.service or something like that on Ubuntu 16.04 ?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The recommended place is /etc/systemd/system/nginx.service

Then issue the command :

systemctl enable nginx

And finally

systemctl start nginx

Method 2

Usually, in Ubuntu, the package provided unit files go in /lib/systemd/system/ directory e.g. /lib/systemd/system/nginx.service whereas the user provided or any modification to the package provided unit file(s) go in /etc/systemd/system/ directory.

Now, to override, you can:

  • Name the unit file as the package provided one e.g. to override /lib/systemd/system/nginx.service, you can create the file /etc/systemd/system/nginx.service and put the directives in there
  • Or you can create a drop-in snippet ending in .conf in a directory that is named after the original unit file with .d appended to the directory name e.g. you can create a file named /etc/systemd/system/nginx.service.d/override.conf and put statements in there

Now, you can leverage the systemctl edit command to do all the manual stuffs for you i.e. it will create the necessary override file for you (at first it will create a temporary file (with intermediate directory(ies) for drop-in snippets), and upon saving it will commit and rename() the temp file to desired final file), and you just need to edit the file to put in your overrides.

For example, to create an override snippet for ngnx.service (/lib/systemd/system/nginx.service) mentioned above:

systemctl edit nginx.service

By default, it will use the file /etc/systemd/system/nginx.service.d/override.conf. If --full is used then the full replacement file /etc/systemd/system/nginx.service will be used (the original content from /lib/systemd/system/nginx.service will be copied to this file).

You can also use --runtime to create the snippet in /run/systemd/system/ (at first drop-in snippet, then whole so --runtime and --full are not mutually-exclusive) that will be temporary of course.

You can obviously choose the editor to use, the order of precedence is:

$SYSTEMD_EDITOR > $EDITOR > $VISUAL > editor > nano > vim > vi


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x