Differences
This shows you the differences between two versions of the page.
— |
linux_wiki:crontab_entry_status [2019/05/25 23:50] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Crontab Entry Status ====== | ||
+ | |||
+ | **General Information** | ||
+ | |||
+ | This script checks the status of and can disable/ | ||
+ | |||
+ | **Checklist** | ||
+ | * User with crontab entries | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ====== Script Usage ====== | ||
+ | |||
+ | cron-jobs.sh -h | ||
+ | <code bash> | ||
+ | ==== Cron Jobs Control Usage ==== | ||
+ | |||
+ | Description: | ||
+ | |||
+ | --Usage-- | ||
+ | cron-jobs.sh status|enable|disable [OPTIONS] | ||
+ | |||
+ | OPTIONS | ||
+ | -h => Display usage. | ||
+ | -j => Job 1 (my_script1.py). | ||
+ | -J => Job 2 (my_script2.py). | ||
+ | |||
+ | --Other Requirements-- | ||
+ | -> Run as application user. | ||
+ | </ | ||
+ | |||
+ | ===== Customizing ===== | ||
+ | |||
+ | Edit the top customization section of the script in order to customize for crontab entries and app username. | ||
+ | |||
+ | <code bash> | ||
+ | ##### Customize These Variables ##### | ||
+ | ## Cron Jobs - Keyword from cron entry (such as script executed) ## | ||
+ | job1_name=' | ||
+ | job2_name=' | ||
+ | |||
+ | ## Application User ## | ||
+ | app_username=' | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ====== The Script ====== | ||
+ | |||
+ | <code bash cron-jobs.sh> | ||
+ | #!/bin/bash | ||
+ | # Name: cron-jobs.sh | ||
+ | # Description: | ||
+ | # Last Modified: 2017-09-08 | ||
+ | # Recent Changes: | ||
+ | |||
+ | ##### Customize These Variables ##### | ||
+ | ## Cron Jobs - Keyword from cron entry (such as script executed) ## | ||
+ | job1_name=' | ||
+ | job2_name=' | ||
+ | |||
+ | ## Application User ## | ||
+ | app_username=' | ||
+ | |||
+ | ## Define colors ## | ||
+ | # End/reset color | ||
+ | color_end=' | ||
+ | |||
+ | # Colors | ||
+ | color_green=' | ||
+ | color_red=' | ||
+ | color_yellow=' | ||
+ | ##### End of Customize Variables ##### | ||
+ | |||
+ | # | ||
+ | # Functions; Main starts after | ||
+ | # | ||
+ | function show_usage | ||
+ | { | ||
+ | echo -e " | ||
+ | echo -e " | ||
+ | echo -e " | ||
+ | echo -e " | ||
+ | echo -e " | ||
+ | echo -e " | ||
+ | echo -e " | ||
+ | echo -e " | ||
+ | echo -e " | ||
+ | echo -e "-> Run as application user." | ||
+ | echo -e | ||
+ | } | ||
+ | |||
+ | # | ||
+ | # Get Script Arguments | ||
+ | # | ||
+ | # Get control command (1st argument to script) and shift arguments for cmd line options | ||
+ | if [[ ${1} == " | ||
+ | control_command=${1} | ||
+ | shift | ||
+ | else | ||
+ | control_command=" | ||
+ | fi | ||
+ | |||
+ | ## Default settings ## | ||
+ | # By default, do not force run script. Prompt for running or not | ||
+ | force_run_script=" | ||
+ | |||
+ | # Select No Job Types By Default | ||
+ | job1_check=" | ||
+ | job2_check=" | ||
+ | |||
+ | ## Get command line arguments ## | ||
+ | while getopts " | ||
+ | case " | ||
+ | h) # -h (help) argument | ||
+ | show_usage | ||
+ | exit 0 | ||
+ | ;; | ||
+ | j) # -j (job 1 selected) | ||
+ | job1_check=" | ||
+ | ;; | ||
+ | J) # -J (job 2 selected) | ||
+ | job2_check=" | ||
+ | ;; | ||
+ | *) # invalid argument | ||
+ | show_usage | ||
+ | exit 0 | ||
+ | ;; | ||
+ | esac | ||
+ | done | ||
+ | |||
+ | ## Argument Sanity Checks ## | ||
+ | |||
+ | # Control command check -> must be status, enable, or disable | ||
+ | if [[ ${control_command} != " | ||
+ | echo -e ">> | ||
+ | show_usage | ||
+ | exit 1 | ||
+ | fi | ||
+ | |||
+ | # At least 1 job type must be controlled | ||
+ | if [[ ${job1_check} == " | ||
+ | echo -e ">> | ||
+ | show_usage | ||
+ | exit 1 | ||
+ | fi | ||
+ | |||
+ | # | ||
+ | # Pre-checks: Script Pre-reqs | ||
+ | # | ||
+ | |||
+ | # Check for external programs/ | ||
+ | # Ensure we are running as an application user | ||
+ | user_name=$(id --user --name) | ||
+ | |||
+ | if [[ ${user_name} != ${app_username} ]]; then | ||
+ | echo -e ">> | ||
+ | exit 1 | ||
+ | fi | ||
+ | |||
+ | # | ||
+ | # Main starts here | ||
+ | # | ||
+ | |||
+ | case ${control_command} in | ||
+ | " | ||
+ | if [[ ${job1_check} == " | ||
+ | # Job 1 status | ||
+ | |||
+ | # Check for Job 1 | ||
+ | crontab -l | grep -E " | ||
+ | job1_is_enabled=$? | ||
+ | crontab -l | grep -E " | ||
+ | job1_is_disabled=$? | ||
+ | |||
+ | # Determine status | ||
+ | if [[ ${job1_is_enabled} -eq 0 && ${job1_is_disabled} -eq 1 ]]; then | ||
+ | echo -e " | ||
+ | elif [[ ${job1_is_enabled} -eq 1 && ${job1_is_disabled} -eq 0 ]]; then | ||
+ | echo -e " | ||
+ | else | ||
+ | echo -e " | ||
+ | fi | ||
+ | |||
+ | fi | ||
+ | |||
+ | if [[ ${job2_check} == " | ||
+ | # Job 2 status | ||
+ | |||
+ | # Check for order disposition job | ||
+ | crontab -l | grep -E " | ||
+ | job2_is_enabled=$? | ||
+ | crontab -l | grep -E " | ||
+ | job2_is_disabled=$? | ||
+ | |||
+ | if [[ ${job2_is_enabled} -eq 0 && ${job2_is_disabled} -eq 1 ]]; then | ||
+ | echo -e " | ||
+ | elif [[ ${job2_is_enabled} -eq 1 && ${job2_is_disabled} -eq 0 ]]; then | ||
+ | echo -e " | ||
+ | else | ||
+ | echo -e " | ||
+ | fi | ||
+ | |||
+ | fi | ||
+ | ;; | ||
+ | |||
+ | " | ||
+ | if [[ ${job1_check} == " | ||
+ | # Enable Job 1 | ||
+ | echo -e "-> Enabling job 1(${job1_name})..." | ||
+ | crontab -l | sed "/ | ||
+ | fi | ||
+ | |||
+ | if [[ ${job2_check} == " | ||
+ | # Enable Job 2 | ||
+ | echo -e "-> Enabling job 2(${job2_name})..." | ||
+ | crontab -l | sed "/ | ||
+ | fi | ||
+ | ;; | ||
+ | |||
+ | " | ||
+ | if [[ ${job1_check} == " | ||
+ | # Disable Job 1 | ||
+ | echo -e "-> Disabling job 1(${job1_name})..." | ||
+ | crontab -l | sed "/ | ||
+ | fi | ||
+ | |||
+ | if [[ ${job2_check} == " | ||
+ | # Disable Job 2 | ||
+ | echo -e "-> Disabling job 2(${job2_name})..." | ||
+ | crontab -l | sed "/ | ||
+ | fi | ||
+ | ;; | ||
+ | |||
+ | *) | ||
+ | echo -e "-> ${color_red}ERROR!${color_end} Unknown control command. Exiting..." | ||
+ | show_usage | ||
+ | exit 1 | ||
+ | ;; | ||
+ | esac | ||
+ | </ | ||
+ | |||
+ | ---- | ||