Showing posts with label networking. Show all posts
Showing posts with label networking. Show all posts

Thursday, April 30, 2026

How to run a command on boot or startup in Ubuntu.


https://linuxvox.com/blog/automatically-run-a-program-on-startup-under-linux-ubuntu/

We start by creating a .sh file in /usr/bin/mycommand.sh
 
Enter the following text in it.

#!/bin/sh
sudo ip  route add 10.30.24.0/24 via  192.168.0.1 dev eth0 onlink

Creating a new service:
sudo nano /etc/systemd/system/mycommand.service # create new service file

Enter the following text in it.

[Unit]
Description=Add route command
After=network.target

[Service]
Type=oneshot
ExecStart=/bin/bash /usr/bin/mycommand.sh
User=root #running as root user
Restart=never # do not restart the service if it fails

[Install]
WantedBy=multi-user.target

Starting the service:
sudo systemctl daemon-reload # reload the system daemon
sudo systemctl start mycommand.service # to start without rebooting for testing
sudo systemctl status mycommand.service # to verify status of service.
sudo systemctl enable mycommand.service # enable the service to run at startup


Removing a systemdservice
Sudo systemctl stop mycommand.service # stop the service
Sudo systemctl disable mycommand.service # disable the service
Sudo rm /ect/systemd/system/mycommand.service