Disclosure: This article may contain affiliate links. When you purchase, we may earn a commission.

10 examples of crontab commands in Linux

In Today's article, You shall be learning the 10 examples of crontab commands in Linux. The Objectives are : what is the meaning of cron, what it is used for and when is it used in Linux.

Now, What is ‘crontab’ Command

Crontab command is derived from cron table and can also be referred to as that. Cron schedule jobs that is time based. Cron jobs are used to executes tasks at scheduled intervals.

The cron table syntax command has six fields that is separated by space. Now we shall be checking it one by one.

 Minutes - for values between 0-59

 Hour - for values between 0-23

 Day of the month - for values between 1-31

 Month of the year - for values between 1-12 in respect of Jan - Dec

 Day of the week - for values between 0-6 in respect to Sun - Sat

 And the last option is the command.

Here is a diagram which provides a nice overview of different crontab options:

10 examples of crontab commands in Linux



When to use crontab command in Linux

This means that When you want to carry out a task, I mean any task that is meant to be timed, use cron.

First of all before you can start using cron jobs, you must have gotten that installed

The first example we will be looking at is editing cron tab


1. To Edit

$ “crontab -e”

By using the command above, the user that is logged in at the moment is going  to be edited. But to edit the crontab of another user you have to add another option to the initial command which is -u. so you are going to have this command.


If you see the image output, I selected 1 as my file editor then I'll be directed there to edit


2. To View

$ “crontab -l”

To view the crontab of the current user the command is used with the option -l. You can as well view the crontab of another user, you will only need to specify the user like we did the first time.

$ crontab -u lilly -l.

Another thing you can do with or in cron is controlling access for instance let’s say you as a user access need access, then you can simply do the following


If I had crontab I would have seen something like "0 20 * * * backup.sh"

3. To Allow or grant access

cron.allow

The same thing with denying access too, here is it below:


4. To Deny Access

 ”cron.deny”


5. To remove Crontab entry

$  crontab -r

To remove a cron tab entry the following code is used, the option r removes any scheduled job that was with no confirmation from the cron tab.


6. To prompt a confirmation before removing an entry

You can use the following command to configure an entry on crontab

$ crontab -i -r

The only difference between the command in number 6 and command in number 7 is -i option. The option is not present in the first one but it is in the second one. If you use the one with option -I what happens is that it prompts the user a confirmation message to be sure if the user really want to remove the entry.


7. To delete another user’s crontab

$ crontab -u lilly -r

If you want to delete another user crontab, then you need to specify that User with -u option. In this example, The command showed that we are deleting the crontab of user lilly using.


8. To  run cron job everyday at a specific time

So for instance let us say the time of the day we want our job to run is 7 am and 10 am, This is the following code that does that:

Crontab -e

0 7,10 * * * /root/sample.sh

 The  arguments seen in terms of numbers and the asterisks has their meaning. I will discuss that much more later


9. To run cron job monthly

@monthly /root/sample.sh

 The @monthly annotation makes this task run every month. You will need this if you have a task that should run every month


10. To run Cron Job yearly

 @yearly /root/sample.sh

Same thing as yearly too this is used when you have a task that needs to run on a yearly basis. The @yearly annotation helps you with that.


Those are the examples on how to use crontab command in Linux. Basically, cron command has to do with timing or scheduling a time for a task to be executed. To give explanation on the arguments when trying to run a job for specific time as we had it in the command in number 8. The first argument is for the minute. While the second is for the hour. For the hour we had 7 and 10 simply because we want it to run at two different hours, 7am and 10am

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.