pCOWeb
+030220966 – rel. 1.1 – 12.12.2017
81
START-UP SCRIPTS
These are ALL the files in the Plugin “rc_users” directory on the Plugin being installed. They are used to be able to run the Plugin functions automatically
when the system is started. During the installation the scripts are installed in the “/usr/local/root/flash/rc_users/” directory, the path string is also returned by
the $RC_USERS system variable. During installation, each of these files are attributed all the permissions required by the Linux operating system, in the
same way as for the executable files seen above (Figure I.c on page 80).
NOTE
The execution permissions are also used by the system to decide which scripts to run automatically; to prevent a Plugin from being run at start-up,
reset the execution bits (“Execute”) relating to the script.
Structure of the start-up scripts
For the start-up script to run correctly, it must have the following structure (the example refers to the NTP Plugin):
1 #!/bin/bash
2
3 .
/etc/init.d/functions/highlight
4
5
case "$1" in
6 start)
7
showMsg "Starting Ntp script..."
8
(ntp.sh &) > /dev/null
9 showStatus
"$?"
10
;;
11
12
stop)
13
showMsg "Stopping Ntp Script..."
14
id=$(pidof -x ntp.sh)
15
kill
$id
16
showStatus
"kill$id"
17
;;
18
19
restart)
20
showMsg "Restart ntp Script..."
21
$0
stop
22
$0
start
23
;;
24
25
*)
26
echo $"Usage: $0 {start|stop|restart}"
27
exit
1
28
;;
29 esac
DESCRIPTION OF THE LINES
1:
Specifies the command executor, currently the only one featured on the
pCOWeb
is the bash shell (“Bourne Again SHell”), therefore “/bin/bash”.
3:
Add the colouring function for the log messages (see the “highlight” script).
5-6:
When the script is run, parameter “$1” is equal to “start”.
7:
showMsg (see lines 13-18 of the “highlight” script) displays the text “Starting Ntp script…” in the system log.
8:
This line runs the “ntp.sh” script (see the previous section); note that the standard output is redirected to “/dev/null” to avoid unnecessary messages.
9:
showStatus displays the result with the related colouring (see lines 20-37 of the “highlight” script); note the variable “$?”, with the value returned by
the previously run statement.
12-17: As for the start section, the stop section is made up of the same parts, and is called when the
pCOWeb
is shutdown or when accessed by the user.
19-23: Runs the script again, first the stop section and then the start section, used to have the Plugin reread the new configuration files; it cannot be run
from the web page but only from a remote console.
24-28: This is the section of the script that is run in the event where “$1” is not equal to start, stop or restart; a message is displayed to indicate that the
script has been run with an incorrect value of “$1”.
“Highlight” script
This script is used to define the standard display used in the various scripts on the
pCOWeb
.
1
export escGreen=`echo -e "\033[1m\033[32m"`
2
export escRed=`echo -e "\033[1m\033[31m"`
3
export escYell=`echo -e "\033[1m\033[33m"`
4
export escNorm=`echo -e "\033[m"`
5
export msgOk=`echo -e "[\033[1m\033[32mOK\033[m]"`
6
export msgDone=`echo -e "[\033[1m\033[32mDone\033[m]"`
7
export msgYes=`echo -e "[\033[1m\033[32mYes\033[m]"`
8
export msgNo=`echo -e "[\033[1m\033[32mNo\033[m]"`
9
export msgError=`echo -e "[\033[31mError\033[m]"`
10
export msgFail=`echo -e "[\033[31mFailed\033[m]"`
11
export msgBad=`echo -e "[\033[31mBad\033[m]"`
12
13 #
14
# Echo script message
15 #
16 function
showMsg(){
17
printf "%-70s" "$1"
18 }
19
20 #
21
# Show status message
22 #
23 function
showStatus(){
24
case "$1" in
25
0) echo
"$msgOk";;
26
1) echo
"$msgFail";;
27
ok)
echo
"$msgOk";;
28
error)
echo
"$msgError";;
29
bad)
echo
"$msgBad";;
30
yes)
echo
"$msgYes";;
31
no)
echo
"$msgNo";;
32
done)
echo
"$msgDone";;
33
failed)
echo
"msgFail";
34
kill*)
test "$1" = "kill" && echo '[Not runn]' || echo $msgOk;;
35
*) echo
"$1";;
36
esac
37 }