Home Terminal Tuts How to Automate & Schedule a Task using Crontab in Ubuntu

How to Automate & Schedule a Task using Crontab in Ubuntu

by Hend Adel
schedule tasks using crontab in ubuntu

Sometimes you need to back up your important files weekly, run a command to monitor system daily, or even run a script at night automatically without having to stay up late to execute it.

Automating tasks on Ubuntu can be done through a package called cron. Crontab is an abbreviation for cron table and is a configuration file used to schedule shell commands to run periodically. Usually, the crontab file is stored in the /etc or a directory under /etc. Only system administrators with root privileges can edit the crontab file.

In this article, we will walk you through crontab file and how-to setup a cron job on Ubuntu easily.

Automate and Schedule Task using Crontab in Ubuntu

First, you have to make sure that your system is up to date by using the following commands:

sudo apt-get update

After entering the sudo password, the output should be something like that:

System update output preview

System update output preview

sudo apt-get upgrade

Output:

System upgrade output preview

System upgrade output preview

Press Y and wait until upgrade finish.

Now, check if the cron package is installed:

dpkg -l cron

Output:

Cron package installed

Cron package installed

In case the cron package is not installed then try installing it:

sudo apt-get install cron

Check if the cron service is running:

systemctl status cron

Output:

Cron service status

Cron service status

If it is not running try starting it:

systemctl start cron

Open the crontab file using your favorite editor:

sudo vi /etc/crontab

Output:

Crontab file

Crontab file

To edit in the crontab file you need to be careful and follow the syntax which is:

Minute      Hour      day-of-Month       Month    Day-of-Week       user       command

So, let’s say for example you need to create a directory and move a backup file to it. And you need this task to be run daily at 2.20. Then edit the crontab file and add the following line:

20              2             *              *              *              hendadel              /bin/mkdir testdir && mv backupfile testdir/

Note that, asterisk (*) in the crontab file means all valid values.

Edit crontab file

Edit crontab file

Next restart the cron service:

systemctl restart cron

And voila, the directory was created and the log file was moved successfully.

Output:

testdir created

testdir created

You can redirect the output of the cron job into a separate file and this can help you in case the cron job generated an error. For example, let’s run the previous job again with adding a redirection to the output to a separate file called logfile (obviously it will give an error cause the directory already exist). Do not forget to change the job time.

Output:

Edit crontab file to redirect error

Edit crontab file to redirect error

Check the error written in the logfile:

Error written to logfile

Error written to logfile

Finally, I hope you find this tutorial helpful, and if you need to get more information about the cron or crontab you can check their manual pages. Enter the following commands in the Terminal.

man cron
man crontab

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.