Thursday, June 07, 2012

Ubuntu Startup Script for Jboss as Non-root User

So ever since I started using Ubuntu I have been trying to figure out startup scripts. I think I finally cracked the code. Below is a startup script (saved in /etc/init.d/jboss ) that I use to start/stop a jboss instance on my ubuntu vm. To actually install I had to run this:

sudo update-rc.d jboss defaults

Notice that my jboss does NOT run as root and its just installed in my users home directory. I'm not a fan of running services as root. Here is the startup script:

#!/bin/sh

### BEGIN INIT INFO
# Provides:             jboss
# Required-Start:       $local_fs $remote_fs $network $syslog
# Required-Stop:        $local_fs $remote_fs $network $syslog
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    JBoss Server
# Description:          JBoss Application Server
### END INIT INFO

#
# this is a basic start stop script that can be run as
# $0 start or $0 stop or $0 restart
#

JBOSS_HOME=/home/mdeanda/install/jboss
JBOSS_SCRIPT=./bin/standalone.sh
USER=mdeanda

set -e

. /lib/lsb/init-functions


start()
{
 cd ${JBOSS_HOME}
 rm -f /var/log/jboss.out
 touch /var/log/jboss.out
 chown ${USER} /var/log/jboss.out
 su ${USER} -c ${JBOSS_SCRIPT} > /var/log/jboss.out &
}

stop()
{
 #ps ax | grep java | grep jboss | cut -d' ' -f2 | xargs kill
 su ${USER} -c "${JBOSS_HOME}/bin/jboss-cli.sh --connect command=:shutdown"
}

case "$1" in
start)
 start
 ;;
stop)
 stop
 ;;
restart)
 stop
 start
 ;;
*)
 echo "Usage: $0 {start|stop|restart}"
 exit 1
esac

Wednesday, June 06, 2012

Control Arduino Sensor through your Browser!

I modified the basic Web Server example from the Arduino IDE to allow my arduino to control digital pins and report values of all available pins. I'm eventually going to use this to read the temperature of my server closet and control fans and turn computers on and off. I'll write a separate desktop program to actually control the device.
/*
  Web Enabled Sensor and Switch

 A simple web server that shows the value of the analog input pins and
 digits pins using an Arduino Wiznet Ethernet shield. Special URLs can
 drive the pins 0-9 (since 10-13 are used by ethernet shield).

 Use http://192.168.0.177/ to read the current state.
 Use http://192.168.0.177/01234 to toggle pints 0-4.
 Use http://192.168.0.177/2-3+ to turn off 2, turn on 3.

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5 (optional)
 * Digital outputs attached to pins D0 through D9 (optional)

 created 6 June 2012
 by Miguel De Anda
 based on Web Server example included in arduino package

 */

#include 
#include 
#include 
#include 

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0, 177);
int pinState[10];

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup()
{
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.begin(9600);
  for (int i=0; i<10; i++) {
    pinState[i] = LOW;
    pinMode(i, OUTPUT);
    digitalWrite(i, LOW);
  }
}

void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    int position = 0;
    boolean isFirstLine = true;
    char line[200];
    char firstLine[200];
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          interpret(firstLine);
          respond(firstLine, client);
          break;
        }
        if (c == '\n') {
          line[position] = '\0';
          // you're starting a new line
          currentLineIsBlank = true;
          position = 0;
          //now check if this line was a request line
          if (isFirstLine) {
            strcpy(firstLine, line);
            isFirstLine = false;
          }
        } else if (c != '\r') {
          line[position] = c;
          position++;
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }

    //serial_out(line);
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}

void respond(char * request, EthernetClient & client)
{
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/plain");
  client.println();

  // output the value of each analog input pin

  for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
    client.print("A");
    client.print(analogChannel);
    client.print("=");
    client.print(analogRead(analogChannel));
    client.println("");
  }
  for (int i=0; i<10; i++) {
    client.print("D");
    client.print(i);
    client.print("=");
    client.println(pinState[i]);
  }

  client.println(request);
}

void interpret(char * request)
{
  //only chars between first and second space are interpretted
  //digit means toggle that pin
  //later: digit followed by + means turn it on, - means off.
  int size = strlen(request);
  boolean started = false;
  int lastDigit = -1;
  for (int i=0; i= '0' && c <= '9') {
        int digit = c - '0';
        if (lastDigit != -1) {
          toggle(lastDigit);
        }
        lastDigit = digit;
      }
    }
  }
  if (lastDigit != -1) {
    toggle(lastDigit);
  }
}

void toggle(int lastDigit) {
  int state = pinState[lastDigit] == HIGH ? LOW : HIGH;
  pinState[lastDigit] = state;
  digitalWrite(lastDigit, state);
}