Linux命令封装手记

Posted Jack.London

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux命令封装手记相关的知识,希望对你有一定的参考价值。

1、Linux系统中有很多命令,无论tomcat,nginx,docker等应用部署到Linux服务器之后。不管启动,停止,查看日志或者杀死进程都要到对应的目录下或者通过管道符号拼接命令才能达到效果。这样是比较耗时的,而且需要记忆很多命令。这里讲一下如何封装一些简单易记的命令

2、首先进入tomcat目录

创建文件夹,在文件夹放入写好的脚本命令,注意给脚本命令赋予权限,我一般都是直接chmod 777 iread.sh,即赋予该文件所有的权限。这里我在服务器上面主要封装了一些工程重启,停止,查看日志的命令

3、然后进入用户目录下,找到.bash_profile文件,如果没有就自己创建一个。

这个文件我们一般都做成隐藏文件,因为一般不需要更改。同时写好之后,需要执行source  .bash_profile命令,让命令生效,然后就可以在sns用户下面执行你封装好的命令了。

 附件1:

封装的系统命令(.bash_profile)

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/.local/bin:$HOME/bin

export PATH


export JAVA_HOME="/usr/jdk1.8.0_102"
export CATALINA_TMPDIR="/tmp"
export JAVA_BINDIR="${JAVA_HOME}/bin"
export JDK_HOME="${JAVA_HOME}"
export SDK_HOME="${JAVA_HOME}"
export PATH="${JAVA_HOME}/bin:${HOME}/bin:/usr/local/bin:/usr/bin:/usr/X11R6/bin:/bin:/usr/games:/opt/gnome/bin:/opt/kde3/bin:$PATH:$NODE_HOME/bin"
export JAVA_ROOT="${JAVA_HOME}"
export JRE_HOME="${JAVA_HOME}/jre"
export CATALINA_HOME="${HOME}/apache-tomcat-8.5.13"
export CATALINA_BASE="${HOME}/apache-tomcat-8.5.13"
export CLASSPATH=".:${JAVA_HOME}/lib/jt.jar:${JAVA_HOME}/lib/tools.jar:${JAVA_HOME}/jre/lib:${CATALINA_HOME}/webapps/common/WEB-INF/classes"

#setenv LC_ALL zh_CN.GBK

alias cfg="cd ${CATALINA_HOME}/cfg"
alias webapps="cd ${CATALINA_HOME}/webapps"
alias class="cd ${CATALINA_HOME}/webapps/ROOT/WEB-INF/classes"
alias tlog="cd ${CATALINA_HOME}/logs"
alias logs="cd ${CATALINA_HOME}/logs"
alias local="cd ${CATALINA_HOME}/local"
alias catlog="tail -f ${CATALINA_HOME}/logs/catalina.out"
alias version="java -version"
alias start="${CATALINA_HOME}/cfg/iread.sh start"
alias stop="${CATALINA_HOME}/cfg/iread.sh stop"
alias restart="${CATALINA_HOME}/cfg/iread.sh restart"
alias status="${CATALINA_HOME}/cfg/iread.sh status"
alias webapps="cd ${CATALINA_HOME}/webapps/ROOT"

 

 附件2:

封装的系统命令(iread.sh)

#!/bin/sh

INST_DIR="${CATALINA_HOME}"
IREAD_USER=`whoami`
SCRIPT_NAME=`basename $0`
LOG_FILE="${CATALINA_HOME}/logs/ireadtrace.log"
SERVICE=IREAD

killadapter()
{
# list pids of the iread processes
pids=`ps -fu ${IREAD_USER} | grep "java" | grep -v "grep" | awk \'{print $2}\'`
if [ -n "$pids" ]; then
kill -9 $pids
else
echo "iRead process does not exist!"
echo "iRead process does not exist!" >>${LOG_FILE}
fi
}

start()
{
# list pids of the iread processes
pids=`ps -fu ${IREAD_USER} | grep "java" | grep -v "grep" | awk \'{print $2}\'`
if [ -n "$pids" ]; then
echo "$SERVICE already running."
echo "$SERVICE already running." >>${LOG_FILE}
exit 0
fi
if [ -e ${LOG_FILE} ]; then
mv ${LOG_FILE} ${LOG_FILE}.bak
fi
echo "starting $SERVICE at: `date`.."
echo "starting $SERVICE at: `date`.." >>${LOG_FILE}
cd ${CATALINA_HOME}
bin/startup.sh 2>&1 >>${LOG_FILE} &
}

stop()
{
echo "stopping $SERVICE at: `date`"
echo "stopping $SERVICE at: `date`">>${LOG_FILE}
killadapter
}


status()
{
# list pids of the iread processes
pids=`ps -f U ${IREAD_USER} | grep "java" | grep -v "grep"`

if [ -n "$pids" ]; then
echo "$SERVICE is running"
echo "$SERVICE is running" >>${LOG_FILE}
exit 0
fi
echo $SERVICE is stopped
exit 3
}


restart()
{
stop
sleep 5
start
}


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

exit 0

 

以上是关于Linux命令封装手记的主要内容,如果未能解决你的问题,请参考以下文章

linux随手记

Linux 随手记

linux命令快速手记 — 让手指跟上思考的速度

学习手记-基于iTOP4412开发板Linux最小系统烧录

学习手记-Linux文件IO

学习手记-Linux用户管理