VPSmini - Adding a common service on boot on Ubuntu/Debian
Adding a common service on Ubuntu/Debian OS
In order to add a service on boot, we are using our own CLI tool called servicectl
.
It is as easy to use as this:
sudo servicectl -e <service_name>
(Make sure the service is already running first with service <service_name> start
)
So for mariadb, it would be:
sudo servicectl -e mariadb
To get more information on how to use servicectl, you can always display the full help:
sudo servicectl -h
Usage:
--enable, -e [service] Enable a service
--disable, -d [service] Disable a service
--start, -s Start all enabled services
--list, -l List all enabled services
--help, -h Display this help message
Creating and Managing Custom Services
Creating custom services allows you to run your own scripts or applications as services.
Creating a Custom SysVinit Service
Create the Init Script:
sudo nano /etc/init.d/myservice
Add Script Content:
#!/bin/sh
### BEGIN INIT INFO
# Provides: myservice
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: My Custom Service
### END INIT INFO
case "$1" in
start)
echo "Starting myservice"
/usr/local/bin/myscript.sh &
;;
stop)
echo "Stopping myservice"
pkill -f /usr/local/bin/myscript.sh
;;
*)
echo "Usage: /etc/init.d/myservice {start|stop}"
exit 1
;;
esac
exit 0
Make the Script Executable:
sudo chmod +x /etc/init.d/myservice
Enable the Service:
sudo update-rc.d myservice defaults
Start the Service:
sudo service myservice start
Managing Custom Services with SysVinit
- Check the status of the custom service:
sudo service myservice status
- Stop the custom service:
sudo service myservice stop
- Restart the custom service:
sudo service myservice restart
Updated on: 25/02/2025
Thank you!