DOCKER HEALTH CHECK USING SHELL SCRIPT AND CRON TAB FOR SCHEDULING THE JOB

Pravat kumar Nath sharma
4 min readMay 16, 2021

DOCKER:-Docker is a container management service. The keywords of Docker are develop, ship and run anywhere. The whole idea of Docker is for developers to easily develop applications, ship them into containers which can then be deployed anywhere.

DOCKER FILE:-Docker also gives you the capability to create your own Docker images, and it can be done with the help of Docker Files. A Docker File is a simple text file with instructions on how to build your images.

vim Dockerfile

in this file we created one docker image from base os.

docker build -t <name:-test:v1> .> this command will build a docker image

lets run some container:- docker run -dit — name os1 test:v1

this container will fetch everything from dockerfile container image which we build

SHELL SCRIPT:- · A shell script is a list of commands in a computer program that is run by the Unix shell which is a command line interpreter. A shell script usually has comments that describe the steps. The different operations performed by shell scripts are program execution, file manipulation and text printing.the script file save with .sh extension

in the above script we are gathering docker container status of health and status of container is it on up state or exited state that we checking through script. if the container goes down or exited or become unhealthy the script automatically restart the container and fix its health issue.

vim entrypoint.sh

which i used to create the folder that make healthy to the container

crontab :-

A crontab file contains instructions to the cron daemon of the general form: “run this command at this time on this date”. Each user can have their own crontab, and though these are files in /var directory, they are not intended to be edited directly.

step1:create a crontab

vim crontab -e

to check list of entries we use crontab -l command

in crontab file we have to specify the job scheduling time according to our requirement the job schedule format like

so here * means all and if you want to specify any time date hour you can specify according to above formart

EXAMPLES

To run /usr/bin/sample.sh at 12.59 every day and supress the output

59 12 * * * simon /usr/bin/sample.sh > /dev/null 2>&1

To run sample.sh everyday at 9pm (21:00)

0 21 * * * sample.sh 1>/dev/null 2>&1

To run sample.sh every Tuesday to Saturday at 1am (01:00)

0 1 * * 2-7 sample.sh 1>/dev/null 2>&1

To run sample.sh at 07:30, 09:30 13:30 and 15:30

30 07,09,13,15 * * * sample.sh

To run sample.sh at 07:30, 09:30 13:30 and 15:30

30 07,09,13,15 * * * sample.sh

To run sample.sh at 2am daily.

0 2 * * *  sample.sh ( This is widely used in cases of backup of files/databases etc on daily basis. )

To run sample.sh twice a day. say 5am and 5pm

0 5,17 * * * sample.sh

To run sample.sh every minutes.

* * * * * sample.sh

To run sample.sh every Sunday at 5 PM.

0 17 * * sun  sample.sh

To run sample.sh every 10 minutes.

*/10 * * * * sample.sh

To run sample.sh on selected months.

* * * jan,may,aug *  sample.sh

To run sample.sh selected days.

0 17 * * sun,fri  sample.sh

To run sample.sh first sunday of every month.

0 2 * * sun  [ $(date +%d) -le 07 ] && sample.sh

To run sample.sh every four hours.

0 */4 * * * sample.sh

To run sample.sh every Sunday and Monday.

0 4,17 * * sun,mon sample.sh

To run sample.sh every 30 Seconds.

* * * * * sample.sh
* * * * * sleep 30; sample.sh

To run multiple jobs using single cron.

* * * * * sample1.sh; sample2.sh

To run sample.sh on yearly ( @yearly ).

@yearly sample.sh

To run sample.sh on monthly ( @monthly ).

@monthly sample.sh

To run sample.sh on Weekly ( @weekly ).

@weekly sample.sh

To run sample.sh on daily ( @daily ).

@daily sample.sh

To run sample.sh on hourly ( @hourly ).

@hourly sample.sh

To run sample.sh on system reboot ( @reboot ).

@reboot sample.sh

--

--