/ launcher / archivesspace.sh
archivesspace.sh
  1  #!/bin/bash
  2  #
  3  # archivesspace          Start the ArchivesSpace archival management system
  4  #
  5  # chkconfig: 2345 90 5
  6  # description: Start the ArchivesSpace archival management system
  7  #
  8  
  9  ### BEGIN INIT INFO
 10  # Provides: archivesspace
 11  # Required-Start: $local_fs $network $syslog
 12  # Required-Stop: $local_fs $syslog
 13  # Should-Start: $syslog
 14  # Should-Stop: $network $syslog
 15  # Default-Start: 2 3 4 5
 16  # Default-Stop: 0 1 6
 17  # Short-Description: Start the ArchivesSpace archival management system
 18  # Description:       Start the ArchivesSpace archival management system
 19  ### END INIT INFO
 20  
 21  
 22  # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
 23  function readlink_dash_f {
 24      max_iterations=32
 25  
 26      target_file=$1
 27  
 28      cd "`dirname "$target_file"`"
 29      target_file="`basename "$target_file"`"
 30  
 31      # iterate down a (possible) chain of symlinks
 32      i=0
 33      while [ -L "$target_file" ]
 34      do
 35          target_file="`readlink "$target_file"`"
 36          cd "`dirname "$target_file"`"
 37          target_file="`basename "$target_file"`"
 38  
 39          if [ $i -gt $max_iterations ]; then
 40              echo "ERROR: maximum iteration count reached ($max_iterations)" > /dev/stderr
 41              return 1
 42          fi
 43  
 44          i=$[i + 1]
 45      done
 46  
 47      # Compute the canonicalized name by finding the physical path
 48      # for the directory we're in and appending the target file.
 49      result="`pwd -P`/$target_file"
 50  
 51      echo $result
 52      return 0
 53  }
 54  
 55  
 56  
 57  
 58  cd "`dirname $0`"
 59  
 60  # Check for Java
 61  java -version &>/dev/null
 62  
 63  if [ "$?" != "0" ]; then
 64      echo "Could not run your 'java' executable."
 65      echo "Please ensure that Java 1.7 or 1.8 is installed and on your PATH"
 66      exit
 67  fi
 68  
 69  if [ ! -e "scripts/find-base.sh" ]; then
 70      cd "$(dirname `readlink_dash_f "$0"`)"
 71  fi
 72  
 73  export ASPACE_LAUNCHER_BASE="$(scripts/find-base.sh)"
 74  
 75  if [ "$ASPACE_LAUNCHER_BASE" = "" ]; then
 76      echo "Couldn't find launcher base directory!  Aborting."
 77      exit
 78  fi
 79  
 80  echo "ArchivesSpace base directory: $ASPACE_LAUNCHER_BASE"
 81  
 82  # We'll provide our own values for these
 83  unset GEM_HOME
 84  unset GEM_PATH
 85  
 86  
 87  if [ "$ARCHIVESSPACE_USER" = "" ]; then
 88      ARCHIVESSPACE_USER=
 89  fi
 90  
 91  if [ "$ARCHIVESSPACE_LOGS" = "" ]; then
 92      ARCHIVESSPACE_LOGS="logs/archivesspace.out"
 93  fi
 94  
 95  if [ "$ARCHIVESSPACE_APPEND_LOGS" = "" ]; then
 96      ARCHIVESSPACE_APPEND_LOGS=
 97  fi
 98  
 99  export JAVA_OPTS="-Darchivesspace-daemon=yes $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom"
100  
101  # Wow.  Not proud of this!
102  export JAVA_OPTS="`echo $JAVA_OPTS | sed 's/\([#&;\`|*?~<>^(){}$\,]\)/\\\\\1/g'`"
103  
104  if [ "$ASPACE_JAVA_XMX" = "" ]; then
105      ASPACE_JAVA_XMX="-Xmx1024m"
106  fi
107  
108  if [ "$ASPACE_JAVA_XSS" = "" ]; then
109      ASPACE_JAVA_XSS="-Xss2m"
110  fi
111  
112  if [ "$ASPACE_GC_OPTS" = "" ]; then
113      ASPACE_GC_OPTS="-XX:NewRatio=1"
114  fi
115  
116  
117  export JRUBY=
118  for dir in "$ASPACE_LAUNCHER_BASE"/gems/gems/jruby-*; do
119      JRUBY="$JRUBY:$dir/lib/*"
120  done
121  
122  
123  startup_cmd="java "$JAVA_OPTS"  \
124          $ASPACE_GC_OPTS $ASPACE_JAVA_XMX $ASPACE_JAVA_XSS -Dfile.encoding=UTF-8 \
125          -cp \"lib/*:launcher/lib/*$JRUBY\" \
126          org.jruby.Main --disable-gems \"launcher/launcher.rb\""
127  
128  if [ "$ASPACE_PIDFILE" = "" ]; then
129    export ASPACE_PIDFILE="$ASPACE_LAUNCHER_BASE/data/.archivesspace.pid"
130  fi
131  
132  case "$1" in
133      start)
134          if [ -e "$ASPACE_PIDFILE" ]; then
135              pid=`cat $ASPACE_PIDFILE 2>/dev/null`
136  
137              if [ "$pid" != "" ] && kill -0 $pid &>/dev/null; then
138                  echo "There already seems to be an instance running (PID: $pid)"
139                  exit
140              fi
141          fi
142  
143          shellcmd="bash"
144          if [ "$ARCHIVESSPACE_USER" != "" ]; then
145              shellcmd="su $ARCHIVESSPACE_USER"
146          fi
147  
148          redirect="&>"
149          if [ "$ARCHIVESSPACE_APPEND_LOGS" != "" ]; then
150              redirect="&>>"
151          fi
152  
153          $shellcmd -c "cd '$ASPACE_LAUNCHER_BASE';
154            (
155               exec 0<&-; exec 1>&-; exec 2>&-;
156               $startup_cmd $redirect \"$ARCHIVESSPACE_LOGS\" &
157               echo \$! > \"$ASPACE_PIDFILE\"
158            ) &
159            disown $!"
160  
161          echo "ArchivesSpace started!  See $ARCHIVESSPACE_LOGS for details."
162          ;;
163      status)
164          if [ -e "$ASPACE_PIDFILE" ]; then
165              pid=`cat $ASPACE_PIDFILE 2>/dev/null`
166  
167              if [ "$pid" != "" ] && kill -0 $pid &>/dev/null; then
168                  echo "ArchivesSpace is running as (PID: $pid)"
169                  exit
170              else
171                  echo "ArchivesSpace is not running"
172                  exit 1
173              fi
174          else
175              echo "ArchivesSpace is not running"
176              exit 1
177          fi
178          ;;
179      stop)
180          pid=`cat $ASPACE_PIDFILE 2>/dev/null`
181          if [ "$pid" != "" ]; then
182              kill -0 $pid &>/dev/null
183              if [ "$?" = "0" ]; then
184                  echo -n "Shutting down ArchivesSpace (running as PID $pid)... "
185                  kill $pid
186                  echo "done"
187              fi
188  
189              rm -f "$ASPACE_PIDFILE"
190          else
191              echo "Couldn't find a running instance to stop"
192          fi
193          ;;
194      restart)
195          $0 stop
196          $0 start
197          ;;
198      "")
199          # Run in foreground mode
200          (cd "$ASPACE_LAUNCHER_BASE"; bash -c "$startup_cmd 2>&1 | tee '$ARCHIVESSPACE_LOGS'")
201          ;;
202      *)
203          echo "Usage: $0 [start|status|stop|restart]"
204          exit 1
205          ;;
206  
207  esac