Creating a systemd Service for CentOS 72 min read

Keeping  your data backed up on your network is very important. However, to back up to an external location on a Linux system, such as a Network Accessible Storage (NAS) drive,  it is necessary to mount the external file system. If you mount a file system, it will become unavailable and require mounting again after each system reboot. This can be done manually, but if you have automated processes which rely on this external file system, you could run in to problems where the file storage becomes unavailable.

A simple solution for this problem is to create a system service which will start each time the system boots. This can be done by creating a systemd service. I needed to create a service like this to keep a NAS location available for Jenkins, my continuous integration server, so configuration data could be backed up.

This service was very simple to implement. First, I created a script which would mount my NAS drive and saved it in the root home directory. Then I created a service file which contained the following:

[Unit]
Description=<service name> Service
After=network.target
[Service]
Type=simple
User=root
ExecStart=<path to script>
Restart=on-abort
[Install]
WantedBy=multi-user.target

The service file is named nas1mnt.service and is saved at etc/systemd/system/. Starting and stopping the service is done with the following commands:

sudo systemctl start <service name>
sudo systemctl stop <service name>

To make the service start when the system boots execute the following command:

sudo systemctl enable <service name>

Or to stop using the service use:

sudo systemctl disable <service name>

Finally, the status of the service can be obtained using:

sudo systemctl status <service name>

That’s it! A very easy way to create a service which mounts an external file location at start up for for CentOS 7. Please check back often for more home networking tips.

Leave a Reply

Your email address will not be published.