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.
- 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.
- Create a systemd service file. Use
sudo nano /etc/systemd/system/your_service.serviceto 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
- Enable and start the service. Run
sudo systemctl enable your_service.serviceto enable your service, and
sudo systemctl start your_service.serviceto 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.
- Create your script and make it executable as described in the Ubuntu section.
- Open the crontab editor with
- Schedule your script to run at startup by adding the following line to the bottom of the file, replacing the script path as needed:
- Save and exit the crontab editor. Your script is now scheduled to run at every system startup.
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
- 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).
- Create symbolic links to the appropriate runlevel directories using the
update-rc.dcommand. This command makes sure your script gets executed whenever the system enters the specified runlevels.
sudo update-rc.d your_script.sh defaultswill work for most cases.
- 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”
what is this S99 and K99 stands for ?