addet crontab
This commit is contained in:
parent
1b767b779d
commit
a3cf9d5c10
1 changed files with 115 additions and 0 deletions
|
@ -53,9 +53,124 @@ gitUserSetup() {
|
|||
chmod 600 "$HOME/.git-credentials"
|
||||
}
|
||||
|
||||
set_cronjob() {
|
||||
# Color setup using tput
|
||||
local RED=$(tput setaf 1)
|
||||
local GREEN=$(tput setaf 2)
|
||||
local YELLOW=$(tput setaf 3)
|
||||
local BLUE=$(tput setaf 4)
|
||||
local BOLD=$(tput bold)
|
||||
local RESET=$(tput sgr0)
|
||||
|
||||
# Validate user is not root
|
||||
if [[ $EUID -eq 0 ]]; then
|
||||
echo "${RED}Error: Do not run this as root! Use your regular user.${RESET}" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Initialize variables
|
||||
local SCHEDULE=""
|
||||
local SCRIPT_PATH=""
|
||||
local LOGGING=""
|
||||
local COMMENT=""
|
||||
local PRESET=""
|
||||
|
||||
echo "${BOLD}${BLUE}Cronjob Setup Wizard${RESET}"
|
||||
|
||||
# 1. Select preset or custom
|
||||
PS3="$(echo -e "${BOLD}Choose schedule type [1-4]: ${RESET}")"
|
||||
echo "${BOLD}Available presets:${RESET}"
|
||||
select PRESET in "Daily" "Weekly" "Monthly" "Custom"; do
|
||||
case $REPLY in
|
||||
1) SCHEDULE="0 0 * * *"; break ;;
|
||||
2) SCHEDULE="0 0 * * 0"; break ;;
|
||||
3) SCHEDULE="0 0 1 * *"; break ;;
|
||||
4) break ;;
|
||||
*) echo "${RED}Invalid selection!${RESET}" ;;
|
||||
esac
|
||||
done
|
||||
|
||||
# 2. Custom schedule setup
|
||||
if [[ "$PRESET" == "Custom" ]]; then
|
||||
echo "${YELLOW}Enter schedule components (use '*' for any)${RESET}"
|
||||
read -p "Minute (0-59): " MIN
|
||||
read -p "Hour (0-23): " HOUR
|
||||
read -p "Day of month (1-31): " DOM
|
||||
read -p "Month (1-12): " MONTH
|
||||
read -p "Day of week (0-6, 0=Sunday): " DOW
|
||||
SCHEDULE="$MIN $HOUR $DOM $MONTH $DOW"
|
||||
fi
|
||||
|
||||
# Validate schedule format
|
||||
if ! [[ "$SCHEDULE" =~ ^([0-9*/, -]+ ){4}[0-9*/, -]+$ ]]; then
|
||||
echo "${RED}Invalid cron schedule format!${RESET}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# 3. Script path selection
|
||||
while true; do
|
||||
read -e -p "$(echo -e "${BOLD}Path to script: ${RESET}")" SCRIPT_PATH
|
||||
if [[ ! -f "$SCRIPT_PATH" ]]; then
|
||||
echo "${RED}Script does not exist!${RESET}"
|
||||
continue
|
||||
fi
|
||||
if [[ ! -x "$SCRIPT_PATH" ]]; then
|
||||
read -p "$(echo -e "${YELLOW}Script is not executable. Make it executable? [y/N]: ${RESET}")" -n 1 -r
|
||||
echo
|
||||
[[ $REPLY =~ ^[Yy]$ ]] && chmod +x "$SCRIPT_PATH"
|
||||
fi
|
||||
break
|
||||
done
|
||||
|
||||
# 4. Logging options
|
||||
local LOG_DIR="$HOME/logs"
|
||||
mkdir -p "$LOG_DIR"
|
||||
PS3="$(echo -e "${BOLD}Select logging option [1-4]: ${RESET}")"
|
||||
echo "${BOLD}Logging options:${RESET}"
|
||||
select LOGGING in "Full logging" "Errors only" "No logging" "Custom path"; do
|
||||
case $REPLY in
|
||||
1) LOGGING=">> $LOG_DIR/${SCRIPT_PATH##*/}.log 2>&1"; break ;;
|
||||
2) LOGGING="2>> $LOG_DIR/${SCRIPT_PATH##*/}.error.log"; break ;;
|
||||
3) LOGGING="> /dev/null 2>&1"; break ;;
|
||||
4) read -e -p "Enter full log path: " LOGGING_PATH
|
||||
LOGGING=">> $LOGGING_PATH 2>&1"
|
||||
break ;;
|
||||
*) echo "${RED}Invalid selection!${RESET}" ;;
|
||||
esac
|
||||
done
|
||||
|
||||
# 5. Add comment
|
||||
read -p "$(echo -e "${BOLD}Optional comment: ${RESET}")" COMMENT
|
||||
[[ -n "$COMMENT" ]] && COMMENT="# $COMMENT"
|
||||
|
||||
# 6. Confirm and install
|
||||
echo "${YELLOW}${BOLD}New cronjob:${RESET}"
|
||||
echo -e "${BLUE}Schedule: ${SCHEDULE}\nCommand: ${SCRIPT_PATH} ${LOGGING}\n${COMMENT}${RESET}"
|
||||
|
||||
read -p "$(echo -e "${BOLD}Add this cronjob? [y/N]: ${RESET}")" -n 1 -r
|
||||
echo
|
||||
[[ $REPLY =~ ^[Yy]$ ]] || return 0
|
||||
|
||||
# Add to crontab
|
||||
(crontab -l 2>/dev/null;
|
||||
echo -e "\n${COMMENT}\n${SCHEDULE} ${SCRIPT_PATH} ${LOGGING}") | crontab -
|
||||
|
||||
# Verify
|
||||
if [[ $? -eq 0 ]]; then
|
||||
echo "${GREEN}${BOLD}Cronjob added successfully!${RESET}"
|
||||
echo "${YELLOW}Current cronjobs:${RESET}"
|
||||
crontab -l
|
||||
else
|
||||
echo "${RED}Failed to add cronjob!${RESET}"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
if ! command_exists git; then
|
||||
return 1
|
||||
echo_error "You don't have git installed!"
|
||||
fi
|
||||
|
||||
gitUserSetup
|
||||
set_cronjob
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue