Linux Commands

How do I delete a cron job in Linux?

cron:

A built-in Linux utility is used for scheduling the process execution on your system, known as “cron” daemon. cron looks for the specified scripts and files in the “crontab” or “cron tables.” The crontab file contains a command list that you wish to run based on a regular schedule. In addition, it has the command names that manage the list of those commands. Crontab also employs the cron job scheduler for executing tasks. cron is utilized as a system function that will perform or execute processes on your behalf according to the predetermined schedule. This schedule is called crontab, and the tool used to edit it likewise named crontab.

Using crontab in Linux:

crontab is a popular task scheduler included in the Linux system pack as it schedules a process to execute as root. As a result, launching an automated operation as root simplifies system updates. All you have to do is to change the cron job and wait for it to be re-started.

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.

That’s all the essential information about crontab. Now, let’s move forward to see how we can delete a cron job in Linux.

Deleting a cron job:

To demonstrate the procedure of deleting a cron job, first of all, we will add a job to crontab using the following command:

$ (crontab -u linuxhint -l ; echo "*/5 * * * * perl /home/linuxhint/test.pl") | crontab -u linuxhint -

This command a new cron job for the user “linuxhint” for listing the cron jobs information in the “test.pl” file after every “5” minute.”

Verify the existence of the newly create cron file.

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

Write-out the below-given command to remove cron job from the crontab.

$ crontab -u linuxhint -l | grep -v 'perl /home/linuxhint/test.pl'  | crontab -u linuxhint -

Using crontab -r command:

The crontab file protections have been settled up so that you can’t remove a crontab file by accident with the rm command. For removing crontab files, utilize the crontab -r command instead.

$ crontab -r

Again check out the crontab for ensuring that the cron job is deleted or not.

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

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. However, deleting any cron job can be a tricky task while surrounding crontab. This article provided you a step-by-step method for deleting a cron job in Linux.

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.