What is systemd and what is it needed for?
systemd is an init system used in Linux. It also manages system processes after booting. So you may need to use systemd if you want to make your own service.
What is service?
service is an application running in the background. For example, Apache or MySQL are services. These apps run in background and you may do some operations using commands:
systemctl start myservice systemctl stop myservice systemctl restart myservice systemctl status myservice # to check service status systemctl reload myservice # to make service reload its settings
systemd in Ubuntu
Please note that systemd is only fully supported in Ubuntu 15.04 and later releases. In previous versions of Ubuntu Upstart was used to manage services.
How to create a service
Service configurations are written in files called units.
System units are located in
User created units are located in
Let's create a new unit file for our service with name
myservice.service and place it in
In that file, first of all, we will declare
We will set some description for our service and also for example let's make it start after NGINX web server service
[Unit] Description=MyService After=nginx.serviceIf you want, you may make other service required to be running before this service could be started. Let's require MySQL database service to be running:
Requires=mysql.serviceNext, let's declare
Servicesection in our config file:
[Service] Type=simpleIn this section we may set working directory:
WorkingDirectory=/var/www/myappWe can also set user and group which will be used to start the service:
User=myserviceuser Group=mygroupEnvironmental variables:
Environment=DATABASE_PORT=3306And most important - commands to start / stop / reload service:
ExecStart=/path/to/your/app [args] ExecStop=/path/to/your/app [args] ExecReload=/path/to/your/app [args]Please note: You need to specify full path. Here is start command example for my Java application:
ExecStart=/usr/bin/java -jar /root/apps/javaapp/javaapp.jar
Here is an example of how your
myservice.service file can look like:
[Unit] Description=MyApplication After=nginx.service Requires=mysql.service [Service] Type=simple WorkingDirectory=/var/www/myapp User=myappuser Group=myappgroup Environment=MYSQL_PORT=3306 ExecStart=/var/www/myapp/app start-service ExecStop=/var/www/myapp/app stop-service ExecReload=/var/www/myapp/app reload-config
After creating this unit and placing it to
/etc/systemd/system you can start it with:
systemctl start myserviceAnd now you can check your service status with:
systemctl status myservice
If you want to reload systemd after making some changes to configuration, run:
systemctl daemon-reloadIf you want to check service logs, run:
journalctl -u myunitIf you want specific service to start on system boot:
systemctl enable myunitOpposite for that command (if you want your service not to be started on system boot) is:
systemctl disable myunit
That was short "cheetsheet"-like tutorial for systemd. Hopefully it helps me or anybody who reads this next time it will be needed to set up a systemd service. Anyway, I am planning to edit / extend / update this post in the future.
Thank you for reading!