- content
工作中常用 Shell 命令
Shell 命令详解
1.Shell if 判断
1.使用[]和[[]]的时候不要吝啬空格,每一项两边都要有空格,[[ 1 == 2 ]]的结果为“假”,但[[ 1==2 ]]的结果为“真”!
– 场景:java 启动参数配置时
#!/bin/bash
echo "export LANG=en_US.UTF-8" >> /etc/profile
source /etc/profile
mkdir -p /logs/$LOG_PREFIX/`hostname` /data/logs
ln -s /logs/$LOG_PREFIX/`hostname` $LOG_HOME
if [ "$ACTIVE" ]; then
export JAVA_OPTS="$JAVA_OPTS -Dspring.profiles.active=$ACTIVE"
else
export JAVA_OPTS="$JAVA_OPTS -Dspring.profiles.active=test"
fi
# shell if [ "$ACTIVE"=="dev" ]; //不管dev 还是其它值都默认为真
if [ "$ACTIVE" == "dev" ]; then
export JAVA_OPTS="$JAVA_OPTS -Xms128m -Xmx1024m -XX:+HeapDumpOnOutOfMemoryError"
fi
if [ "$ACTIVE" == "test" ]; then
export JAVA_OPTS="$JAVA_OPTS -Xms128m -Xmx1280m"
fi
if [ "$ACTIVE" == "prod" ]; then
export JAVA_OPTS="$JAVA_OPTS -Xms256m -Xmx3072m"
fi
# -XX:+UseG1GC -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -XX:MaxRAMFraction=1
JAVA_OPTS="$JAVA_OPTS -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -verbose:gc -Xloggc:$LOG_HOME/gc.log"
echo -e "Starting the Server ...\c"
mkdir -p $LOG_HOME
echo "" >>$LOG_HOME/gc.log
if [ "$APOLLO" ]; then
java $JAVA_OPTS -Dapollo.meta=$APOLLO -jar /data/$JOB --server.port=$PORT --apollo.bootstrap.enabled=true
else
java $JAVA_OPTS -jar /data/$JOB --server.port=$PORT
fi
2.通过名称杀掉进程
# 文件名称: /opt/data_warehouse/schedule/kill_job.sh
#!/bin/bash
source ~/.bash_profile
echo "input parameter is: $1 "
ps -ef | grep $1 | grep -v grep | awk '{print $2}' | xargs kill -9
# 执行脚本 /opt/data_warehouse/schedule/kill_job.sh jobName.kjb
# 杀掉进程,并启动应用 smartfire 应用名称
ps -ef | grep smartfire | grep -v grep | awk '{print $2}' | xargs kill -9 && nohup java -jar -Dspring.profiles.active=dev smartfire-admin.jar >msg.log 2>&1 &