Skip to content

Instantly share code, notes, and snippets.

@dmccuk
Created September 15, 2021 07:10
Show Gist options
  • Save dmccuk/0452dada062e040c88490e5bc7f20529 to your computer and use it in GitHub Desktop.
Save dmccuk/0452dada062e040c88490e5bc7f20529 to your computer and use it in GitHub Desktop.
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.
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.
• Root access is required.
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
````
#!/bin/bash
for i in {1..100}
do
echo $i "- " `date` > /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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment