Linux Commands

How to edit crontab with nano

A built-in Linux utility is used for scheduling the process execution on your system, known as “cron” daemon. cron is a task scheduler that runs tasks at scheduled intervals, whereas crontab is an acronym for “cron table.” It enables the usage of the cron job scheduler to conduct tasks or processes. Crontab is also the name of the application that is utilized for making changes in the task schedule. It is controlled by a crontab file, a configuration file that specifies shell commands to execute the scheduled tasks.

You can use a cron job to automate operations like upgrading the system with the latest security patches, backing up databases or data, sending emails, and checking the disk space usage. cron can set tasks to execute every minute, hour, day, month, weekday, or by specifying any combination of dates.

Why use cron jobs?

cron jobs are helpful in Linux for the following reasons:

  • assists the operating system is taking a scheduled backup of log files or databases.
  • A crontab is an excellent tool for automating Unix tasks.
  • A tool that is used for automating system maintenance.
  • To delete old log files.
  • Useful for sending email notifications, password expiration emails, or newsletters.
  • To purge and archive database tables.

Syntax of the crontab:

Each field in a crontab file exists in the following order:

minute(s) hour(s) day(s) month(s) weekday(s) command(s)

Here is the syntax of the crontab command:

*****  /path_to_script
  • Use asterisks (*) for matching.
  • Specify range: With the use of a hyphen, you may define a range such as 1-20, 60-80, or mon-wed, Jan-mar.
  • Define multiple ranges: it also permits you to define numerous fields separated by a command, such as Jan-mar, April-July.

Install cron:

Write out the below-given command for installing cron on your Linux system.

$ sudo apt install cron

After that, enable the cron to autostart.

$ sudo systemctl enable --now cron

Editing crontab with nano:

To demonstrate to you the procedure of editing any crontab file with nano, we will create a crontab job. Then, a Linux user can use the “crontab -u USERNAME -e” command for editing the crontab file with nano editor.

Let’s utilize the “crontab -e” command to add a cron job for the current “linuxhint” user in the crontab configuration file.

$ crontab -u linuxhint -e

Every Sunday at 1:00 a.m, the following command entry is added to a new crontab file which will delete any log files from the “linuxhint” home directory. In addition, redirect characters are added to the command line after *.log so that the command entry does not redirect output. This guarantees that the command is properly executed.

1 0 * * 0 rm /home/linuxhint/*.log > /dev/null 2>&1

Press “CTRL+O.” It will write out the added content in the crontab file.

Now, verify the newly created cron job of the “linuxhint” user by utilizing the following command.

$ sudo ls -l /var/spool/cron/crontabs

The output declares that your cron job is successfully added to the crontab.

Conclusion:

The Linux system pack includes crontab, a helpful job scheduler that can perform an automatic procedure as root. The crontab contains cron jobs that assist the operating system while taking a scheduled backup of log files or databases. You can create multiple cron jobs in the crontab file. This article provided you the method of editing crontab files with nano editor.

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.