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


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


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


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


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.


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.


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.


Please enter your comment!
Please enter your name here




Top 10 Reasons to use Xfce as your Desktop Environment

There are many choices for desktop environments for Linux based operating systems. Mainly, you can install any DE of your choice on most of the Linux based distributions, even if they are not offered as a package officially. In our recent articles, we discussed the best of KDE and Cinnamon. In this article, we wish to present to you the top reasons why you should consider Xfce as your desktop environment.

The 10 Best Linux Network Monitoring Tools

Having total control over your network is essential to prevent programs from overusing your network resources and slowing down the overall performance. This is why you should install a network monitoring tool on your system, giving you a visual overview of everything that's happening on your network. Networking Tools are like swiss-knife for the system administrators for troubleshooting system issues.

How to install CouchPotato on Ubuntu

Downloading movies and copying them over to your home server can get frustrating, especially if you are doing it daily! What if you have an option to download videos automatically, quickly, and above all with excellent quality. CouchPotato allows you to download movies easily once they are available and released automatically.

Top 20 Rsync Examples in Linux

The Rsync (remote sync) command is a Linux/Unix utility used to synchronize and copy files and directories either locally or remotely. Most Linux geeks use Rsync to mirror, backup or migrate data across folders, across disks and networks. One notable feature with the Rsync command is that it uses the "delta transfer algorithm."

Setting up NFS Server on Ubuntu Server

We have put together a detailed step-by-step tutorial that will guide you on how to install and set up NFS Server on Ubuntu so you too can start sharing data with other client systems. It is divided into sections, the first part discussing how to set up the host server and the second part showing you how to set up the client-side. For this tutorial, we will be using Ubuntu 20.04 LTS.

Crontab in Linux Explained With Examples

Crontab is the Linux equivalent of the Window's Task Scheduler. It can help you set up a task to run automatically at a regular...


Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.
Elementary OS 5.1 Hera has received a point release with a handful of new features and bug fixes, and we will be reviewing the significant changes in this article. For those new to elementary OS, this Ubuntu-based Linux distribution uses their inhouse built Pantheon desktop environment and AppCenter.

How to dual-boot Ubuntu and Fedora on your PC

Every Linux distribution has its capabilities and preferences as per the user requirements. That brings forth the need to run different distros on one laptop. For example, a CyberSecurity enthusiast might prefer to use Ubuntu for development and Kali Linux for penetration testing.

Top 5 reasons to switch from Windows to Linux right now

Do you love Windows or Linux? Being on a Linux website, it's not hard to take a guess, but what are the strong reasons to switch to Linux? Linux is a 100% free OS. Is that reason alone enough? Let's find out! Never have there been more compelling reasons to make the switch. Here are just five great reasons for users to create that magical move from the wayward world of Windows to the lauded land of Linux.

How to clone hard disk on Linux using Clonezilla

Disk cloning refers to the process of copying data from one disk to another, thus creating a one-to-one copy of the drive. Technically, this process is possible using the copy-and-paste method.

5 Things to do when your Linux system GUI freezes

All operating systems freeze at some point and, Linux distributions are no exceptions. In this post, we are going to look at five methods you can apply to your frozen system before thinking of plugging out the power cable or holding down the power button to power off the PC.