Creating a Service
All services are located in /etc/systemd/system, as they depend on the libraries offered by systemd to function properly.
In order to create a service,
we must create a .service file in that directory, which can be done with this command:
mkdir myservice
cd myservice
vi example.sh
#!/bin/bash
echo "the script works"
sudo chmod 005 myservice.sh
Create service file:
sudo vi /etc/systemd/system/myservice.service
[Unit]
Description=My first service!
[Service]
User=omkar
#Code to execute
#Can be the path to an executable or code itself
WorkingDirectory=/home/omkar/myservice
ExecStart=/bin/bash /home/omkar/myservice/example.sh
Type=simple
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
Check logs: sudo journalctl --unit=myservice
Manage service using systemctl tool:
sudo systemctl daemon-reload
sudo systemctl enable mydaemon
sudo systemctl start mydaemon
sudo systemctl stop mydaemon
sudo systemctl status mydaemon