How to Automate & Schedule a Task using Crontab in Ubuntu

Crontab is an abbreviation for cron table is a configuration file used to schedule shell commands to run periodically.

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
Hend Adel
Hi! I'm Hend Adel, a freelancer technical geek with successful experience in Database, Linux and many other IT fields. I help to build solutions to suit business needs and creating streamlined processes. I love Linux and I'm here to share my skills via FOSS Linux! Thanks for reading my article.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

24,236FansLike
192FollowersFollow

LATEST ARTICLES

How to create a bootable CentOS Live USB drive on Windows

create centOS Live USB drive
CentOS ISO downloads are available in two different variations - Minimal ISO and DVD ISO. So what are these? In this guide, you will know how to create a CentOS Live USB drive that can also be used to install CentOS.
clean virus ubuntu live CD
In this tutorial, we are going to show you how to clean your Windows machine from infected viruses using an Ubuntu live USB or CD and the ClamAV antivirus. The ClamAV is a free, open-source antivirus that can be used on Ubuntu.
best linux distros beginners
Making the shift to Linux is not as complicated as some people make it out to be. There is a variety of Linux distributions available that cater to the needs of people who are planning on making the jump to Linux from any other operating system.
screen capture apps linux
Do you want to make a video tutorial for YouTube or show-off your epic victory royale to your friends? Look no further — as we have compiled the 10 Best Screen Capturing Software for Linux. Whether you need to record your desktop screen or take a simple screenshot, FOSSLinux has got your back.