check if program running in linux

In shell,we use the below program to check if some program already running:
export HOMEDIR=/../../
export JAVA_HOME=/usr/lib/jvm/java-1.6.0
export LOGFILE=/tmp/log_`date +"%Y%m%d-%H%M%S"`.log
export JAVA_CMD=$JAVA_HOME/bin/java
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$HOMEDIR/lib/ojdbc-14.jar:$HOMEDIR/lib/log4j-1.2.14.jar:$HOMEDIR/lib/commons-logging-1.1.jar
export MAIN_PROGRAM=com.test.theClassEntry
ps -ef | grep "java" > SomeFile
num=$(wc -l SomeFile | awk '{print $1}')
if [ $num -gt 0 ]
then
echo "the java process is running" > $LOGFILE
exit 1
else
nohup $JAVA_CMD -Xmx10g -Dflag=true -classpath "$CLASSPATH" $MAIN_PROGRAM program_parameter > $LOGFILE 2>&1 &
fi

note:
1.-Dproperty=value
Set a system property value.You can call the following from anywhere in the code to read the properties given on the command line:
String value = System.getProperty("key", "defaultvalue");
2.
http://www.linfo.org/ps.html
An alternative set of options for viewing all the processes running on a system is
ps -ef | less
The -e option generates a list of information about every process currently running. The -f option generates a listing that contains fewer items of information for each process than the -l option.
3.
http://www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples/
Awk Example Print only specific field.

Awk has number of built in variables. For each record i.e line, it splits the record delimited by whitespace character by default and stores it in the $n variables. If the line has 4 words, it will be stored in $1, $2, $3 and $4. $0 represents whole line. NF is a built in variable which represents total number of fields in a record.

$ awk '{print $2,$5;}' employee.txt
Thomas $5,000
Jason $5,500
Sanjay $7,000
Nisha $9,500
Randy $6,000

$ awk '{print $2,$NF;}' employee.txt
Thomas $5,000
Jason $5,500
Sanjay $7,000
Nisha $9,500
Randy $6,000
In the above example $2 and $5 represents Name and Salary respectively. We can get the Salary using $NF also, where $NF represents last field. In the print statement ‘,’ is a concatenator.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值