Linux is a powerful operating system known for its flexibility and automation capabilities. One of the most common requirements for system administrators and developers is to auto execute commands or scripts during reboot or startup. This automation ensures that essential services, applications, or maintenance tasks run without manual intervention each time the system boots.
In this detailed guide, we will explore multiple methods to configure Linux for executing scripts at system startup, reboot, login, and logout. We will cover both traditional approaches (like cron
and rc.local
) and modern systemd-based methods, making this tutorial distro-agnostic and useful for all Linux users.
Why Auto Execute Commands on Linux?
Setting up Auto Execute Commands is beneficial in many scenarios:
Service Initialization: Automatically start web servers, databases, or monitoring tools.
System Maintenance: Schedule backups, cleanup jobs, or disk checks at startup.
Custom Configurations: Load firewall rules, mount storage drives, or tune system parameters.
Developer Productivity: Start containers, environments, or applications automatically.
Security Enforcement: Apply access controls or security scripts at every boot.
Without automation, administrators would need to manually repeat these tasks after every reboot, which is inefficient and prone to human error.
Methods to Auto Execute Commands at Reboot or Startup
Linux offers several ways to run commands automatically during boot or startup. Below are the most commonly used methods:
1. Using Cron’s @reboot
Directive
The cron scheduler is widely used in Linux to automate recurring tasks. It also provides a special keyword @reboot, which allows you to run a script once every time the system boots.
Steps:
Open crontab for the desired user:
crontab -e
- Add an entry with
@reboot
followed by the full path of your script:
@reboot /home/user/myscript.sh
- Save and exit.
Important Notes:
Ensure the script has execute permissions:
chmod +x /home/user/myscript.sh
If your script depends on environment variables, explicitly define them in the script or inside the crontab entry.
This method works well for lightweight tasks and is simple to implement.
2. Using /etc/rc.d/rc.local
File
The rc.local file is a traditional method for executing commands at startup. Though some modern systemd-based distributions have deprecated it, it can still be enabled manually.
Steps:
Open the rc.local file (create it if it doesn’t exist):
sudo nano /etc/rc.d/rc.local
- Add your script or command at the end of the file:
/home/user/myscript.sh
3. Using Systemd Service Units (Recommended for Modern Linux)
Most modern Linux distributions (Ubuntu, CentOS, Fedora, Debian, etc.) use systemd as the init system. The best practice today is to create a systemd service for your script.
Steps:
Create a service file under
/etc/systemd/system/
:
sudo nano /etc/systemd/system/myscript.service
- Add the following configuration:
[Unit] Description=Run My Script at Startup After=network.target [Service] ExecStart=/home/user/myscript.sh Restart=on-failure User=user [Install] WantedBy=multi-user.target
- Reload systemd and enable the service:
sudo systemctl daemon-reload sudo systemctl enable myscript.service sudo systemctl start myscript.service
With this method, you gain fine-grained control:
Start, stop, or check status of the script like any other service.
Define dependencies (e.g., run only after the network is available).
Ensure reliability with automatic restarts.
This is the officially recommended approach for auto-executing commands in modern Linux systems.
4. Executing Scripts at User Logon and Logout
Sometimes, you may want to run commands not at boot, but at user login or logout.
At Login: Add your script to
~/.bash_profile
or~/.profile
. Example:
/home/user/myscript.sh
- At Logout: Use
~/.bash_logout
. If the file doesn’t exist, create it. Example:
/home/user/cleanup.sh
This is useful for personal scripts, such as setting environment variables or cleaning up temporary files.
Example Scripts
Example 1 – Logging Date & Time at Startup:
#!/bin/bash DATE=$(date +'%F %H:%M:%S') echo "System booted at: $DATE" >> /home/user/bootlog.txt
Example 2 – Starting a Custom Application:
#!/bin/bash /usr/bin/python3 /home/user/myapp.py &
Don’t forget to give execute permissions:
Best Practices for Auto Execute Commands
Always provide absolute paths for scripts and binaries.
Ensure scripts have correct permissions (
chmod +x
).Test scripts manually before enabling them at boot.
Use systemd services for critical or long-running applications.
Log output to a file for easier debugging.
Summary
In this article, we explored different ways to auto execute commands or scripts during reboot, startup, logon, and logout in Linux systems.
Cron’s @reboot → Simple for small tasks.
rc.local → Legacy method, still works in many systems.
Systemd services → Modern, powerful, and recommended.
.bash_profile & .bash_logout → For user-specific login/logout automation.
By implementing these methods, you can automate routine tasks, enhance system reliability, and boost productivity.
Frequently Asked Questions (FAQ)
Q1. What is the best method to auto execute commands in modern Linux?
The recommended method is to use systemd service units because they offer reliability, dependency management, and are supported across modern distributions.
Q2. Does rc.local still work in Ubuntu or CentOS?
Yes, but it is deprecated by default. You can re-enable it manually, though systemd is the preferred approach.
Q3. How do I run multiple scripts at startup?
You can either add multiple entries in crontab with @reboot
, list multiple scripts in rc.local, or create separate systemd service files for each script.
Q4. Can I run commands only when a specific user logs in?
Yes. Add commands to the user’s ~/.bash_profile
(for login) or ~/.bash_logout
(for logout).
Q5. How do I debug if my script does not run at startup?
Check file permissions.
Use absolute paths.
Redirect script output to a log file.
For systemd, check logs with:
journalctl -u myscript.service
Conclusion
I hope that now you have a good understanding of How to Auto Execute Commands and Scripts at Reboot or Startup on Linux.
If anyone does have any questions about what we covered in this guide then feel free to ask in the comment section below and I will do my best to answer those.
Suggested Read:
- How to Kill a Process (Terminate a running process) in Linux
- How Linux handles Processes
- How to manipulate Process Priority in Linux using nice and renice Commands
- Linux Networking Commands for Sysadmins: A Complete Guide
- Most Useful Linux Ping Command (Ping Utility) With Examples
- Best Linux Ifconfig Command With Examples
- How To Install Wireshark Network Analyzer In Ubuntu – A Best Network Traffic Analyzer For Linux
- Setup Netdata For Real Time Performance Monitor In Linux System