What is a Cron job?
Beginners guide to scheduling a Cron job
Scheduled jobs are an essential part of web-based applications. To process failed tasks, run clean-up activities, or something else. Scheduled jobs find an efficient use case in most enterprise-level applications.
Software tools were used to schedule jobs daily in a few enterprise projects I worked in. I have also developed server-based applications using SpringBoot and the SpringBoot scheduler to run daily tasks every three hours.
Recently, I came across the concept of Cron jobs. Curious, I read a few articles on it and wanted to schedule a sample job to test it out. It turns out I was the only one unaware of this scheduler.
This article is for people like me who haven’t tried scheduling a Cron job.
What is Cron and Cron Job?
Unix-like Operating systems provide a command line utility named Cron. We can use this utility to schedule tasks on a machine. Tasks or jobs scheduled in the Cron utility are called Cron jobs.
Cron is a daemon.
A daemon is a type of program that runs silently in the background. Instead of a user, events and conditions control this program. Examples of these actions are specific time, specific date, changes in a certain directory, etc.
How to Schedule a Cron job
Create a directory (I created it under my user folder, but you can ideally do this at the root)
2. Create a file to write a shell script
Next, provide modification access to it
chmod u+x ./test.sh
3. Add this content to the test.sh file (edit command — vi test.sh)
#!/bin/bashecho "Starts here"echo Hello >> test.txtecho "Done"
The above script will print “Starts here” and “Done” in the stdout.log file, whereas the “Hello” will be printed to the test.txt file.
4. To run the above script, we need to schedule it in crontab.
Definition from computerhope.com
The crontab command is used to view or edit the table of commands to be run by cron. Each user on your system can have a personal crontab.
To open an editor to make an entry to crontab, use this command,
5. Add the following entry to the file
* * * * * cd ~/.scripts && ./test.sh >/tmp/stdout.log 2>/tmp/stderr.log
Here the five asterisks denote the following data in that order,
1 — minute(0–59)
2 — hour (0–23)
3 — day of the month (1–31)
4 — month(1–12)
5 — day of the week(0–6; Sunday to Saturday)
Not providing any value denotes that the job would run every minute. Sixty seconds is the least possible value we can set on the scheduler.
To validate that the job is scheduled, use command — crontab -l
6. Validate the output in the test.txt file
test.txt file should have a similar output after a few minutes
7. Validate the stdout.log file (This file is located in the root folder)
8. Once tested, do not forget to delete the cron job. Use the command
and delete the content.
Delete the .scripts directory as well using
rm -r .scripts
We just learned to schedule a cron job on a Mac / Linux machine. These jobs run on the computer that it’s scheduled; it won’t be an ideal solution in a distributed system.
Each cloud provider has its own scheduler services to run scheduled jobs in cloud-based applications.