Cron Jobs
According to google cron stands for “a command to an operating system or server for a job that is to be executed at a specified time.”
Cron is a daemon that runs on Linux and Unix environments that execute scheduled commands also known as cron jobs created by the crontab command. In Windows, it’s called Scheduled Tasks. For example, a cron job may be created to process logs each day in the early mornings.
For commands that need to be executed repeatedly (e.g., hourly, daily, or weekly), you can use the crontab
command. The crontab
command creates a crontab file containing commands and instructions for the cron daemon to execute. You can use the crontab
command with the following options:
crontab -a filename
:-Install filename
as your crontab file. On many systems, this command is executed simply as crontab filename
(i.e., without the -a
option).
crontab -e
:-Edit your crontab file, or create one if it doesn’t already exist.
crontab -l
:-Display your crontab file.
crontab -r
:-Remove your crontab file.
crontab -v
:-Display the last time you edited your crontab file. (This option is available on only a few systems.)
crontab -u user
:-Used in conjunction with other options, this option allows you to modify or view the crontab file of user
. When available, only administrators can use this option.
Syntax of crontab
Format of crontab
* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
For Example some crontab commands are
First, install your cronjob by running the following command:# crontab -e
Append the following entry:0 3 * * * /root/backup.sh
Save and close the file. Given job backup file every 3hrs .
Simillarly
To run command five minutes after midnight, every day, enter:5 0 * * * /root/backup.sh
Run /path/to/script.sh at 2:15pm on the first of every month, enter:15 14 1 * * /path/to/script.sh
Run /path/to/script.sh at 10 pm on weekdays, enter:0 22 * * 1-5 /path/to/script.sh
Run /path/to/script.sh at 23 minutes after midnight, 2am, 4am …, everyday, enter:23 0-23/2 * * * /path/to/script.sh
Run /path/to/script.sh at 5 after 4 every Sunday, enter:5 4 * * sun /path/to/script.sh
Use of Cronjob
- Schedule cron jobs to regularly deactivate or delete accounts that are past their expiration dates.
- Send out daily newsletter e-mails.
- Expire and erase cached data files in a certain interval.
- Generate sitemaps
- Fetching your most recent Tweets, to be cached in a text file.
Implementing Cron in Ruby On Rails
Using Cron, a developer can automate such tasks as mailing that might be better sent during an off-hour, automatically updating stats, or the regeneration of static pages from dynamic sources. Cron has something for everyone.
RoR uses whenever gem
Install gem using:gem 'whenever', require:false
Now, bundle up:$ bundle
Go to project folder and run wheneverize:$ wheneverize
This will create a schedule.rb file in the config directory of your Rails application. Edit the schedule.rb file to schedule the task. Say we need to send the digest email at 10AM every day:
# file: config/schedule.rb
# send_email is a function to send newsletters
every :day, at: '10am' do
# specify the task name as a string
rake 'send_email'
end
Custom Job Types
The whenever gem offers three other job types, outside of Rake tasks: command, runner, and rake.
The runner job type allows you to input an executable piece of code as a string to be run at a particular interval. Similarly, command accepts a script to be run at an interval whereas. As you already know, rake runs a Rake task defined in your application. For example:-
every 3.hours do
runner 'User.expire_session_cache'
rake 'rake_task_name'
command '/usr/bin/command_name'
end
Similarly, this can be followed in different programming languages.