How to Run a Script at Startup on Linux
Table of Contents
Linux is known for its flexibility and customization, which means that users have a lot of control over their systems. One of the tasks that users may want to accomplish is running a script at startup. This can be useful for automating tasks or configuring settings that need to be set each time the system is booted up. There are several ways to run a script at startup on Linux, and each has its own advantages and disadvantages. In this how-to guide, we will explore some of the most common methods.
Using the init system
The init system is a fundamental part of the Linux operating system and is responsible for managing the boot process. The most common init systems are systemd and SysVinit. Each of these systems has its own way of running scripts at startup.
With systemd, the easiest way to run a script at startup is to create a unit file in the /etc/systemd/system/ directory. This file should contain information about the script, such as its location, what it does, and when it should be run.
Step 1: Open Text Editor
Open a text editor of your choice, such as nano or Vim.
Step 2: Create File
Type the following text into the editor:
[Unit]
Description=My Startup Script
After=network.target
[Service]
ExecStart=/path/to/my_script.sh
[Install]
WantedBy=default.target
The Description field is a brief description of your script.
The After field specifies that the script should be run after the network has been started.
The ExecStart field specifies the command to run your script. Replace /path/to/my_script.sh with the actual path to your script.
The WantedBy field specifies the target that the service should be started with. In this case, default.target will start the script during the default startup process.
Step 3: Save File
Save the file as myscript.service in the /etc/systemd/system/ directory.
Step 4: Reload systemd daemon
To reload the systemd daemon, run:
sudo systemctl daemon-reload
Step 5: Enable at Boot
To enable the service to start at boot time, run:
sudo systemctl enable myscript.service
Your script will now run automatically at startup.
Step 6: (Optional) Start/Stop Script
To start or stop the script manually, run:
sudo systemctl start myscript.service
sudo systemctl stop myscript.service
Using cron
Cron is a time-based job scheduler on Linux that allows users to schedule tasks to run at specific times or intervals. This can also be used to run a script at startup.
Step 1: Edit crontab file
To create a cron job, you can edit the crontab file by running:
crontab -e
Then, add a line to the file that specifies when the script should be run. For example, to run the script every time the system is booted up, you can use the @reboot keyword:
@reboot /path/to/script.sh
Using the .bashrc file
The .bashrc file is a script that is executed each time a user logs into a shell. This can be used to run a script at startup for a specific user.
Step 1: Edit .bashrc file
To edit the .bashrc file, you can use any text editor, such as nano or Vim. For example, run:
nano ~/.bashrc
Then, add a line to the file that specifies the script to be run. For example:
/path/to/script.sh
It’s important to note that this method will only run the script for the user that has the .bashrc file configured.
Using the GUI
Finally, some Linux distributions provide a graphical interface for managing startup applications. This can be a convenient way to run a script at startup, as it allows users to easily manage their startup applications without having to use the command line.
For example, on Ubuntu, you can use the “Startup Applications” tool, which can be found in the Dash by searching for “Startup Applications.” Once the tool is open, you can click the “Add” button to add a new startup application. You will need to specify the name of the application, the command to run the script, and any other options that are required.
Conclusion
Running a script at startup on Linux can be a useful way to automate tasks and configure settings that need to be set each time the system starts up. There are several ways to achieve this, including using system-wide or user-specific startup scripts, using cron jobs, or using systemd services. Each method has its own advantages and disadvantages, depending on the specific requirements of the script and the system configuration. By choosing the right method and properly configuring the startup script, Linux users can save time and streamline their workflow, ensuring that their system is always configured the way they need it to be.