How to run a command on startup in Ubuntu?

In this post we are going to look at how to run a command on startup using cron job scheduler in Ubuntu 18.04.

What is cron?

Cron is a Linux scheduler that allows us to run commands at a specific time. It is very flexible and allows to specify time, date, day of the week, year. Besides that we can use cron to run a command on system startup. Now we will see how to run a command on startup.

How to run a command on a startup?

Option 1. Edit /etc/crontab directly

Cron uses configuration file /etc/crontab where we can specify which commands we want to run on a startup.

The /etc/crontab file in Ubuntu 18.04 has the following default content:

orkhans@matrix:~$ cat /etc/crontab 
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user	command
17 *	* * *	root    cd / && run-parts --report /etc/cron.hourly
25 6	* * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6	* * 7	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6	1 * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#

 

The command we are going to run on startup is date >> date.txt. You can execute this command in your terminal and see that it appends current time to the file date.txt:

orkhans@matrix:~$ date >> date.txt
orkhans@matrix:~$ cat date.txt
Sun Jun 24 14:09:47 +04 2018
orkhans@matrix:~$ date >> date.txt
orkhans@matrix:~$ cat date.txt
Sun Jun 24 14:09:47 +04 2018
Sun Jun 24 14:10:00 +04 2018

 

So we want to run this command every time our operating system boots.

Now let’s add this command to our cron configuration file /etc/crontab and make it look like this:

orkhans@matrix:~$ cat /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user	command
17 *	* * *	root    cd / && run-parts --report /etc/cron.hourly
25 6	* * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6	* * 7	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6	1 * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
@reboot root date>>/home/orkhans/date.txt

 

We have added the last line to /etc/crontab: @reboot root date>>/home/orkhans/date.txt

Keyword @reboot  tells the scheduler that we want this command to run at every reboot.  root specifies that we want to run this command as a root user.

After you save the file you can reboot your computer and check the contents of the date.txt file to find the appended line with time.

 

Option2. Add a command to a user’s crontab file

If you want to edit the /etc/crontab file you will need root permissions because it is a system-wide configuration file, but there’s another way to schedule cron tasks for those who do not have root privileges.

Each user has its own crontab file and we can view its contents by running crontab -l :

orkhans@matrix:~$ crontab -l
no crontab for orkhans

As you can see my crontab file is empty, and now we will add our date >> date.txt command to it.

Command crontab -e opens up a text editor so we can add  @reboot date>>date.txt to our crontab file.  After you save the file you can check its contents again:

orkhans@matrix:~$ crontab -l
# 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
@reboot date >> date.txt

Now you can restart your computer and check the date.txt file to make sure that your cron job really works.

 

Thank you for reading!

Add a Comment