Systemd: setup a simple systemd service on Linux

Systemd: setup a simple systemd service on Linux

HomeOther ContentSystemd: setup a simple systemd service on Linux
ChannelPublish DateThumbnail & View CountActions
Channel Avatar LondonIAC / Dennis McCarthy / Automation Engineer2021-06-09 12:29:55 Thumbnail
6,186 Views
Systemd gives us the ability to create services out of almost anything. If you want to run an application and make sure that the application is available after a reboot, consider creating a service to manage that for you.

This GIST contains all the command:
https://gist.github.com/dmccuk/0452dada062e040c88490e5bc7f20529

In this Demo, I’ll cover the following:

• Create a simple bash script to output the date to a log file.
• Setup the basic systemd service unit file.
• Reload systemd to pick up the new service.
• Enable, then start the service.
• Stop and restart the service.
• Test it with a server reboot.

There’s no documentation link for this. Please use google to search for anything specific. There are a lot of options available to you may need to add some like the user/group depending on the requirements of your application.

All the commands and code used in the demo are below

** Subscribe and check out my other video’s! **

COMMANDS:

vi simple_service.sh
**** NOTE: the cheveron symbol isn’t allowed in the description! Replace /”chevron/” below with the greater-than/redirect symbol. ****

#!/bin/bash
for i in {1..100}
do
echo $i /”- /” `date` Chevron /tmp/simple.log
sleep 5
done

Sudo or root access for this section.

As root:
vi /etc/systemd/system/simple.service
[Unit]
Description=service to check the time
After=network.target

[Service]
ExecStart=/bin/bash /home/ec2-user/how2/simple_service/simple_service.sh

[Install]
WantedBy=multi-user.target

Then run these commands:

systemctl daemon-reload
systemctl start simple.service
systemctl enable simple.service
systemctl status simple.service

Tail the log file. Is it working?

Reboot the server.

Check if it’s working?

Subscribe for more videos like this.

Please take the opportunity to connect and share this video with your friends and family if you find it useful.