Running a Command on Boot in Linux

Running specific commands or scripts automatically at boot can be useful for starting applications, running maintenance tasks, or configuring system settings every time your system starts. In this guide, we’ll walk you through the process of setting up a command to run at boot on a Linux system using systemd.

Step 1: Access Your Terminal

First, you’ll need to access the terminal on your Linux system. You can do this directly on your device or via SSH if you have remote access set up.

Step 2: Create a Systemd Service File

Most modern Linux distributions use systemd to manage services. We’ll create a new service file for your command.

  1. Open a terminal.
  2. Create a new service file in the /etc/systemd/system/ directory. You can name it something like mycommand.service.
sudo nano /etc/systemd/system/mycommand.service

Step 3: Define the Service

In the service file, specify how the service should run. Here’s a template you can use:

[Unit]
Description=Run my custom command at boot
After=network.target

[Service]
ExecStart=/path/to/your/command
Restart=always
User=root

[Install]
WantedBy=multi-user.target
  • Description: A brief description of the service.
  • After: Specifies the service dependencies. Here, it ensures the network is up before the command runs.
  • ExecStart: The command or script you want to run.
  • Restart: Configures the service to restart if it fails.
  • User: Specifies the user under which the command should run.
  • WantedBy: Defines the target under which the service should be started.

Replace /path/to/your/command with the actual command or script you want to execute.

If you’re running CasaOS, here’s a template you can use, tailored to ensure it runs after casaos.service:

[Unit]
Description=Run my custom command at boot
After=casaos.service

[Service]
ExecStart=/path/to/your/command
Restart=always
User=root

[Install]
WantedBy=multi-user.target
  • Description: A brief description of the service.
  • After: Ensures the command runs after the casaos.service has started.
  • ExecStart: The command or script you want to run.
  • Restart: Configures the service to restart if it fails.
  • User: Specifies the user under which the command should run.
  • WantedBy: Defines the target under which the service should be started.

Replace /path/to/your/command with the actual command or script you want to execute.

Step 4: Save and Exit the Nano Editor

Once you’ve defined your service, you’ll need to save your changes and exit nano:

  1. Press Ctrl+O (write out) to save the file.
  2. Press Enter to confirm the file name.
  3. Press Ctrl+X to exit the editor.

Step 5: Reload systemd to Recognize the New Service

After creating and saving the service file, reload systemd to apply the changes:

sudo systemctl daemon-reload

Step 6: Enable the Service to Run at Boot

Enable the service so it runs automatically at boot:

sudo systemctl enable mycommand.service

Step 7: Start the Service Immediately (Optional)

If you want to start the service without rebooting, start it manually:

sudo systemctl start mycommand.service

Step 8: Verify the Service Status

Check the status of your service to ensure it’s running correctly:

sudo systemctl status mycommand.service

Troubleshooting

If your service doesn’t start as expected, check the logs for any errors:

journalctl -u mycommand.service

Conclusion

By following these steps, you can configure your Linux system to run any command or script at boot using systemd. This method is powerful and flexible, allowing you to automate various tasks and ensure your system is always configured exactly how you need it at startup.

Feel free to reach out with any questions or share how you’re using systemd services to enhance your Linux setup!