************************************************************************************ * Description: Comment out and enable certain task on crontab by Linux shell script * Date: 11:49 PM EST, 03/12/2018 ************************************************************************************ <1> In some case, for app server, when Linux system up, some certain crontab jobs need to be commented out first. After app up, then uncomment the jobs: | |__ o. Above action can be archived by manual work, but following shell script can manipulate crontab automatically. | | |__ o. "Crontab -l" output is a text file, which can be written by ">" | | # Keep the jobs, which need to be active as it is via "grep -v option". | crontab -l | grep -v /u01/job_name_not_comment.sh > /u01/crontab_disable.txt | | # Comment out the jobs need to be disabled. | crontab -l | grep /u01/job_name_comment.sh | sed '/\/u01\/job_name.sh/s!^!#Disabled#!' >> /u01/crontab_disable.txt | | # Writing the final result back to crontab as text file | crontab < /u01/crontab_disable.txt | | |__ o. Now, "crontab -l" will show job got commented out with heads "#Disabled#": | | #Disabled#/u01/job_name_comment.sh | | | |__ o. Following command will remove "#Disabled#" to re-activate job on Crontab: crontab -l | sed '/\/u01\/job_name.sh/s!#Disabled#!!' > /u01/crontab_enable.txt crontab < /u01/crontab_enable.txt
Your Comments