How to Automatically Run a Command on Ubuntu Startup

Sina MokhtariSina Mokhtari
3 min read

Various Linux distributions, unlike other available operating systems, offer numerous attractive features and capabilities for their users. One of these features is the ability to configure your operating system in any desired manner and automate it to perform any computation automatically. In this guide, we will discuss how to set up Ubuntu Linux so that it runs a specific command automatically after each reboot without any manual intervention.

Automatically Running a Command Using cron

To automatically run a specific command, we will use cron. cron is a Linux service that allows us to schedule specified programs to run at particular times. By configuring this service, you no longer need to perform repetitive tasks manually. It is worth mentioning that the application of cron is not limited to Ubuntu and is also used in servers. In fact, its primary application is related to servers, but since we are currently talking about personal computers, we will not delve into server usage.

Setting up a Command in crontab

To register a command for execution, follow these steps. Note that Ubuntu 24.04 is used in this tutorial.

We will begin with the assumption that we want to set up the following command in crontab to run each time the system starts:

sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0

First, access crontab by running the following command:

sudo crontab -e

Once you access the required settings, you will see that the file already contains sufficient and useful explanations that can be used to set up various commands. These explanations are fully provided below:

# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command

As indicated in these instructions, for more information, you can refer to the manual pages of crontab(5) and cron(8).

To configure the command to run every time the system starts, prefix the desired command with @reboot :

@reboot [command]

In our example, it becomes:

@reboot sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0

After adding the command, save it using ctrl+s and exit the crontab environment using ctrl+x.

Summary

In this guide, we discussed how to configure a Linux system to automatically run a specific command each time it starts. We utilized cron and crontab for this purpose and completed the tutorial on the Ubuntu 24.04 platform. We hope the information provided has been helpful. If you have any questions, ambiguities, or issues regarding this topic, please mention them in the comments, and we will respond.

0
Subscribe to my newsletter

Read articles from Sina Mokhtari directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sina Mokhtari
Sina Mokhtari