Buscar este blog

sábado, 24 de junio de 2017

CentOS - Run Elasticsearch as service

I assume there is a configured Elasticsearch installation in /opt/elasticsearch-1.5.2. The purpose of this post is to show how to configure this tool to run as an auto start service in CentOS.

First of all, is a best practice to use symbolic links with these kind of installations. If you change the program version you only will need to update the symbolic reference.
ln -s /opt/elasticsearch-1.5.2 /opt/elasticsearch

Also another best practice is to run the program with its own user.
useradd elasticsearch

chown -Rf elasticsearch:elasticsearch /opt/elasticsearch
chown -Rf elasticsearch:elasticsearch /opt/elasticsearch-1.5.2

Then, you go to /etc/init.d and create the service file ("inspired" from here and from one my previous post here):
#!/bin/sh
#
# 
# elasticsearch
#
# chkconfig: 2345 90 60

 
 
# Source function library.
. /etc/init.d/functions
 

 
PROGRAM_CONSOLE_LOG=/var/log/elasticsearch/elasticsearch.log
PROGRAM_PIDFILE=/var/run/elasticsearch.pid
PROGRAM_SCRIPT="/opt/elasticsearch/bin/elasticsearch -p $PROGRAM_PIDFILE"


 
 
PROGRAM_USER=elasticsearch
prog='Elasticsearch'
 
 
 
# Set defaults.
export PROGRAM_PIDFILE
 
 
if [ -z "$STARTUP_WAIT" ]; then
  STARTUP_WAIT=30
fi

 
if [ -z "$SHUTDOWN_WAIT" ]; then
  SHUTDOWN_WAIT=30
fi
 
 
 
start() {
  echo -n "Starting $prog: "
  if [ -f $PROGRAM_PIDFILE ]; then
    read ppid < $PROGRAM_PIDFILE
    if [ `ps --pid $ppid 2> /dev/null | grep -c $ppid 2> /dev/null` -eq '1' ]; then
      echo -n "$prog is already running"
      failure
      echo
      return 1
    else
      rm -f $PROGRAM_PIDFILE
    fi
  fi
  mkdir -p $(dirname $PROGRAM_CONSOLE_LOG)
  cat /dev/null > $PROGRAM_CONSOLE_LOG
  chown -Rf $PROGRAM_USER $(dirname $PROGRAM_CONSOLE_LOG) || true
 
 
  mkdir -p $(dirname $PROGRAM_PIDFILE)
  chown $PROGRAM_USER $(dirname $PROGRAM_PIDFILE) || true
 
   
  daemon --user $PROGRAM_USER --pidfile $PROGRAM_PIDFILE "$PROGRAM_SCRIPT" 1>"$PROGRAM_CONSOLE_LOG" 2>&1 &
  sleep 2
 # pidofproc node > $PROGRAM_PIDFILE
 
  count=0
  launched=false
 
  until [ $count -gt $STARTUP_WAIT ]
  do
    echo 'checking start...'
    grep 'started' $PROGRAM_CONSOLE_LOG > /dev/null
    if [ $? -eq 0 ] ; then
      launched=true
      break
    fi
    sleep 1
    let count=$count+1;
  done
 
  if [ "$launched" = true ] ; then
    echo 'Unable to start program'
  fi 

  success
  echo
  return 0
}
 
stop() {
  echo -n $"Stopping $prog: "
  count=0;
 
  if [ -f $PROGRAM_PIDFILE ]; then
    read kpid < $PROGRAM_PIDFILE
    let kwait=$SHUTDOWN_WAIT
 
    # Try issuing SIGTERM
 
    kill -15 $kpid
    until [ `ps --pid $kpid 2> /dev/null | grep -c $kpid 2> /dev/null` -eq '0' ] || [ $count -gt $kwait ]
    do
      sleep 1
      let count=$count+1;
    done
 
    if [ $count -gt $kwait ]; then
      kill -9 $kpid
    fi
  fi
  rm -f $PROGRAM_PIDFILE
  success
  echo
}
 
status() {
  if [ -f $PROGRAM_PIDFILE ]; then
    read ppid < $PROGRAM_PIDFILE
    if [ `ps --pid $ppid 2> /dev/null | grep -c $ppid 2> /dev/null` -eq '1' ]; then
      echo "$prog is running (pid $ppid)"
      return 0
    else
      echo "$prog dead but pid file exists"
      return 1
    fi
  fi
  echo "$prog is not running"
  return 3
}
 
case "$1" in
  start)
      start
      ;;
  stop)
      stop
      ;;
  restart)
      $0 stop
      $0 start
      ;;
  status)
      status
      ;;
  *)
      ## If no parameters are given, print which are avaiable.
      echo "Usage: $0 {start|stop|status|restart|reload}"
      exit 1
      ;;
esac

Finally, you have to make this script as runnable and configure the service as autostart.
chmod +x /etc/init.d/elasticsearch

chkconfig --add elasticsearch
chkconfig elasticsearch on

viernes, 23 de junio de 2017

CentOS - Install Java

Steps to install a JDK in Centos 6:
[root@servidor ~]# mkdir /usr/java
[root@servidor ~]# tar -zxvf jdk-8u121-linux-x64.tar.gz --directory /usr/java

In order to set this JDK as default installation, you have to run the followings commands:
[root@servidor ~]# alternatives --install "/usr/bin/javac" "javac" "/usr/java/jdk1.8.0_121/bin/javac" 50

[root@servidor ~]# alternatives --install "/usr/bin/java" "java" "/usr/java/jdk1.8.0_121/bin/java" 50

[root@servidor ~]# alternatives --install "/usr/bin/javaws" "javaws" "/usr/java/jdk1.8.0_121/bin/javaws" 50

[root@servidor ~]# alternatives --install "/usr/bin/jar" "jar" "/usr/java/jdk1.8.0_121/bin/jar" 50

CentOS - Iptables - Open TCP ports

In order to open TCP ports in a CentOS 6 machine you have to edit the following files:
  • /etc/sysconfig/iptables
  • /etc/sysconfig/ip6tables

For example, to open HTTP and HTTPS ports, you can use the following lines:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT

Then, just restart the services:
[root@ servidor ~]# /etc/init.d/iptables restart
[root@ servidor ~]# /etc/init.d/ip6tables restart

lunes, 5 de junio de 2017

Windows 7 - Set default TWAIN scanner

I had a Epson scanner installed in Windows 7. Then I installed another scanner, a fujitsu, and unplugged the first one.

When working with an application wich used TWAIN drivers in order to connect to the scanner, the default device was the Epson. Specifically, the application used the Dynamsoft Dynamic Web TWAIN plugin.

The default TWAIN device is stored in Windows registry, under the following key:
HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\TWAIN




In the referenced folder, C:\Windows\twain_32, I had the following structure:



The solution was just to select the fujitsu ds file, and update the registry entry.