Skip to content

Commit ac80d47

Browse files
committed
incorporate cron job builder function
1 parent 0a97cc0 commit ac80d47

File tree

1 file changed

+88
-9
lines changed

1 file changed

+88
-9
lines changed

cron_job/cron_builder.sh

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,60 @@
11
#!/bin/bash
22

3+
4+
DEFAULT_PING_COUNT=4
5+
DEFAULT_TELNET_RETRY=4
6+
DEFAULT_LOG_FILENAME="logs.log"
7+
8+
# Function to generate the cron pattern
9+
generate_cron_pattern() {
10+
echo "Please specify the following values to generate the cron pattern:"
11+
echo "1. Minute (0-59)"
12+
read -p " (default: *): " minute
13+
minute=${minute:-*}
14+
echo "2. Hour (0-23)"
15+
read -p " (default: *): " hour
16+
hour=${hour:-*}
17+
echo "3. Day of the month (1-31)"
18+
read -p " (default: *): " day_of_month
19+
day_of_month=${day_of_month:-*}
20+
echo "4. Month (1-12)"
21+
read -p " (default: *): " month
22+
month=${month:-*}
23+
echo "5. Day of the week (0-6, Sunday is 0)"
24+
read -p " (default: *): " day_of_week
25+
day_of_week=${day_of_week:-*}
26+
cron_pattern="$minute $hour $day_of_month $month $day_of_week"
27+
echo "$cron_pattern"
28+
}
29+
30+
# function to increment log filename
31+
increment_log_filename() {
32+
log_filename=$1
33+
34+
if [[ $log_filename == $DEFAULT_LOG_FILENAME ]]; then
35+
new_log_filename="logs2.log"
36+
else
37+
log_number=$(echo "$log_filename" | sed 's/[^0-9]*//g')
38+
new_log_number=$((log_number+1))
39+
new_log_filename="logs$new_log_number.log"
40+
fi
41+
42+
echo "$new_log_filename"
43+
}
44+
45+
# function to generate cron job string
46+
generate_cron_job_string() {
47+
cron_pattern=$1
48+
ping_check_filename=$2
49+
ping_count=$3
50+
telnet_retry=$4
51+
log_filename=$5
52+
53+
cron_job_string="$cron_pattern /usr/bin/python /home/[email protected]/OPT/pingTester/Test/Temp/cron_job/python2port.py -f /home/[email protected]/OPT/pingTester/Test/Temp/cron_job/$ping_check_filename -pc $ping_count -tr $telnet_retry &>/home/[email protected]/OPT/pingTester/Test/Temp/cron_job/cron_output/$log_filename"
54+
55+
echo "$cron_job_string"
56+
}
57+
358
# Ask if the user wants to remove existing cron jobs
459
read -p "Do you want to remove existing cron jobs? (y/n): " remove_cron_jobs
560
if [[ "$remove_cron_jobs" == "y" ]]; then
@@ -10,17 +65,41 @@ fi
1065
# Loop to create new cron jobs
1166
while true; do
1267
# Get user input for cron pattern, filename, ping count, telnet retry, and log file name
13-
read -p "Enter cron pattern (e.g. '58 11 * * *'): " cron_pattern
14-
read -p "Enter filename (e.g. 'pingcheck1'): " filename
15-
read -p "Enter ping count (default 4): " ping_count
16-
ping_count=${ping_count:-4} # Set default value if user input is empty
17-
read -p "Enter telnet retry (default 4): " telnet_retry
18-
telnet_retry=${telnet_retry:-4} # Set default value if user input is empty
19-
read -p "Enter log file name (default 'logs.log'): " log_file
20-
log_file=${log_file:-logs.log} # Set default value if user input is empty
68+
# Generate the cron pattern
69+
read -p "Do you want to generate the cron pattern? (y/n): " generate_cron_pattern
70+
if [[ "$generate_cron_pattern" == "y" ]]; then
71+
echo "Please specify the following values to generate the cron pattern:"
72+
echo "1. Minute (0-59)"
73+
read -p " (default: *): " minute
74+
minute=${minute:-*}
75+
echo "2. Hour (0-23)"
76+
read -p " (default: *): " hour
77+
hour=${hour:-*}
78+
echo "3. Day of the month (1-31)"
79+
read -p " (default: *): " day_of_month
80+
day_of_month=${day_of_month:-*}
81+
echo "4. Month (1-12)"
82+
read -p " (default: *): " month
83+
month=${month:-*}
84+
echo "5. Day of the week (0-6, Sunday is 0)"
85+
read -p " (default: *): " day_of_week
86+
day_of_week=${day_of_week:-*}
87+
cron_pattern="$minute $hour $day_of_month $month $day_of_week"
88+
else
89+
read -p "Enter the cron pattern (e.g. '58 11 * * *'): " cron_pattern
90+
fi
91+
92+
read -p "Enter ping check filename: " ping_check_filename
93+
read -p "Enter ping count (default $DEFAULT_PING_COUNT): " ping_count_input
94+
ping_count=${ping_count_input:-$DEFAULT_PING_COUNT}
95+
read -p "Enter telnet retry (default $DEFAULT_TELNET_RETRY): " telnet_retry_input
96+
telnet_retry=${telnet_retry_input:-$DEFAULT_TELNET_RETRY}
97+
read -p "Enter log filename (default $DEFAULT_LOG_FILENAME): " log_filename
98+
log_filename=${log_filename:-$DEFAULT_LOG_FILENAME}
2199

22100
# Build cron job string
23-
cron_job_string="$cron_pattern /usr/bin/python /home/[email protected]/OPT/pingTester/Test/Temp/cron_job/python2port.py -f /home/[email protected]/OPT/pingTester/Test/Temp/cron_job/$filename -pc $ping_count -tr $telnet_retry &>/home/[email protected]/OPT/pingTester/Test/Temp/cron_job/cron_output/$log_file"
101+
cron_job_string=$(generate_cron_job_string "$cron_pattern" "$ping_check_filename" "$ping_count" "$telnet_retry" "$log_filename")
102+
echo "Cron job string: $cron_job_string"
24103

25104
# Confirm cron job string with the user
26105
echo "Your cron job is:"

0 commit comments

Comments
 (0)