linux_wiki:crontab_entry_status

Crontab Entry Status

General Information

This script checks the status of and can disable/enable crontab entries.

Checklist

  • User with crontab entries

Script Usage

cron-jobs.sh -h

==== Cron Jobs Control Usage ====
 
Description: Control application user cron jobs.
 
--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.

Edit the top customization section of the script in order to customize for crontab entries and app username.

##### Customize These Variables #####
## Cron Jobs - Keyword from cron entry (such as script executed) ##
job1_name='my_script1.py'
job2_name='my_script2.py'
 
## Application User ##
app_username='appuser'

The Script

cron-jobs.sh
#!/bin/bash
# Name: cron-jobs.sh
# Description: Cron Jobs Control
# Last Modified: 2017-09-08
# Recent Changes:-Initial Release
 
##### Customize These Variables #####
## Cron Jobs - Keyword from cron entry (such as script executed) ##
job1_name='my_script1.py'
job2_name='my_script2.py'
 
## Application User ##
app_username='appuser'
 
## Define colors ##
# End/reset color
color_end='\033[0m'
 
# Colors
color_green='\033[0;32m'
color_red='\033[0;31m'
color_yellow='\033[0;33m'
##### End of Customize Variables #####
 
#=====================================
# Functions; Main starts after
#=====================================
function show_usage
{
  echo -e "\n==== Cron Jobs Control Usage ===="
  echo -e "\nDescription: Control application user cron jobs."
  echo -e "\n--Usage--"
  echo -e "cron-jobs.sh status|enable|disable [OPTIONS]\n"
  echo -e "OPTIONS"
  echo -e "-h                    => Display usage."
  echo -e "-j                    => Job 1 (${job1_name})."
  echo -e "-J                    => Job 2 (${job2_name})."
  echo -e "\n--Other Requirements--"
  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} == "status" || ${1} == "enable" || ${1} == "disable" ]]; then
  control_command=${1}
  shift
else
  control_command="unknown"
fi
 
## Default settings ##
# By default, do not force run script. Prompt for running or not
force_run_script="no"
 
# Select No Job Types By Default
job1_check="no"
job2_check="no"
 
## Get command line arguments ##
while getopts "hjJ" opt; do
  case "${opt}" in
    h) # -h (help) argument
      show_usage
      exit 0
    ;;
    j) # -j (job 1 selected)
      job1_check="yes"
    ;;
    J) # -J (job 2 selected)
      job2_check="yes"
    ;;
    *) # invalid argument
      show_usage
      exit 0
    ;;
  esac
done
 
## Argument Sanity Checks ##
 
# Control command check -> must be status, enable, or disable
if [[ ${control_command} != "status" && ${control_command} != "enable" && ${control_command} != "disable" ]]; then
  echo -e ">> ${color_red}ERROR!${color_end} First argument to script must be 'status','enable',or 'disable'. Exiting..."
  show_usage
  exit 1
fi
 
# At least 1 job type must be controlled
if [[ ${job1_check} == "no" && ${job2_check} == "no" ]]; then
  echo -e ">> ${color_red}ERROR!${color_end} At least one job type must be controlled. Exiting..."
  show_usage
  exit 1
fi
 
#=============================
# Pre-checks: Script Pre-reqs
#=============================
 
# Check for external programs/configuration needed
# Ensure we are running as an application user
user_name=$(id --user --name)
 
if [[ ${user_name} != ${app_username} ]]; then
  echo -e ">> ${color_red}ERROR!${color_end} Script must be executed as the application user. (${app_username}) Exiting..."
  exit 1
fi
 
#===================
# Main starts here
#===================
 
case ${control_command} in
  "status")
    if [[ ${job1_check} == "yes" ]]; then
      # Job 1 status
 
      # Check for Job 1
      crontab -l | grep -E "^[^#].*${job1_name}" &> /dev/null
      job1_is_enabled=$?
      crontab -l | grep -E "^#.*${job1_name}" &> /dev/null
      job1_is_disabled=$?
 
      # Determine status
      if [[ ${job1_is_enabled} -eq 0 && ${job1_is_disabled} -eq 1 ]]; then
        echo -e "--> Cron Job 1 (${job1_name}): ${color_green}Enabled${color_end}"
      elif [[ ${job1_is_enabled} -eq 1 && ${job1_is_disabled} -eq 0 ]]; then
        echo -e "--> Cron Job 1 (${job1_name}): ${color_red}Disabled${color_end}"
      else
        echo -e "--> Cron Job 1 (${job1_name}): ${color_yellow}Unknown${color_end} Job Status!"
      fi
 
    fi
 
    if [[ ${job2_check} == "yes" ]]; then
      # Job 2 status
 
      # Check for order disposition job
      crontab -l | grep -E "^[^#].*${job2_name}" &> /dev/null
      job2_is_enabled=$?
      crontab -l | grep -E "^#.*${job2_name}" &> /dev/null
      job2_is_disabled=$?
 
      if [[ ${job2_is_enabled} -eq 0 && ${job2_is_disabled} -eq 1 ]]; then
        echo -e "--> Cron Job 2 (${job2_name}): ${color_green}Enabled${color_end}"
      elif [[ ${job2_is_enabled} -eq 1 && ${job2_is_disabled} -eq 0 ]]; then
        echo -e "--> Cron Job 2 (${job2_name}): ${color_red}Disabled${color_end}"
      else
        echo -e "--> Cron Job 2 (${job2_name}): ${color_yellow}Unknown${color_end} Job Status!"
      fi
 
    fi
  ;;
 
  "enable")
    if [[ ${job1_check} == "yes" ]]; then
      # Enable Job 1
      echo -e "-> Enabling job 1(${job1_name})..."
      crontab -l | sed "/^#.*${job1_name}/s/^#//" | crontab -
    fi
 
    if [[ ${job2_check} == "yes" ]]; then
      # Enable Job 2
      echo -e "-> Enabling job 2(${job2_name})..."
      crontab -l | sed "/^#.*${job2_name}/s/^#//" | crontab -
    fi
  ;;
 
  "disable")
    if [[ ${job1_check} == "yes" ]]; then
      # Disable Job 1
      echo -e "-> Disabling job 1(${job1_name})..."
      crontab -l | sed "/^[^#].*${job1_name}/s/^/#/" | crontab -
    fi
 
    if [[ ${job2_check} == "yes" ]]; then
      # Disable Job 2
      echo -e "-> Disabling job 2(${job2_name})..."
      crontab -l | sed "/^[^#].*${job2_name}/s/^/#/" | crontab -
    fi
  ;;
 
  *)
    echo -e "-> ${color_red}ERROR!${color_end} Unknown control command. Exiting..."
    show_usage
    exit 1
  ;;
esac

  • linux_wiki/crontab_entry_status.txt
  • Last modified: 2019/05/25 23:50
  • (external edit)