]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/examples/oils_ctl.sh
much more premacrud
[Evergreen.git] / Open-ILS / examples / oils_ctl.sh
1 #!/bin/bash
2
3 OPT_ACTION=""
4 OPT_SIP_CONFIG=""
5 OPT_PID_DIR=""
6 SIP_DIR="/opt/SIPServer";
7
8 # ---------------------------------------------------------------------------
9 # Make sure we're running as the correct user
10 # ---------------------------------------------------------------------------
11 [ $(whoami) != 'opensrf' ] && echo 'Must run as user "opensrf"' && exit;
12
13
14 function usage {
15         echo "";
16         echo "usage: $0 -d <pid_dir> -s <sip_config> -a <action>";
17         echo "";
18         echo "Actions include:"
19         echo -e "\tstart_sip"
20         echo -e "\tstop_sip"
21         echo -e "\trestart_sip"
22         echo "";
23         exit;
24 }
25
26
27 # ---------------------------------------------------------------------------
28 # Load the command line options and set the global vars
29 # ---------------------------------------------------------------------------
30 while getopts  "a:d:s:" flag; do
31         case $flag in   
32                 "a")            OPT_ACTION="$OPTARG";;
33                 "s")            OPT_SIP_CONFIG="$OPTARG";;
34                 "d")            OPT_PID_DIR="$OPTARG";;
35                 "h"|*)  usage;;
36         esac;
37 done
38
39
40 [ -z "$OPT_PID_DIR" ] && OPT_PID_DIR=/tmp;
41 [ -z "$OPT_ACTION" ] && usage;
42
43 PID_SIP="$OPT_PID_DIR/oils_sip.pid";
44
45
46 # ---------------------------------------------------------------------------
47 # Utility code for checking the PID files
48 # ---------------------------------------------------------------------------
49 function do_action {
50
51         action="$1"; 
52         pidfile="$2";
53         item="$3"; 
54
55         if [ $action == "start" ]; then
56
57                 if [ -e $pidfile ]; then
58                         pid=$(cat $pidfile);
59                         echo "$item already started : $pid";
60                         return 0;
61                 fi;
62                 echo "Starting $item";
63         fi;
64
65         if [ $action == "stop" ]; then
66
67                 if [ ! -e $pidfile ]; then
68                         echo "$item not running";
69                         return 0;
70                 fi;
71
72                 pid=$(cat $pidfile);
73                 echo "Stopping $item : $pid";
74                 kill -s INT $pid;
75                 rm -f $pidfile;
76
77         fi;
78
79         return 0;
80 }
81
82
83 # ---------------------------------------------------------------------------
84 # Start / Stop functions
85 # ---------------------------------------------------------------------------
86
87
88 function start_sip {
89         do_action "start" $PID_SIP "OILS SIP Server";
90         DIR=$(pwd);
91         cd $SIP_DIR;
92     perl SIPServer.pm "$OPT_SIP_CONFIG" > /dev/null 2>&1 &
93         pid=$!;
94         cd $DIR;
95         echo $pid > $PID_SIP;
96         return 0;
97 }
98
99 function stop_sip {
100         do_action "stop" $PID_SIP "OILS SIP Server";
101         return 0;
102 }
103
104
105
106 # ---------------------------------------------------------------------------
107 # Do the requested action
108 # ---------------------------------------------------------------------------
109 case $OPT_ACTION in
110         "start_sip") start_sip;;
111         "stop_sip") stop_sip;;
112         "restart_sip") stop_sip; start_sip;;
113         *) usage;;
114 esac;
115
116
117