停止命令代码解读
PID=$(ps -ef | grep jenkins | grep -v grep | awk '{ print $2 }')
if [ -z "$PID" ]
then
echo jenkins is already stopped
else
echo kill $PID
kill $PID
fi
ps -ef | grep jenkins | grep -v grep | awk ‘{ print $2 }’
ps -ef | grep jenkins:查看所有jenkins进程
ps -ef | grep jenkins | grep -v grep
grep -v 代表不包括;上述语句是查找除了grep命令操作的jenkins外的所有信息,如下图所示;
awk ‘{ print $2 }’ :将查询结果的第二个字段输出,即PID
if [ -z "$PID" ]
[ -z STRING ] “STRING” 的长度为零则为真。 字符串为空即NULL时为真。
#####################
if [ -z "$PID" ]
then
echo jenkins is already stopped
else
echo kill $PID
kill $PID
fi
#####################
如果PID的长度为0,输出 jenkins is already stopped,
否则执行kill $PID,停止进程。
grep -n 打印行号
grep -E = egrep 匹配正则表达式
grep -i 忽略大小写
awk命令知识点较多,单独进行整理
参考资料:
https://www.cnblogs.com/xudong-bupt/p/3721210.html
https://www.cnblogs.com/qianjinyan/p/9244746.html