How To Run A Command Or Script As Root On Startup / Boot Using systemd or A Cron Job

Linux run command or script as root (sudo) on startup / boot

This article explains how to run a command or script at startup / boot as root on Linux, in two ways: using systemd or a cron job.


How to use systemd to run a command or script as root on boot


To use systemd to run a command or script as root when your computer boots, create a file (as root) called mycommand.service (replace mycommand with whatever you want to call it) in /etc/systemd/system/.

We can use Nano command line text editor to open / create this file:

sudo nano /etc/systemd/system/mycommand.service

In this file, paste the following:

[Unit]

Description=your description

[Service]

ExecStart=/path/to/command/or/script

[Install]
WantedBy=multi-user.target

Here, change the Description value to describe what this does, and the ExecStart value to the command or path of the script you want to run as root on startup. Don't add sudo at the beginning of the command or script, because it runs as root anyway.

Now save the file and exit Nano. In case you're not familiar with Nano text editor, you can save the file by pressing Ctrl + o, then Enter. Exit by pressing Ctrl + x.

Next, you need to enable the systemd service to run on boot, using the following command:

sudo systemctl enable mycommand.service

Remember to replace mycommand.service with the actual filename you've used for this systemd service file. There's no need to run the systemd service right now, since this is about running it on boot.

If you use this to run a script, make sure to make the script executable (chmod +x /path/to/script) or else it won't run.

This is a very simple systemd unit file that runs only once. These can be a lot more complex, depending on what you need. For example, you could use a command that runs before ExecStart, have it start only after another unit becomes active, have the command run only after another service, e.g. the network service has been started (After=network.target, while also declaring a dependency to this service using Wants= or Requires=), and more. Check out the systemd.service and systemd.unit man pages for more details.

You might also like: How To Launch Startup Applications With A Delay


How to use a cron job to run a command or script as root on startup / boot


To use a cron job to run a command or script as root when the system boots, edit the root user crontab using:

sudo crontab -e

And at the bottom of the file (it may also be empty), use the following:

@reboot /path/to/command/or/script

Now save the crontab and exit. If you've used Nano command line editor to edit it (should be default in most cases), you can save the file by pressing Ctrl + o, then Enter. Exit Nano by pressing Ctrl + x. Don't add sudo before command or script, because it runs as root anyway, since it's added to the root crontab.

In case you want to use a particular editor to edit the root crontab, run it like this: sudo EDITOR=editor crontab -e, e.g. for Vim: sudo EDITOR=vim crontab -e, or for Nano: sudo EDITOR=nano crontab -e.

A few notes about this:

  • If you use this to run a script, make sure to make the script executable (chmod +x /path/to/script) or else it won't run
  • Use the full path to the command or script, or else it may fail to run (this depends on the Linux distribution you're using, e.g. you don't need to use the full path on Ubuntu 20.04 for example)
  • If the script ran by cron also includes commands without the full path, you can avoid having to rewrite the script by adding this at the top of the crontab file: PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  • If you need to delay the start of the command / script, you can make use of the sleep command, e.g.: @reboot /usr/bin/sleep 60; /path/to/command/or/script to run the command or script 60 seconds after the system boots

You might also like: How To Run A Command After The Previous One Has Finished On Linux

Which to choose between systemd or a cron job to run a command or script as root on startup / boot, if you have a choice? When in doubt, pick systemd (if it's available on your system) because it should be more reliable and easier to use.

For example, not every version of cron supports the @reboot option, or the command / script may only run when the system is rebooted, and not when it's shut down (this didn't happen for me on Ubuntu 20.04, Fedora 24, Manjaro Linux and Debian 10, but it may happen on some Linux distributions).

It's also worth noting that @reboot configures a job to run once when the daemon is started. cron is not usually restarted, so this usually corresponds to the machine being booted. For example, Debian (and Debian-based Linux distributions) enforces this, making cron not re-run @reboot jobs when the cron service is restarted. On some Linux distributions, though, restarting the cron service may re-run the @reboot commands.

Also, on Fedora, cron is not installed by default (install it using sudo dnf install cronie). On Manjaro, cron is installed by default, but not enabled by default (enable it using sudo systemctl enable --now cronie).

You might like: How To Find All Files Containing Specific Text On Linux From The Command Line