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}
23
24
25check_pid_running(){
26 get_pid
27 if [ "${PID}" == "" ]; then
28 # no PID in the PIDFILE
29 RETVAL=1
30 else
31 RESPONSE=$(ps -p "${PID}" -o comm=)
32 if [ "${RESPONSE}" == "pvWebMonitor" ]; then
33 # PID matches the pvWebMonitor profile
34 RETVAL=0
35 else
36 # PID is not pvWebMonitor
37 RETVAL=1
38 fi
39 fi
40 return $RETVAL
41}
42
43
44start(){
45 cd "${PROJECT_DIR}"
46 "${EXECUTABLE_SCRIPT}" "${CONFIGFILE}" 2>&1 >> "${LOGFILE}" &
47 PID=$!
48 /bin/echo "${PID}" > "${PIDFILE}"
49 /bin/echo "# [$0 $(/bin/date)] started "${PID}": "${EXECUTABLE_SCRIPT}"" 2>&1 >> "${LOGFILE}" &
50 /bin/echo "# [$0 $(/bin/date)] started "${PID}": "${EXECUTABLE_SCRIPT}""
51}
52
53
54stop(){
55 get_pid
56 check_pid_running
57
58 if [ ${RETVAL} == 1 ]; then
59 /bin/echo "# [$0 $(/bin/date)] not running "${PID}": "${EXECUTABLE_SCRIPT}"" 2>&1 >> "${LOGFILE}" &
60 else
61 kill "${PID}"
62 /bin/echo "# [$0 $(/bin/date)] stopped "${PID}": "${EXECUTABLE_SCRIPT}"" 2>&1 >> "${LOGFILE}" &
63 /bin/echo "# [$0 $(/bin/date)] stopped "${PID}": "${EXECUTABLE_SCRIPT}""
64 fi
65 /bin/cp -f /dev/null "${PIDFILE}"
66}
67
68
69restart(){
70 stop
71 start
72}
73
74
75checkup(){
76 #=====================
77 # call periodically (every 5 minutes) to see if pvWebMonitor is running
78 #=====================
79 # field allowed values
80 # ----- --------------
81 # minute 0-59 (use /5 for 5 minute interval)
82 # hour 0-23
83 # day of month 1-31
84 # month 1-12 (or names, see below)
85 # day of week 0-7 (0 or 7 is Sun, or use names)
86 #
87 # Re-direct output to throw away (>> /dev/null)
88 # or log to a file (>> /some/file/some/where/log/txt).
89 # */5 * * * * /tmp/pv/manage.sh checkup 2>&1 > /dev/null
90
91
92 get_pid
93 check_pid_running
94 if [ $RETVAL == 0 ]; then
95 echo "# [$0 $(/bin/date)] running fine, so it seems" 2>&1 > /dev/null
96 else
97 echo "# [$0 $(/bin/date)] could not identify running process "${PID}", starting new process" 2>&1 >> "${LOGFILE}"
98 start
99 fi
100}
101
102
103case "$1" in
104 start) start ;;
105 stop) stop ;;
106 restart) restart ;;
107 checkup) checkup ;;
108 *)
109 echo $"Usage: $0 {start|stop|restart|checkup}"
110 exit 1
111esac
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, …