Shell Script : Auto Enable/Disable Specific Cronjob

Shell Script : Auto Enable/Disable Specific Cronjob

What is Cronjob ?
A cron job is a scheduled task that is executed by the system at a specified time/date.

Sometimes we want to perform some task during specific time(Ex: During production Hours), and once the production time completes, we will be disabling cronjob. this requires manual work.The above manual work, can be automated with this script.

Create two scripts,
1.Auto_enable_cron.sh
2. Auto_disable_cron.sh

Below are both the scripts with cronjob details,

COMMANDS used:

crontab -l | sed ‘/^#.*GIVE-SCRIPT-NAME-HERE.sh/s/^#//’ | crontab –
crontab -l | sed ‘/^[^#].*GIVE-SCRIPT-NAME-HERE.sh/s/^/#/’ | crontab –

cronandscript

cronoutput

In the above image we can see that after running Auto_enable_cron.sh
The script in the cron GIVE-SCRIPT-NAME-HERE.sh is uncommented and after running
Auto-disable_cron.sh the script GIVE-SCRIPT-NAME-HERE.sh is commented again.

Now add scripts to desired time in cronjob.
9 * * * * /home/cronjob/Auto_enable_cron.sh (This will enable GIVE-SCRIPT-NAME-HERE.sh @9)
23 * * * * /home/cronjob/Auto_disable_cron.sh (This will disable GIVE-SCRIPT-NAME-HERE.sh @23)

Enjoy, just now you have automated your task!!!!.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>