linux redirect java System.out to file - java

I running jar via "start-stop-daemon". My code:
PIDFILE=/var/run/$NAME.pid
USER=root
MAIN="/usr/share/folder/jarfile.jar"
DAEMON="/usr/bin/java"
ARGS="-server -Djava.awt.headless=true -jar $MAIN"
start() {
start-stop-daemon --start --pidfile "$PIDFILE" --chuid "$USER" --background --make-pidfile --startas "$DAEMON" -- $ARGS
}
stop() {
start-stop-daemon --stop --quiet --pidfile $PIDFILE
if [ -e $PIDFILE ]
then rm $PIDFILE
fi
}
I want redirect java System.out to file. When I try this code:
start() {
start-stop-daemon --start --pidfile "$PIDFILE" --chuid "$USER" --background --make-pidfile --startas /bin/bash -- -c "$DAEMON $ARGS > /usr/share/folder/log.txt 2>&1"
}
method stop() does not stoped deamon.
How possibile to redirect System.out to log file?
Thanks!

You have just did it in the second part, please check the log in /usr/share/folder/log.txt

Related

CrossCompile jrxtx with yocto

I'm trying to crosscompile jrxtx library (https://github.com/openmuc/jrxtx) using a yocto toolchain but it fails.
first
source /opt/poky/environment-setup-cortexa8t2hf-vfp-neon-poky-linux-gnueabi
then
cd /home/peppe/projects/rxtx/jrxtx/native && ./BUILD.sh
I had to hand modify config.status to remove some errors on mv and rm, nevertheless i get
/bin/bash /home/peppe/projects/rxtx/jrxtx/native/libtool --mode=link arm-poky-linux-gnueabi-gcc -march=armv7-a -marm -mthumb-interwork -mfloat-abi=hard -mfpu=neon -mtune=cortex-a8 --sysroot=/opt/poky/sysroots/cortexa8t2hf-vfp-neon-poky-linux-gnueabi -O2 -pipe -g -feliminate-unused-debug-types -D_POSIX_SOURCE -D_BSD_SOURCE -D__need_timespec -Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed -lpthread -release 2.2pre1 -o librxtxSerial.la -rpath /usr/lib/jvm/java-11-openjdk-amd64/jre/lib/amd64 /home/peppe/projects/rxtx/jrxtx/native/x86_64-unknown-linux-gnu/SerialImp.lo; \
fi; \
)
/bin/bash: line 2: cd: x86_64-unknown-linux-gnu: No such file or directory
/bin/bash: /home/peppe/projects/rxtx/jrxtx/native/libtool: No such file or directory
/bin/bash: /home/peppe/projects/rxtx/jrxtx/native/libtool: No such file or directory
/bin/bash: /home/peppe/projects/rxtx/jrxtx/native/libtool: No such file or directory
make: *** [Makefile:756: x86_64-unknown-linux-gnu/librxtxSerial.la] Error 127
Even it it seems to launch the correct compiler arm-poky-linux-gnueabi-gcc it tries to cd into x86_64-unknown-linux-gnu and obviously things gets mad...

fabric-java-sdk: where to change the chaincode

Foreword
Hi, I am new to stackoverflow. If there is any place that is not clear, please point it out. Thank you!
Question
I just started to study hyperledger-fabric. As a Java programmer, I choose to use the fabric-java-sdk.
After I can run the test case End2endIT.java, I want to change the chaincode. I just find the example_cc.go at fabric-sdk-java/src/test/fixture/sdkintegration/gocc/sample1/src/github.com/example_cc/example_cc.go . However, after I changed the chaincode, it did't work. Even after I deleted this code, the test case can still run.
Therefore, I guess I found a wrong place. Can anyone tell me where to change the chaincode? Thx!
Additional
The code to load chaincode
if (isFooChain) {
// on foo chain install from directory.
////For GO language and serving just a single user, chaincodeSource is mostly likely the users GOPATH
installProposalRequest.setChaincodeSourceLocation(new File(TEST_FIXTURES_PATH + "/sdkintegration/gocc/sample1"));
//[output]: src/test/fixture/sdkintegration/gocc/sample1
System.out.println(TEST_FIXTURES_PATH + "/sdkintegration/gocc/sample1");
} else {
// On bar chain install from an input stream.
installProposalRequest.setChaincodeInputStream(Util.generateTarGzInputStream(
(Paths.get(TEST_FIXTURES_PATH, "/sdkintegration/gocc/sample1", "src", CHAIN_CODE_PATH).toFile()),
Paths.get("src", CHAIN_CODE_PATH).toString()));
}
I solved the question in the end as I noticed the fabric.sh in the fabric-sdk-java.
./fabric.sh up to force recreate the docker container
./fabric.sh clean to clean the peers
The reason why I could run the invoke request without chaincode is that I didn't clean the volumns of peers.
And the source code as follows:
#!/usr/bin/env bash
#
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
# simple batch script making it easier to cleanup and start a relatively fresh fabric env.
if [ ! -e "docker-compose.yaml" ];then
echo "docker-compose.yaml not found."
exit 8
fi
ORG_HYPERLEDGER_FABRIC_SDKTEST_VERSION=${ORG_HYPERLEDGER_FABRIC_SDKTEST_VERSION:-}
function clean(){
rm -rf /var/hyperledger/*
if [ -e "/tmp/HFCSampletest.properties" ];then
rm -f "/tmp/HFCSampletest.properties"
fi
lines=`docker ps -a | grep 'dev-peer' | wc -l`
if [ "$lines" -gt 0 ]; then
docker ps -a | grep 'dev-peer' | awk '{print $1}' | xargs docker rm -f
fi
lines=`docker images | grep 'dev-peer' | grep 'dev-peer' | wc -l`
if [ "$lines" -gt 0 ]; then
docker images | grep 'dev-peer' | awk '{print $1}' | xargs docker rmi -f
fi
}
function up(){
if [ "$ORG_HYPERLEDGER_FABRIC_SDKTEST_VERSION" == "1.0.0" ]; then
docker-compose up --force-recreate ca0 ca1 peer1.org1.example.com peer1.org2.example.com ccenv
else
docker-compose up --force-recreate
fi
}
function down(){
docker-compose down;
}
function stop (){
docker-compose stop;
}
function start (){
docker-compose start;
}
for opt in "$#"
do
case "$opt" in
up)
up
;;
down)
down
;;
stop)
stop
;;
start)
start
;;
clean)
clean
;;
restart)
down
clean
up
;;
*)
echo $"Usage: $0 {up|down|start|stop|clean|restart}"
exit 1
esac
done

object com.github.nscala_time.time.Imports not found error?

I used package com.github.nscala_time.time.Imports in code and I m running the code using spark.
Here is my stream.sh file:
#!/bin/bash
JARS_HOME=$HOME/spark-job/lib
JARS=$JARS_HOME/job-server-api_2.10-0.6.0.jar,$JARS_HOME/httpmime-4.4.1.jar,$JARS_HOME/noggit-0.6.jar,$JARS_HOME/nscala-time_2.10-2.0.0.jar
export SPARK_IP=`ifconfig | grep eth0 -1 | grep -i inet | awk '{ print $2 }' | cut -d':' -f2`
APP_JAR=$JARS_HOME/spark-jobs-tests.jar
export SPARK_LOCAL_IP=$SPARK_IP
dse spark-submit --conf "spark.cassandra.input.consistency.level=LOCAL_QUORUM" \
--total-executor-cores 2 \
--jars=$JARS \
--class "my file classpath" $APP_JAR "$1" --files $1
I have set $JARS_HOME/nscala-time_2.10-2.0.0.jar in .sh then still getting following error:
Exception in thread "main" scala.reflect.internal.MissingRequirementError: object com.github.nscala_time.time.Imports not found.
at scala.reflect.internal.MissingRequirementError$.signal(MissingRequirementError.scala:16)
at scala.reflect.internal.MissingRequirementError$.notFound(MissingRequirementError.scala:17)
at scala.reflect.internal.Mirrors$RootsBase.ensureModuleSymbol(Mirrors.scala:126)
at scala.reflect.internal.Mirrors$RootsBase.staticModule(Mirrors.scala:161)
at scala.reflect.internal.Mirrors$RootsBase.staticModule(Mirrors.scala:21)
How to resolve this??

linux top command CPU usage doesn't add up / how to view Java threads usage in realtime?

When I run top -p <pid> command, I can see that process is using about 10% of cpu but when I press "H" to list threads, cpu usage of each of the thread is shown as 0%.
Why is that so? I want to see Java threads CPU usage in realtime, is top not the best way?
Screenshots
(before pressing H)
After pressing H-
If you prefer to work with a script like tool there is also jvmtop that gives you the cpu usage per thread.
Sample output bellow.
You can use jconsole if you are using the Sun Java JDK Distribution. It is built in as part of the bundle from Java 1.5 onwards if I am not mistaken. You can see Threads, Classes loaded, Memory Usage and CPU Usage of JVMs in real time.
You can use VisualVM or jconsole.
Java 1.6 and above version have inbuild java tools for monitoring / debugging java modules.
Here isthe below sample script :
`
#/bin/bash
#Initilization of variable
CUREENT_DATE=`date -d "0 day" "+%Y%m%d"` #2012-06-22
CURRENT_MONTH=`date -d "0 month" "+%B%Y"` #June2012
LAST_DATE=`date -d "1 day ago" "+%Y%m%d"` #2012-06-21
BASE_PATH="/home/javaMonitoring"
SCRIPT_LOGS="/home/javaMonitoring/logs"
JAVA_PATH=`echo $JAVA_HOME`
PERM=0
MSG="No Message"
HOSTIP="localhost"
THRESHOLD=80;
if [ ! -d $BASE_PATH ];then
echo " Base Directory not exists![$BASE_PATH]"
exit
fi
cd $BASE_PATH
if [ ! -d $SCRIPT_LOGS ];then
echo " Log Directory note found [$SCRIPT_LOGS]"
mkdir -p $SCRIPT_LOGS
fi
##LOG Directory
if [ ! -d $CUREENT_DATE ];then
echo " Log Directory note found [$CUREENT_DATE]"
mkdir -p $CUREENT_DATE
fi
log_file_path="$SCRIPT_LOGS/${CUREENT_DATE}-"`basename $0 .sh`".log"
echo "############ Script:$log_file_path##################" >$log_file_path
#####Funcations
cd $JAVA_PATH/bin
BOOTSTRAP=`${JAVA_PATH}/bin/jps|grep Bootstrap|cut -d " " -f1`
if [ $? -ne 0 ];then
echo "No Bootstrap PID exists[${JAVA_PATH}/bin/jps|grep Bootstrap]" >>$log_file_path
exit
#If log file not exists
else
if [ ! -f ${BASE_PATH}/${CUREENT_DATE}/gcutil.txt ];then
touch ${BASE_PATH}/${CUREENT_DATE}/gcutil.txt
echo "Timestamp S0 S1 E O P YGC YGCT FGC FGCT GCT" > ${BASE_PATH}/${CUREENT_DATE}/gcutil.txt
fi
./jstat -gcutil -t $BOOTSTRAP|grep -v Timestamp >> ${BASE_PATH}/${CUREENT_DATE}/gcutil.txt
PERM=`jstat -gcutil -t ${BOOTSTRAP} | grep -v Timestamp|awk -F " " '{print $6}'`
#if [ ${PERM} -ge ${THRESHOLD} ];then
if [ $PERM > $THRESHOLD ];then
MSG="MCOM: Alert `date` on ${HOSTIP} Server Current Perm is :: ${PERM}%"
fi
#continue;
fi `
OUTPUT save in :: ../YYYYMMDD/gcutil.txt
Timestamp S0 S1 E O P YGC YGCT FGC FGCT GCT
16193.4 0.00 59.95 55.13 93.86 89.63 226 26.192 41 48.532 74.724
16196.5 0.00 59.95 55.14 93.86 89.63 226 26.192 41 48.532 74.724

Backgrounding, Java & init script

I am trying to write an init script for a java program. The program has no ability to background itself, so I'm using & to do it. It writes its logfiles to stdout.
The problem I have is regarding the RETVAL in the init script. Here's the function I'm using:
JAR_FILE=/opt/application/server/server.jar
JAVA=/usr/bin/java
start() {
echo -n $"Starting ${NAME}: "
daemon --pidfile=${PID_FILE} --user $USER \
$JAVA $JAVA_ARGS >> /var/log/application/server.log &
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $LOCKFILE
return $RETVAL
}
When I run the init script, the app starts up or fails nicely, the problem is that the retval is being written to the logfile, rather than to stdout:
[root#rpmbuild rpmbuild]# /etc/init.d/application-server start
Starting application-server:
[root#host]# tail /var/log/application/server.log
JAVA APPLICATION ERRORS ARE HERE
[FAILED]
Is there anyway I can make it log to stdout and also ensure that any retval (whether it good or bad) gets echoed to stdout?
There are options on how to direct the output when using the daemon command
-l, --errlog=spec - Send daemon's error output to syslog or file
-b, --dbglog=spec - Send daemon's debug output to syslog or file
-o, --output=spec - Send client's output to syslog or file
-O, --stdout=spec - Send client's stdout to syslog or file
-E, --stderr=spec - Send client's stderr to syslog or file
I would change the output using the -o option and not redirect. You should not have to background the task either
daemon -o /var/log/application/server.log -name SuperJavaProgram \
--pidfile=${PID_FILE} --user $USER $JAVA $JAVA_ARGS
Then you could test if it's running with daemon --running -n SuperJavaProgram
Maybe, you could print to stderr in java, to separate standard logs and errors. Your >> will redirect only stdout.
BTW, it's surprising to test $? whereas your program as not executed yet, because background-ed.

Categories

Resources