Creating systemd Service Files

[DevDungeon Intro] Hello, this is NanoDano at … and we’re going to walk through the … process of creating a custom service file for systemd. In this video I’ll be using Ubuntu, but these instructions should apply to other distributions … that also use systemd. For this tutorial, let’s create a Bash script that … will be our service. I’ll make a file called In, I’ll add a shebang, create an unending while loop, and inside it, echo the current time, and then sleep for a second. Then save the file and quit. Be sure to add the executable permission to the … script with chmod +x Now that we have a service that we want systemd to … manage, let’s create a service file. Go to the /etc/systemd/system directory … and create a new file. I’ll name this one … pointless.service. The file needs to end with … .service, but the prefix can be … whatever you want. In the pointless.service file, the only section that is … required is a service section, so let’s create one by typing … out [Service] in a pair of brackets. In this section we’ll specify some of the options. The most important option is the ExecStart option. Type out ExecStart=and then the name of the … command you want to run. In this case, /home/nanodano/ Then let’s save the file. Technically, that’s all we need are … required to do. We can now run our service … with sudo systemctl start pointless. We can verify the service is running with sudo systemctl … status pointless. We should see a green circle, along with the active(running) text to indicate it is running. It also tells us what the process id is. After the status information, it will also output a few lines … of the most recent log output. By default, the standard output of a … service goes to syslog, so we can view the output by … manually inspecting /var/log/syslog, or by running sudo journalctl -u pointless You can also run sudo journalctl -u pointless -f to tail … the log, and see new entries in real … time. Notice the date, hostname, and process id are … prepended to the log message for each line. We can stop our service with sudo systemctl stop pointless At this point, you know how to create and … manage a very simple service. Depending on your use case, this may be all you need, but you will likely need to customize a few things, so let’s look at some more options. To view all of the options, open the manual page for … systemd.service with man systemd.service. Look for the section labeled Options, this lists some options we can use in our service section of … the file. ExecStart is there, the one we already have in our file. If we look further there is an ExecStartPre and … ExecStartPost. We can trigger scripts to be … run before and after the ExecStart command. There are also ExecReload, ExecRestart and ExecStop … options if stopping and reloading requires something … besides a typical killing and re-running ExecStart. You can also set timeouts for starting and stopping as well … as a maximum runtime for the service. If a RuntimeMaxSec value is provided, the service will be terminated after the limit is reached. Let’s go back to our service file now. There are a few common options I want to … demonstrate. First, let’s ad the Restart option. Add Restart=always on a new line. Let’s also add the WorkingDirectory=/home/nanodano Let’s also specify User=nanodano and … Group=nanodano. Those are a few options that … most people will find useful. If you needed to set environment variables, you can do that with the following format. Environment=KEY=value. For example, if we wanted to set the GOPATH to … /home/nanodano/go we would add … Environment=GOPATH=/home/nanodano/go. You can have multiple … Environment lines to add multiple variables. You can also use an EnvironmentFile that contains … all of the environment information. So far we’ve only had a Service section to our file. Let’s create a new section called Unit at the top of the … file, and provide a description and … then specify This specifies that the service needs networking to be ready … before starting. Our service doesn’t truly … need the networking service, but many services are tcp/ip … servers that do require this. Let’s also add a section at the bottom named Install. The install section is needed if you want to run systemctl … enable to have your service run automatically on startup. In the Install section, add … This tells systemd we want … this to load as part of the standard multi user boot process. Now we can go back to the terminal and run sudo … systemctl enable pointless. Now the service will run … automatically when the system reboots. You can disable this by running sudo systemctl … disable pointless. If you manually modify a service file, make sure to run sudo systemctl daemon-reload. We can also restart the service with sudo systemctl … restart pointless. Now if we check the systemctl status pointless, we will see the description we provided. You can also edit a service file with sudo systemctl edit … pointless –full. If you edit it this way, you don’t need to run daemon-reload. You can use this to edit existing service files but not … to create new ones. That’s all for now. Thank you for watching. Check out and subscribe to the channel.


Add a Comment

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