Skip to main content

Lab 8.2 - Creating Custom systemd Services

Objectives​

  • Create a .service file
  • Configure a custom service
  • Manage the service

Exercise 1: Create a Service​

# Create a script
cat > /usr/local/bin/mon-service.sh << 'EOF'
#!/bin/bash
while true; do
echo "$(date): Service actif" >> /var/log/mon-service.log
sleep 60
done
EOF

chmod +x /usr/local/bin/mon-service.sh

# Create the service
sudo nano /etc/systemd/system/mon-service.service

Service content:

[Unit]
Description=My Custom Service

[Service]
ExecStart=/usr/local/bin/mon-service.sh
Restart=always

[Install]
WantedBy=multi-user.target
# Enable
sudo systemctl daemon-reload
sudo systemctl enable mon-service
sudo systemctl start mon-service

Lab created on: December 2024 - Tested on Ubuntu 24.04