-
Notifications
You must be signed in to change notification settings - Fork 0
/
RefreshWeatherReader.sh
executable file
·78 lines (72 loc) · 3.06 KB
/
RefreshWeatherReader.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
#!/bin/bash
#
#
# RefreshWeatherReader.sh -- Shell script to kill and restart WeatherReader
# program every day at 2325.
# Copyright (C) 2015 Thomas A. DeMay
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# *
# */
#
# This script creates a special file in the user's home directory that the
# program looks for. When found, the program quits gracefully.
#
# Script is run in the source directory.
#
declare -i waitCount=20 # Will cause us to give up clean quit after 200 seconds.
# Get pid of ReadEKM process -- look for "ReadEKM" surrounded by word breaks.
# Not just "ReadEKM" because that could be in some other process.
# wpid is NOT empty if the ReadEKM application is running, and the status is true.
if wpid=$(ps -AcU"tom" | grep "\<WeatherReader\>")
then
touch $HOME/.CloseWeatherReader # Create .CloseWeatherReader file if not exist.
while wpid=$(ps -AcU"tom" | grep "\<WeatherReader\>")
do ## Wait for ReadEKM program to see .CloseWeatherReader file and quit.
sleep 10
## But don't wait forever.
if [ $((--waitCount)) -lt 0 ]; then break; fi
done
fi
# if wpid is not empty, clean termination above did not work.
if [ -n "$wpid" ]; then
wpid=$(echo $wpid | cut -d" " -f1) # Extract the pid
kill $wpid # kill the process
rm $HOME/.CloseWeatherReader # remove the .CloseWeatherReader file
# so it won't kill the process
# immediately after starting.
fi
# sleep for 10 seconds
sleep 10
## if there is an "at" job scheduled for this program, remove it before starting another.
for j in $(at -l | cut -f1) # get a list of pids for my "at" jobs.
do
# The last lines of the script restart it; extract the file name from the script.
jobFile=$(at -c $j | tail -n4 | grep 'at -f.*.sh' | sed -E s/'at -f(.*sh ).*/\1/')
if [ -n "$jobFile" ] # ignore empty jobFiles
then
if [ $jobFile = ${0##*/} ] # if the file name from the "at" script matches us
then # remove it from the list
# echo "$jobFile is pid $j"
at -r $j
fi
fi
done
# restart WeatherReader program
#### MAKE SURE THERE IS A LINK TO THE WeatherReader EXECUTABLE
#### WHERE THIS SCRIPT EXPECTS IT TO BE.
$PWD/WeatherReader &
# Reschedule this script to run at 2325 tomorrow.
at -fRefreshWeatherReader.sh 2325 >/dev/null 2>$1