IT Consultant – Java, J2EE, IBM WebSphere Portal, Lotus Notes/Domino
RSS icon Home icon
  • Bash: Capturing stderr in a variable while still printing to the console.

    (2 votes) 1 Star2 Stars3 Stars4 Stars5 Stars
    Loading ... Loading ...
    Posted on 31 January 2011 No comments

    Storing the stdout output of a command in a variable and displaying it is simple:

    OUTPUT=$(command)
    echo $OUTPUT
    

    If you have longer running commands where you want to display stdout in realtime and also store it in a variable you can tee the output to stderr:

    OUTPUT=$(command | tee /dev/stderr)
    
    OUTPUT=$(command | tee /proc/self/fd/2)
    
    OUTPUT=$(command | tee >(cat - >&2))
    

    If you have longer running commands where you want to display stdout/stderr in realtime and also store stderr in a variable it gets a bit complicated.
    However, this can be achieved by switching stdout and stderr and then teeing the new stdout (which is stderr now) back to stderr for console output.

    ERROR=$(command 3>&1 1>&2 2>&3 | tee /dev/stderr)
    
    ERROR=$(command 3>&1 1>&2 2>&3 | tee /proc/self/fd/2)
    
    ERROR=$(command 3>&1 1>&2 2>&3 | tee >(cat - >&2))
    

    Good reading:
    Bash FAQ: How can I store the return value/output of a command in a variable?

  • Installing Tomcat 6 on Debian Squeeze

    (10 votes) 1 Star2 Stars3 Stars4 Stars5 Stars
    Loading ... Loading ...
    Posted on 13 March 2010 10 comments

    This post describes how to setup Tomcat 6 on Debian Squeeze. The configured Tomcat serves requests on port 80 without the need of an additional web server. This is especially good for virtual servers (VPS) providing limit memory. It also has multiple virtual hosts configured, each with it’s own webapp with context root / and optional support for PHP via the Quercus PHP implementation.

    Installing Sun Java 6

    Ensure the non-free section is enabled for the APT repository configuration in /etc/apt/sources.list, e.g. “deb http://ftp.de.debian.org/debian testing main contrib non-free”

    apt-get update
    apt-get install sun-java6-jdk
    echo 'JAVA_HOME="/usr/lib/jvm/java-6-sun"' >> /etc/environment
    echo 'JRE_HOME="/usr/lib/jvm/java-6-sun/jre"' >> /etc/environment
    

    Installing Tomcat 6

    apt-get install tomcat6 tomcat6-admin
    /etc/init.d/tomcat6 stop
    

    Creating standard Tomcat directory layout (optional)

    mkdir /opt/tomcat
    cd /opt/tomcat
    ln -s /etc/tomcat6/ conf
    ln -s /usr/share/tomcat6/bin/ bin
    ln -s /usr/share/tomcat6/lib/ lib
    ln -s /var/lib/tomcat6/webapps webapps
    ln -s /var/log/tomcat6/ logs
    

    Creating a Tomcat admin user

    In /opt/tomcat/conf/tomcat-users.xml add an entry like:

    <user name="ADMIN_USERNAME" password="ADMIN_PASSWORD" roles="admin,manager" />
    

    Setting up virtual hosts

    For each virtual host execute the following command. Replace “mydomain.com” with the desired virtual host name, but omit the “www.” part.

    mkdir -p /opt/tomcat/webapps.mydomain.com/ROOT
    

    In the <Engine> tag of “/opt/tomcat/conf/server.xml” add one host entry for each virtual host.

    <Host name="mydomain.com" appBase="/opt/tomcat/webapps.mydomain.com">
        <Alias>www.mydomain.com</Alias>
        <Valve className="org.apache.catalina.valves.AccessLogValve" prefix="mydomain_access_log." suffix=".txt" pattern="common"/>
    </Host>
    

    The <Alias> tag tells Tomcat to redirect from www.mydomain.com to mydomain.com.
    The <Valve> tag enables access logging in the standard logging format.

    Using xinetd to configure port 80 for Tomcat

    Binding a service on port 80 requires root permissions. Thus we use port forwarding to “bind” Tomcat to port 80. My VPS does not support the use of “iptables -j REDIRECT” therefore I am using xinetd as a web proxy.
    Ensure that no other service is listening on port 80/443:

    netstat -pan | grep ":80\|:443"
    

    Register the required xinetd services:

    echo echo "
    service www
    {
            socket_type     = stream
            protocol        = tcp
            user            = root
            wait            = no
            bind            = 88.80.198.181
            port            = 80
            redirect        = localhost 8080
            disable         = no
            flags           = REUSE
            log_type        = FILE /var/log/wwwaccess.log
            log_on_success  -= PID HOST DURATION EXIT
    
            per_source      = UNLIMITED
            instances       = UNLIMITED
    }
    
    service https
    {
            socket_type     = stream
            protocol        = tcp
            user            = root
            wait            = no
            bind            = 88.80.198.181
            port            = 443
            redirect        = localhost 8443
            disable         = no
            flags           = REUSE
            log_type        = FILE /var/log/httpsaccess.log
            log_on_success  -= PID HOST DURATION EXIT
    
            per_source      = UNLIMITED
            instances       = UNLIMITED
    }
    " > /etc/xinetd.d/tomcat
    /etc/init.d/xinetd restart
    

    If you want to use a different service name, e.g. “tomcat” instead of “www” you must add this service to /var/services, e.g. “tomcat 80/tcp”
    In /opt/tomcat/conf/server.xml modify the <Connector> as follows:

    <Connector port="8080" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8443" proxyPort="80" address="127.0.0.1" />
    

    This binds Tomcat to localhost. It also tells Tomcat that port 80 is the proxy port which is necessary for correct URL generation.
    From now on the Tomcat admin applications are only accessible via localhost. You can use SSH port forwarding to still access the applications from your workstation’s web browser. E.g. if you are using PuTTY you can use this command line option “-L 8080:localhost:8080″ to forward the server’s local 8080 port to your workstation’s local 8080 port. On your workstation’s browser you then simply enter http://localhost:8080/manager/html and are connected to the server’s Tomcat admin application.

    Enabling PHP support (optional)

    Download Quercus.

    mkdir -p /opt/downloads
    wget -o /opt/downloads/quercus-4.0.3.war http://caucho.com/download/quercus-4.0.3.war
    

    Install Quercus as a shared library.

    unzip -j /opt/downloads/quercus-4.0.3.war \*.jar -d /opt/tomcat/lib
    

    Enable PHP support for the virtual hosts by installing the quercus web.xml. For each virtual host execute:

    unzip /opt/downloads/quercus-4.0.3.war *web.xml -d /opt/tomcat/webapps.mydomain.com/ROOT
    

    Starting Tomcat

    /etc/init.d/tomcat start
    

    References

  • Determine the user who logged on via SSH

    1 Star2 Stars3 Stars4 Stars5 Stars
    Loading ... Loading ...
    Posted on 23 February 2009 No comments

    Today we had the need to determine the initial id of a user who logged onto a Linux box via SSH and executed the su command. When the su command is issued the effective user is changed and whoami or id commands will report that new user id instead.

    For anyone who is interested, that is what we came up to put the initial user id into a variable named ${LOGIN_USER}

    LOGIN_USER=`who -m`; LOGIN_USER=${LOGIN_USER%% *}

    or alternatively

    LOGIN_USER=`who -m | cut -d' ' -f1`