2 ways to Creating cron jobs in Linux.

Introduction

In the tutorial, we are creating cron jobs in Linux. Cron is to run the script or command at a given date and time.

It is to schedule the backup, for logrotate, and delete files on a weekly basis. Suppose we have to take the backup or archive the logs daily then we can use cron jobs.

Cron jobs in Linux help us to automate the tasks without a fail. At the given timestamp it will run like a scheduler and do your mentioned job.

So there is crond daemon which enables cron to run in the background. You can schedule your script as well.

There are 2 ways to create cron jobs in Linux.

  1. Creating cron jobs with a specific user.
  2. Creating cron jobs for system.

Before discussing the ways to create cron, let’s have a look into the crontab fields.


Explain cron jobs fields in Linux

fields for the crontab that we will use for cron jobs.

So on the above screenshot, we can see the fields like “* * * * *”. What this *(star) represents.

FieldValuesExamplesDescription
minute0 – 59*/5 * * * *Run job every 5th minute.
hour0 – 230 16 * * *Run job at 4:00 PM.
day of month1 – 310 16 25 * * *Run job at 25th day at 4:00 PM.
month1 – 120 0 * 5 *Run job at 25th May at 12:00 AM.
day of week0 – 6/1-70 3 * * 1Run job at 3 AM at Monday.

So there are 5 important fields that help us to schedule the cron job in Linux. let’s discuss this in detail.

  • minute: This field will execute the jobs at a particular minute. e.g: Run the jobs every 5 min.
  • hour: This field will execute the jobs on an hourly basis. e.g: Run the job every 6 hours.
  • day of month: Suppose you have to execute your job on a particular day of the month. e.g: Have to run the script on the 10th day of every month.
  • month: This field will execute the jobs on a monthly basis. e.g: You have to execute the job only in January.
  • day of week: This field will execute the job on a weekly basis. e.g: You have to run the script only from Monday to Friday.

So let’s have a look into how we are creating cron jobs in Linux.


Creating cron jobs with a specific user.

So create a bash script which will list the files and folders in the tastethelinux directory. And will schedule this script to run every 5 minutes.

$ cat >> check_the_files.sh
#!/bin/bash
ls /home/tastethelinux/tastethelinux > /home/tastethelinux/tastethelinux/files_present.log

Once you have done with your script then press “cntrl+d” it will back to your terminal. Make the script in executable format by using below command.

$ chmod +x check_the_files.sh

Now check the user you logged in with by using the “whoami” command.

$ whoami

OUTPUT:
tastethelinux

So edit the crontab file by executing the command “crontab -e” in a terminal.

$ crontab -e

OUTPUT:
no crontab for tastethelinux - using an empty one

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.basic
  3. /usr/bin/vim.tiny
  4. /bin/ed

Choose 1-4 [1]: 2

So here we can see the option to select the editor, you can select the editor of your choice. I am going with vim.basic editor.

*/5 * * * * /home/tastethelinux/check_the_files.sh

Once you have scheduled the script save and quit the file. let’s verify the scheduled cron jobs by using the “crontab -l”.

$ crontab -l

OUTPUT:
# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command

*/5 * * * * /home/tastethelinux/check_the_files.sh

So you can see the scheduled job in the crontab file. It will run every 5 minutes and execute the script. If you want to know more about crontab command use “man crontab” into terminal. Or refer to crontab link.


Creating Cron jobs for system in Linux.

To create cron jobs for the system we required root privileges. We have to edit the /etc/crontab file to schedule the script.

So create a bash script which will list the files and folders in the tastethelinux directory. And will schedule this script to run every 5 minutes.

$ cat >> check_the_files.sh
#!/bin/bash
ls /home/tastethelinux/tastethelinux > /home/tastethelinux/tastethelinux/files_present.log

Once you have done with your script then press “cntrl+d” it will back to your terminal. Make the script in executable format by using below command.

$ chmod +x check_the_files.sh

So let’s set up the job in the /etc/crontab file. You can use nano, vim, or vi editors to edit the crontab file.

$ sudo vim /etc/crontab
*/5 * * * *     root /home/tastethelinux/check_the_files.sh

So we have scheduled the job for every 5 minutes with the username root and path of the script. now save and quit the file.

Check at every five-minute interval 5, 10, 15, and so on in the “files_present.log” file, you will get the output.

If anything goes wrong you can check the log files which are at /var/log/syslog. The logs file can be in a different path it depends upon the Linux distro you are using.


Conclusion

So we have learned about the 5 fields for the crontab. We have written a bash script and then schedule the jobs in the crontab file and in cron.

Cron jobs help us to do automation for the manual tasks and the task that we have to perform very late at night. Find any issues or have to give us your valuable feedback then leave a comment.


Give your valuable time