There are many occasions when we need to run a bash script every time a Linux server boots. This can be particularly helpful for tasks such as mounting storage, starting services, and other essential Linux configurations.

Here are the steps to set up bash script execution on server boot using Debian 12.

Step 1 : Example you create the bash command on one file, create a bash file and write your needs on that bash file.

Step 2 : Make sure the file is executable

Step 3 : Create a new file called init-boot.service in the /etc/systemd/system/ directory

Step 4 : Add the following content to the service file

Type=oneshot: Ensures the service runs the script once and then stops.

User=mylinuxuser: User who will be run the script.

RemainAfterExit=true: Keeps the service in active state after the script finishes execution.

Step 5, Save and close the file

Step 6, Reload systemd to recognize the new service:

Step 7, Enable the service to start at boot:

Step 8, Start the service immediately (optional):

Step 9. Check the status of the service to ensure it is running correctly:

sudo systemctl status init-boot.service

This setup ensures that /var/app/script/init-boot.sh runs once every time the system boots, using the mylinuxuser user. The RemainAfterExit=true directive ensures that the service is marked as active after the script completes, preventing it from being restarted by systemd.

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.