-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcron-wrapper.sh
executable file
·129 lines (129 loc) · 4.27 KB
/
cron-wrapper.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash
#
# ----------------------------------------------------------------------------
# Script for wrapping scripts called by cron, but with collision avoidance
# ============================================================================
# Created: 2024-06-14, by [email protected]
# Last modified: 2024-06-14, by [email protected]
# ----------------------------------------------------------------------------
#
# This script will be used to "wrap" other scripts,
# to allow them to be safely invoked from the Linux
# "cron" scheduler. This wrapper will ensure that
# scripts for two different schedules do not attempt
# to run simultaneously, creating problems.
#
# One example might be the HOURLY or OFTEN scripts
# attempting to run, while the WEEKLY script (which
# may run for a long time) is still running.
#
# Figure-out where the scripts folder is located ...
#
SCRIPTS_FOLDER="$( dirname -- "$( readlink -f -- "$0"; )"; )";
#
# Include the ANSI escape code definitions ...
#
source $SCRIPTS_FOLDER/include/include-ansi.inc;
#
# Innclude the base script dependancies ...
#
source $SCRIPTS_FOLDER/include/include-base.inc;
#
# Determine script log file ...
#
SCRIPT_LOG_FILE="$LOGS_FOLDER/cron-wrapper.log";
#
# Display start of stuff ...
#
source $SCRIPTS_FOLDER/include/include-outputbegin.inc;
#
# Determine path to CRON scripts folder ...
#
CRON_SCRIPTS_FOLDER="$SCRIPTS_FOLDER/cron";
#
# Grab command-line parameter (which should be a cron script (without the path) ...
#
CRON_SCRIPT=$1;
#
# Validate that the requested script exists ...
#
if ! [ -e "$CRON_SCRIPTS_FOLDER/$CRON_SCRIPT" ]; then
#
# Display an error message ...
#
MESSAGE="${ANSI_REDLT}$(figlet "Error:")${ANSI_OFF}\n";
MESSAGE+="${ANSI_WHITE}Specified script (${ANSI_REDLT}$CRON_SCRIPTS_FOLDER/$CRON_SCRIPT${ANSI_WHITE}) does not exist!${ANSI_OFF}";
echo -e "$MESSAGE";
if [[ $SCRIPT_LOG_FILE ]]; then
echo -e "$MESSAGE" | ansi2txt >> "$SCRIPT_LOG_FILE";
fi;
#
# Display end of stuff ...
#
source $SCRIPTS_FOLDER/include/include-outputend.inc;
#
# Exit returning an error code.
#
exit 1;
fi;
#
# Determine the cron check file that will be used to avoid collisions ...
#
CRON_CHECK_FILE="$CRON_SCRIPTS_FOLDER/cron-in-progress.txt";
#
# Display and log some stuff ...
#
echo -e "Option: Value:";
echo -e "Option: Value:" >> "$SCRIPT_LOG_FILE";
echo -e "---------------------------- -----";
echo -e "---------------------------- -----" >> "$SCRIPT_LOG_FILE";
echo -e "Scripts folder: $SCRIPTS_FOLDER";
echo -e "Scripts folder: $SCRIPTS_FOLDER" >> "$SCRIPT_LOG_FILE";
echo -e "Cron Scripts folder: $CRON_SCRIPTS_FOLDER";
echo -e "Cron Scripts folder: $CRON_SCRIPTS_FOLDER" >> "$SCRIPT_LOG_FILE";
echo -e "Cron Script Requested: $CRON_SCRIPT";
echo -e "Cron Script Requested: $CRON_SCRIPT" >> "$SCRIPT_LOG_FILE";
echo -e "Cron Check-File: $CRON_CHECK_FILE";
echo -e "Cron Check-File: $CRON_CHECK_FILE" >> "$SCRIPT_LOG_FILE";
#
# Check if the check control file already exists ...
#
if [ -e "$CRON_CHECK_FILE" ]; then
#
# Display a notification that there
# is already an update in progress ...
#
MESSAGE="${ANSI_YELLOW}$(figlet "Warning:")${ANSI_OFF}\n";
MESSAGE+="${ANSI_WHITE}Another scheduled cron job is already in progress!${ANSI_OFF} ${ANSI_WHITE}Aborting this script.${ANSI_OFF}";
echo -e "$MESSAGE";
if [[ $SCRIPT_LOG_FILE ]]; then
echo -e "$MESSAGE" | ansi2txt >> "$SCRIPT_LOG_FILE";
fi;
else
#
# If the coron check file does not exist,
# then, its all-clear to proceed ...
#
echo -e "Created at: $(date)" > $CRON_CHECK_FILE;
echo -e "Created by: $CRON_SCRIPT" >> $CRON_CHECK_FILE;
#
# Go ahead and run the requested script ...
#
echo -e "Running this script as requested";
echo -e "Running this script as requested" >> "$SCRIPT_LOG_FILE";
echo -e "$CRON_SCRIPTS_FOLDER/$CRON_SCRIPT";
echo -e "$CRON_SCRIPTS_FOLDER/$CRON_SCRIPT" >> "$SCRIPT_LOG_FILE";
cd $CRON_SCRIPTS_FOLDER;
$CRON_SCRIPTS_FOLDER/$CRON_SCRIPT;
#
# Delete the cron check file, since we are done ...
#
rm -f $CRON_CHECK_FILE;
fi;
#
# Display end of stuff ...
#
source $SCRIPTS_FOLDER/include/include-outputend.inc;
#
# ... thats all folks!
#