The manage.sh file#
The pvWebMonitor
may be run as either a command-line program or run in a
background (daemon) process. To ensure the program is always as intended, a
shell script is used to initialize and/or restart as needed. The shell script
may be called periodically by a system scheduler such as cron - The Linux system scheduler. An
example shows the shell script installed with a new project directory. The
contents of the file are described below.
The shell script is run with either of these commands:
command |
description |
---|---|
|
Short text showing these commands. |
|
Starts a process and writes the pid to the |
|
Stops the process with pid matching value in the |
|
Stops, then starts the process (in the background). |
|
Checks if the process is running. Starts it if it is not running. |
Example shell script
Example shell script to manage the pvWebMonitor
process either as a startup or a background daemon.
1#!/bin/bash
2# init file for pvWebMonitor
3#
4# chkconfig: - 98 98
5# description: pvWebMonitor WWW page update script for NAME_YOUR_SYSTEM_HERE
6#
7# processname: pvWebMonitor_MAKE_THIS_NAME_UNIQUE
8
9
10PROJECT_DIR=/tmp/pv
11# MANAGE=${PROJECT_DIR}/manage.sh
12LOGFILE=${PROJECT_DIR}/log-manage.txt
13PIDFILE=${PROJECT_DIR}/pid.txt
14CONFIGFILE=${PROJECT_DIR}/config.xml
15EXECUTABLE_SCRIPT=/home/oxygen/JEMIAN/Apps/anaconda/bin/pvWebMonitor
16RETVAL=0
17
18
19get_pid(){
20 cd ${PROJECT_DIR}
21 PID=$(/bin/cat ${PIDFILE})
22 return "$PID"
23}
24
25
26check_pid_running(){
27 get_pid
28 if [ "${PID}" == "" ]; then
29 # no PID in the PIDFILE
30 RETVAL=1
31 else
32 RESPONSE=$(ps -p "${PID}" -o comm=)
33 if [ "${RESPONSE}" == "pvWebMonitor" ]; then
34 # PID matches the pvWebMonitor profile
35 RETVAL=0
36 else
37 # PID is not pvWebMonitor
38 RETVAL=1
39 fi
40 fi
41 return $RETVAL
42}
43
44
45start(){
46 cd ${PROJECT_DIR}
47 ${EXECUTABLE_SCRIPT} ${CONFIGFILE} 2>&1 >> ${LOGFILE} &
48 PID=$!
49 /bin/echo ${PID} > ${PIDFILE}
50 /bin/echo "# [$0 $(/bin/date)] started ${PID}: ${EXECUTABLE_SCRIPT}" 2>&1 >> ${LOGFILE} &
51 /bin/echo "# [$0 $(/bin/date)] started ${PID}: ${EXECUTABLE_SCRIPT}"
52}
53
54
55stop(){
56 get_pid
57 check_pid_running
58
59 if [ $RETVAL == 1 ]; then
60 /bin/echo "# [$0 $(/bin/date)] not running ${PID}: ${EXECUTABLE_SCRIPT}" 2>&1 >> ${LOGFILE} &
61 else
62 kill ${PID}
63 /bin/echo "# [$0 $(/bin/date)] stopped ${PID}: ${EXECUTABLE_SCRIPT}" 2>&1 >> ${LOGFILE} &
64 /bin/echo "# [$0 $(/bin/date)] stopped ${PID}: ${EXECUTABLE_SCRIPT}"
65 fi
66 /bin/cp -f /dev/null ${PIDFILE}
67}
68
69
70restart(){
71 stop
72 start
73}
74
75
76checkup(){
77 #=====================
78 # call periodically (every 5 minutes) to see if pvWebMonitor is running
79 #=====================
80 # field allowed values
81 # ----- --------------
82 # minute 0-59 (use /5 for 5 minute interval)
83 # hour 0-23
84 # day of month 1-31
85 # month 1-12 (or names, see below)
86 # day of week 0-7 (0 or 7 is Sun, or use names)
87 #
88 # Re-direct output to throw away (`` >> /dev/null``)
89 # or log to a file (`` >> /some/file/some/where/log/txt``).
90 # */5 * * * * /tmp/pv/manage.sh checkup 2>&1 > /dev/null
91
92
93 get_pid
94 check_pid_running
95 if [ $RETVAL == 0 ]; then
96 echo "# [$0 $(/bin/date)] running fine, so it seems" 2>&1 > /dev/null
97 else
98 echo "# [$0 $(/bin/date)] could not identify running process ${PID}, starting new process" 2>&1 >> ${LOGFILE}
99 start
100 fi
101}
102
103
104case "$1" in
105 start) start ;;
106 stop) stop ;;
107 restart) restart ;;
108 checkup) checkup ;;
109 *)
110 echo $"Usage: $0 {start|stop|restart|checkup}"
111 exit 1
112esac
PROJECT_DIR
:The local directory for the configuration files used by the
pvWebMonitor
process. You probably want to change this. All other files are relative to the${PROJECT_DIR}
.CONFIG_FILE
:The configuration file used by the
pvWebMonitor
process. You probably want to change this. See the The config.xml file section.EXECUTABLE_SCRIPT
:The absolute path to the
pvWebMonitor
code. You probably do not need to change this.LOGFILE
:Any information logged by the
pvWebMonitor
process will be written to this file. It is not necessary for you to change this value.PIDFILE
:This file contains the current system process identifier (pid) of the
pvWebMonitor
process. Do not change this file.RETVAL
:Internal use. Do not change this.
cron - The Linux system scheduler#
Linux has a system task scheduler called cron
. (see, for example
https://opensource.com/article/17/11/how-use-cron-linux) This may be used to run
periodic processes or to run processes at certain times (such as on system
startup). You edit your cron
file and add or modify the configuration
lines for your periodic command.
Tip
Be sure to redirect the output or you’ll get lots of email from cron
!
Here, we use cron
to both start pvWebMonitor
on system startup and to
check up on it to ensure it stays running by running the manage.sh checkup
command periodically. The logic is the period between calls is the time we choose to wait
between checkups. Either on system startup or a restart of the process, this will be the
longest time that the process is not running.
To edit your cron
file, type crontab -e
on the linux command line. (You
might be asked to choose your text editor.) Paste these lines into the editor
(usually at the bottom):
# every five minutes (redirect cron output to ignore)
*/5 * * * * /tmp/pv/manage.sh checkup 2>&1 >> /dev/null
The syntax of the file is described in cron
’s documentation.
Suffice to say this runs the manage.sh checkup
command (from the /tmp/pv
directory – change to where your ${PROJECT_DIR}
directory is located).
It runs all day, every day, every 5 minutes, starting at :00, :05, :10, …