Edit with systemctl edit to add restart on fail to a service – nfs-ganesha

Author:

A quick tip how to edit a service unit file under a c system like CentOS Stream 9 or Ubuntu. The best way is to edit it with the the tool “systemctl edit [service_name]”, which will trigger the default editor to open a temporary copy of the systemd unit file with the service name used with it. The default editor in the console is controlled by “EDITOR” variable and may be changed prior using the systemctl edit. After a successful manipulation of the system unit file the new one will be installed and a reload of the systemd unit files will be triggered with “systemctl daemon-reload” automatically. Indeed, it is just a text edit of a text file, which will do several actions when using “systemctl edit” command.

main menu
systemctl cat service

systemd options ro restart a service on fail are:

[Service]
Restart=on-failure
RestartSec=5s

Here, the example is to add a restart-on-fail functionality to the nfs-ganesha service (NFS service). The systemctl edit may be used for many other changes to the systemd unit file under the console and it is the easiest and proper way.

SCREENSHOT 1) Use “systemctl edit” to edit a copy of the systemd override unit file.

do not insert anything at the end of the comments or below the second red line comments – “### Lines below this comment will be discarded”. This temporary override file includes a systemd unit file of the service, which is opened for editing. The result override.conf file will only include the added lines, no other comments shown below the second red line.

main menu
systemctl edit opened

SCREENSHOT 2) Add the options for the restart-on-failure and a period of time to wait for the next restart.

Under section [Section] the two new options will override the previously mentiond ones, that’s why the override file is included in the end of the systemd unit service file.

main menu
restart on failure

SCREENSHOT 3) Save the changes and exit the editor.

main menu
save

SCREENSHOT 4) The “systemctl cat” command will show the systemd unit with concatenated override file.

main menu
systemctl cat

Here is the override files, which is automatically created by the systemctl:

[root@srv ~]# cat /etc/systemd/system/nfs-ganesha.service.d/override.conf
[Service]
Restart=on-failure
RestartSec=5s

Troubleshooting

If after exiting the editor there is an error:

[root@srv ~]# systemctl edit nfs-ganesha
Editing "/etc/systemd/system/nfs-ganesha.service.d/override.conf" canceled: temporary file is empty.

The problem is the user did not follow the (STEP 1) above and added the text in the write place. The added lines should be between the lines:

### Anything between here and the comment below will become the new contents of the file


### Lines below this comment will be discarded

More on systemdhttps://ahelpme.com/tag/systemd/.

Leave a Reply

Your email address will not be published. Required fields are marked *