How to schedule a cron job for seconds?

Share

What is a cron job?

cron is a command line utility also known as cron job, is used for scheduling a job   to be done repeatedly/periodically at fixed time intervals, tasks like checking a service status, sending an email every day, running scripts periodically and many more can be done using cron.

How to schedule a cron job?

To check if cron is installed or not run the below command in your terminal

crontab -l

Let’s learn about crontab in next steps.
if this command fails, most likely cron is not installed use the command given below to install cron if you are using ubantu/debian or use yum for linux systems.

sudo apt-get install cron

after the installation run “crontab -e” again, if it prompts to select the editor use which ever you are comfortable with and if a file opens similar to below picture cron is successfully installed

This is a crontab file, close it we will learn how to use this to schedule a cronjob.

What is a crontab?

Crontab file contains instructions for the scheduled cron job, these instructions are given by the user who scheduled it, each user of the system can define their own crontab and commands given under crontab are executed under the same user who owns it.

Crontab commands
These are some basic commands to use a crontab.

use this command to open crontab file and schedule your cronjob, this command will open the crontab file if one exists or will create a new one.

crontab -e

command to see the scheduled cron jobs.

crontab -l

This command will display the crontab file with all the scheduled cronjobs, if one exists for the user or it will show no crontab for the user.

command to remove users crontab file

crontab -r

This command will remove the exiting crontab file for the user.

command to reload crontab

service cron reload

use this command to reload the crontab, usually cron will check all the changes and will reload them whenever there is a modification but use this command if you want to make sure of it.

 

Crontab Sysntax

*  *  * * * Command to execute

cron syntax is made of five “*” fields which represents time and day to execute the job and followed by the command or file to be executed.

  ┌───────────── minute (0 - 59)
  │  ┌───────────── hour (0 - 23)
  │  │  ┌───────────── day of the month (1 - 31)
  │  │  │ ┌───────────── month (1 - 12)
  │  │  │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
  │  │  │ │ │                                   7 is also Sunday on some systems)
  │  │  │ │ │
  │  │  │ │ │
  *  *  * * *  <command to execute>

 

Let’s see some examples using above syntax

use the command “crontab -e” and write your syntax there save and close it.

Example-1:

Scheduling cronjob on nth minute of every hour

below syntax executes file hello.sh at 5th minute of every hour and every day of the moth

5  *  * * * /home/akubantu/hello.sh

 

Example-2:

Scheduling cronjob to be executed on nth hour of every day

*  5  * * * /home/akubantu/hello.sh

above cron job syntax executes bash file “hello.sh” at 5th hour of every day

 

Example-3:
syntax to schedule a cron job on nth day of every month

*  *  1 * * /home/akubantu/hello.sh

This syntax executes hello.sh on every 1st day of every month

 

Example-4:

To schedule a cronjob on particular day of particular month

*  *  1 5 * /home/akubantu/hello.sh

here “hello.sh” will be executed on first day of fifth month.

 

Example-5:
To schedule a cronjob at a particular time and specific day of the week

15  5  * * 6 /home/akubantu/hello.sh

above syntax will execute the job at 5:15 on every Saturday (as days of week counted from 0-6)

 

Example-6:
Scheduling a cronjob to execute a bash file for every minute

*  *  * * * /home/akubantu/hello.sh

above syntax will execute hello.sh file for every minute, this will help in checking status of any critical service collecting cpu, memory usage for analysis.

How to schedule a cronjob for every X number of seconds?
By default cronjob cannot be scheduled to execute for seconds, least parameter of time that a cron job can be scheduled is minute, but you can manipulate it with “sleep” command to schedule it for seconds.

 

Example-7:
using sleep command to schedule a cronjob for every 30 seconds

*  *  * * * /home/akubantu/hello.sh
*  *  * * * sleep 30; /home/akubantu/hello.sh

here both the cronjobs executes hello.sh for every minute, both the first and second cronjobs are expected to be executed at the same time but as “sleep” command is used in the second one which was set for a 30 seconds delay, it will be paused for 30 seconds and then the command/script is executed and this will be done for every minute repeatedly

 

Example-8:
Scheduling cronjob for every 20 seconds

*  *  * * * /home/akubantu/hello.sh
*  *  * * * sleep 20; /home/akubantu/hello.sh
*  *  * * * sleep 40; /home/akubantu/hello.sh

All the cronjobs will execute hello.sh for every minute, but as explained in the above example second cronjob will be paused for 20 seconds and the third will be paused for 40 seconds and that’s how the cycle repeats and hello.sh will be executed for every 20 seconds.

Please Contact us with Checkmate Management Consulting to hire dedicated software developers , virtual CTO Services in India to manage cloud infrastructure operations, IT Staff Augmentation Services ,  and 24×7 cloud support services in India.

Leave a Reply

Your email address will not be published.

*