- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Cron Job Testing and Debugging in Linux
Introduction
First, we need to understand what is Cron Job in Linux. Cron job is also a Linux command that is used to schedule any kind of job in future. Therefore, this can serve many purpose of user as if some specific commands have to be run after reboot of system or some specific time in a day.
Now there are many challenges in cron job testing and debugging. Let us understand these in details in this article.
Syntax
If we want set up a cron job, then we must understand the syntax of cron job.
“a b c d e” “command script path” output
Let us now understand each parts of syntax.
a b c d e − This indicates data/time.
Now let us see in details about all time elements.
[a] indicates Minute and possible values are from 0 to 59.
Example if a = 8
The cron job is executed when system clock minute is in 8 position.
[b] indicates Hour and possible values are from 0 – 23. Note that here time format is 24hours not 12 hours. That means 8pm is shown as 20.
Example if b = 8
The cron job is executed when system clock hour is in 8 position.
[c] indicates Day and possible values are from 0 to 31.
Example if c = 8
The cron job is executed every 8th day of the month.
[d] indicates Month where 0 means none and 12 means December.
Example if d = 8
The cron job is executed every 8th month which is Auguest.
[e] indicates Day of the Week where 0 means Sunday and 7 means Sunday.
Example if e = 1
The cron job is executed every 1st day which is Monday.
command script path − This indicates the path and script we want to run.
For example we can run “/root/diskspace.sh” or any direct command.
output − This is not mandatory. It notifies user about job completion.
Set up a Cron Job
This simplest way to create a corn job is using below command.
$ crontab –e
If we are running this command for the first time then we will get below output to choose the editor.
no crontab for rian - using an empty one Select an editor. To change later, run 'select-editor'. 1. /bin/ed 2. /bin/nano <---- easiest 3. /usr/bin/vim.basic 4. /usr/bin/vim.tiny Choose 1-4 [2]: 4
If we are comfortable with vim then let us choose “4” and press enter.
Now below file will be opened. Here at last we can add our commands. For example ,
“48 23 * * * df > /tmp/cron.log 2>&1”. This means,s run df command when time is 11pm 48min and output will be stored in /tmp/cron.log file.
….many lines… # 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 48 23 * * * df > /tmp/cron.log 2>&1
We can see above text already conatins the cron tab format or syntax
# m h dom mon dow command
If we enter any wrong syntax then we may get below message. We can re-try.
crontab: installing new crontab "/tmp/crontab.4837sP/crontab":23: bad day-of-month errors in crontab file, can't install. Do you want to retry the same edit? (y/n) y
If there is no error then we get below message.
crontab: installing new crontab
List Down all Cron Job
We can use below command to list down all cron jobs present for current user.
Command
$ crontab -l
Output
……Many lines…. # m h dom mon dow command 48 23 * * * df > /tmp/cron.log 2>&1
Now when our cron job was executed we can see the output at “/tmp/cron.log”.
Command
$ cat /tmp/cron.log
Output
Filesystem 1K-blocks Used Available Use% Mounted on udev 985120 0 985120 0% /dev tmpfs 202976 6396 196580 4% /run /dev/sda1 305480952 5688928 284251444 2% / tmpfs 1014868 220 1014648 1% /dev/shm tmpfs 5120 4 5116 1% /run/lock tmpfs 1014868 0 1014868 0% /sys/fs/cgroup tmpfs 202976 56 202920 1% /run/user/1000
Conclusion
From this article, we got idea on how to create a cron job and test it. Now depending on our requirements we can cron jobs to make our life easy with Linux systems.