How to Run a Linux Script on Startup: A Detailed Guide

linux, shell bash, ubuntu

Running a script or application at Linux startup can be a major time-saver for system administrators and users who need certain tasks to execute automatically upon booting up their systems. Whether you are looking to perform routine maintenance tasks, start applications, or automate complex processes, understanding how to run a script at startup in Linux is a valuable skill. This guide will provide instructions on how to achieve this in the top three Linux distributions: Ubuntu, CentOS, and Debian.

Ubuntu: Running a Script at Startup

Ubuntu, a Debian-based Linux operating system, is one of the most widely used distributions due to its user-friendly nature. With Ubuntu, you can run a script at startup using the systemd service.

  1. Create your script. Save it to your desired location, for example, /usr/local/bin/your_script.sh. Ensure the script is executable with the command chmod +x /usr/local/bin/your_script.sh.
  2. Create a systemd service file. Use sudo nano /etc/systemd/system/your_service.service to create and open a new service file. Add the following content, adjusting as necessary:
[Unit]
Description=Your Service Description

[Service]
ExecStart=/usr/local/bin/your_script.sh

[Install]
WantedBy=multi-user.target
  1. Enable and start the service. Run sudo systemctl enable your_service.service to enable your service, and sudo systemctl start your_service.service to start it. Your script should now run at startup.

CentOS: Automate Scripts at Startup

CentOS is a popular choice for web servers due to its robustness and stability. Here, we’ll use a different method—crontab—to automate scripts at startup.

  1. Create your script and make it executable as described in the Ubuntu section.
  2. Open the crontab editor with crontab -e.
  3. Schedule your script to run at startup by adding the following line to the bottom of the file, replacing the script path as needed: @reboot /usr/local/bin/your_script.sh
  4. Save and exit the crontab editor. Your script is now scheduled to run at every system startup.
See also  Linux: How to Kill all instances of a script/program by using a string search

Debian: Initiate Scripts on Startup

Debian, one of the oldest and most respected Linux distributions, uses the traditional init system (SysVinit). This system requires you to place your startup scripts in the /etc/init.d directory.

  1. Create your startup script and place it in the /etc/init.d/ directory, for example /etc/init.d/your_script.sh. Make sure it’s executable (chmod +x /etc/init.d/your_script.sh).
  2. Create symbolic links to the appropriate runlevel directories using the update-rc.d command. This command makes sure your script gets executed whenever the system enters the specified runlevels. sudo update-rc.d your_script.sh defaults will work for most cases.
  3. Reboot your system to test if your script is running at startup.

Remember, using startup scripts can significantly increase your productivity and efficiency, automating routine tasks so you can focus on more important ones. Remember to always test your scripts and services to ensure they’re functioning as expected after the system reboots.

Be sure you check out some of our other Linux Tips articles such as Insert/Add String to Beginning of a File.

1 thought on “How to Run a Linux Script on Startup: A Detailed Guide”

Leave a Comment