1 |
#!/bin/bash |
2 |
#------------------------------------------------------------------------------------------------- |
3 |
#$Header$ |
4 |
#------------------------------------------------------------------------------------------------- |
5 |
#This file is part of "Server Scripts, Automatic Maintenance, Cron", a set of scripts for |
6 |
#performing automatic periodic maintenance on a Linux server. |
7 |
#------------------------------------------------------------------------------------------------- |
8 |
#This source code and any program in which it is compiled/used is provided under the MIT License, |
9 |
#reproduced below. |
10 |
#------------------------------------------------------------------------------------------------- |
11 |
#Permission is hereby granted, free of charge, to any person obtaining a copy of |
12 |
#this software and associated documentation files(the "Software"), to deal in the |
13 |
#Software without restriction, including without limitation the rights to use, |
14 |
#copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the |
15 |
#Software, and to permit persons to whom the Software is furnished to do so, |
16 |
#subject to the following conditions : |
17 |
# |
18 |
#The above copyright notice and this permission notice shall be included in all |
19 |
#copies or substantial portions of the Software. |
20 |
# |
21 |
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
22 |
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
23 |
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE |
24 |
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
25 |
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
26 |
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
27 |
#SOFTWARE. |
28 |
#------------------------------------------------------------------------------------------------- |
29 |
#Master script for synthetic (custom) automatic maintenance of server. |
30 |
#Runs every hour. Paradigm is that cannot run if already running, |
31 |
#so must block concurrent executions. |
32 |
# |
33 |
#If the file with information about the battery status doesn't exist, cannot continue. |
34 |
if [ -f /sys/class/power_supply/BAT1/capacity ] ; then |
35 |
# |
36 |
#If the file contains unfavorable information, cannot continue. |
37 |
if [ `cat /sys/class/power_supply/BAT1/capacity` -gt 60 ] ; then |
38 |
# |
39 |
if [ ! -f /etc/cron.custom/lockfiles/auto_maint_cron_lock ] ; then |
40 |
#If we're here, the lockfile does not exist, and we are clear |
41 |
#to do the maintenance cron. |
42 |
# |
43 |
#Create the lockfile. This way, if we're invoked again |
44 |
#while still running, will bail. |
45 |
touch /etc/cron.custom/lockfiles/auto_maint_cron_lock |
46 |
# |
47 |
#Change to a safe working directory in case something goes wrong, |
48 |
#especially a failed cd followed by a mass delete. |
49 |
cd /root/cronjob_sandbox |
50 |
# |
51 |
#Gather required date information for exec'ing children. To avoid |
52 |
#complexity, can use the lock file for the timestamp. |
53 |
DATE_YEAR4=`date --reference=/etc/cron.custom/lockfiles/auto_maint_cron_lock +%Y` |
54 |
DATE_YEAR2=`date --reference=/etc/cron.custom/lockfiles/auto_maint_cron_lock +%y` |
55 |
DATE_MONTH_LONG=`date --reference=/etc/cron.custom/lockfiles/auto_maint_cron_lock +%B` |
56 |
DATE_MONTH_SHORT=`date --reference=/etc/cron.custom/lockfiles/auto_maint_cron_lock +%b` |
57 |
DATE_MONTH2=`date --reference=/etc/cron.custom/lockfiles/auto_maint_cron_lock +%m` |
58 |
DATE_DOM2=`date --reference=/etc/cron.custom/lockfiles/auto_maint_cron_lock +%d` |
59 |
DATE_DOW_LONG=`date --reference=/etc/cron.custom/lockfiles/auto_maint_cron_lock +%A` |
60 |
DATE_DOW_SHORT=`date --reference=/etc/cron.custom/lockfiles/auto_maint_cron_lock +%a` |
61 |
DATE_DOW_DIGIT=`date --reference=/etc/cron.custom/lockfiles/auto_maint_cron_lock +%w` |
62 |
DATE_HOUR_24=`date --reference=/etc/cron.custom/lockfiles/auto_maint_cron_lock +%H` |
63 |
DATE_HOUR_12=`date --reference=/etc/cron.custom/lockfiles/auto_maint_cron_lock +%I` |
64 |
DATE_AM_PM=`date --reference=/etc/cron.custom/lockfiles/auto_maint_cron_lock +%p` |
65 |
DATE_MIN2=`date --reference=/etc/cron.custom/lockfiles/auto_maint_cron_lock +%M` |
66 |
DATE_SEC2=`date --reference=/etc/cron.custom/lockfiles/auto_maint_cron_lock +%S` |
67 |
# |
68 |
#Run child script, sending both standard output and standard error to the temporary |
69 |
#log file. |
70 |
# |
71 |
#The parameters passed to the child are, in order: |
72 |
# 1)Year (4-digit) |
73 |
# 2)Year (2-digit) |
74 |
# 3)Month (long text) |
75 |
# 4)Month (short text) |
76 |
# 5)Month (2-digit) |
77 |
# 6)Day of month (2-digit) |
78 |
# 7)Day of week (long text) |
79 |
# 8)Day of week (short text) |
80 |
# 9)Day of week (single digit) |
81 |
# 10)Hour (24-hour, 2-digit) |
82 |
# 11)Hour (12-hour, 2-digit) |
83 |
# 12)AM or PM, as a text string |
84 |
# 13)Minutes (2-digit) |
85 |
# 14)Seconds (2-digit) |
86 |
# |
87 |
nice /etc/cron.custom/scripts/master_logged ${DATE_YEAR4} ${DATE_YEAR2} ${DATE_MONTH_LONG} ${DATE_MONTH_SHORT} ${DATE_MONTH2} ${DATE_DOM2} ${DATE_DOW_LONG} ${DATE_DOW_SHORT} ${DATE_DOW_DIGIT} ${DATE_HOUR_24} ${DATE_HOUR_12} ${DATE_AM_PM} ${DATE_MIN2} ${DATE_SEC2} &>/hl/logs/maint_auto_root/log_in_progress.txt |
88 |
# |
89 |
#Change to a safe working directory in case something goes wrong, |
90 |
#especially a failed cd followed by a mass delete. |
91 |
cd /root/cronjob_sandbox |
92 |
# |
93 |
#Do an atomic move to replace the file that Apache can serve. |
94 |
#The presumed atomic move below should prevent any race conditions |
95 |
#with Apache. My understanding of Unix is that if the log file is open and another file |
96 |
#is moved on top of it, it will stay available until it is closed. The |
97 |
#atomic move should be SSD-friendly. |
98 |
mv -f /hl/logs/maint_auto_root/log_in_progress.txt /hl/logs/maint_auto_root/${DATE_YEAR4}${DATE_MONTH2}${DATE_DOM2}_${DATE_HOUR_24}${DATE_MIN2}${DATE_SEC2}.txt |
99 |
# |
100 |
#We want to be really careful with this next section because of |
101 |
#the recursive rm. Want to be sure the directory exists. |
102 |
# |
103 |
#Maintain about 90 days worth of logs. 24*90=2160. |
104 |
if [ -d /hl/logs/maint_auto_root ] ; then |
105 |
cd /hl/logs/maint_auto_root |
106 |
rm -f `ls -1t | sed -e '2161,$!d'` |
107 |
cd /root/cronjob_sandbox |
108 |
fi |
109 |
# |
110 |
#Change to a safe working directory in case something goes wrong, |
111 |
#especially a failed cd followed by a mass delete. |
112 |
cd /root/cronjob_sandbox |
113 |
# |
114 |
#Remove the lock file. |
115 |
rm /etc/cron.custom/lockfiles/auto_maint_cron_lock |
116 |
fi |
117 |
fi |
118 |
fi |
119 |
#End of script. |