Skip to content

Commit 645534b

Browse files
committed
update
1 parent bde246d commit 645534b

22 files changed

+543
-81
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<p align="center">
2-
<a href="https://spring.io/projects/spring-boot" target="_blank" rel="noopener noreferrer">
2+
<a href="https://dunwu.github.io/linux-tutorial/" target="_blank" rel="noopener noreferrer">
33
<img src="http://dunwu.test.upcdn.net/common/logo/linux.svg" alt="logo" width="100px">
44
</a>
55
</p>

assets/docker.xmind

203 KB
Binary file not shown.

assets/linux.xmind

789 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM python:3.6-alpine
2+
ADD . /code
3+
WORKDIR /code
4+
RUN pip install redis flask
5+
CMD ["python", "app.py"]
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from flask import Flask
2+
from redis import Redis
3+
4+
app = Flask(__name__)
5+
redis = Redis(host='redis', port=6379)
6+
7+
@app.route('/')
8+
def hello():
9+
count = redis.incr('hits')
10+
return 'Hello World! 该页面已被访问 {} 次。\n'.format(count)
11+
12+
if __name__ == "__main__":
13+
app.run(host="0.0.0.0", debug=True)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: '3'
2+
services:
3+
4+
web:
5+
build: .
6+
ports:
7+
- "5000:5000"
8+
9+
redis:
10+
image: "redis:alpine"
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
3+
set -x
4+
docker-compose up

codes/linux/lib/env.sh

+50
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/usr/bin/env bash
22

3+
# ------------------------------------------------------------------------------
4+
# 常用变量库
5+
# @author Zhang Peng
6+
# ------------------------------------------------------------------------------
7+
38
# ------------------------------------------------------------------------------ 颜色状态
49

510
# Regular Color
@@ -51,3 +56,48 @@ YES=0
5156
NO=1
5257
SUCCEED=0
5358
FAILED=1
59+
60+
# 显示打印日志的时间
61+
DATE=`date "+%Y-%m-%d %H:%M:%S"`
62+
# 那个用户在操作
63+
USER=$(whoami)
64+
65+
# ------------------------------------------------------------------------------ log
66+
67+
logInfo() {
68+
#($0脚本本身,$@将参数作为整体传输调用)
69+
echo "[${DATE}] [${USER}] [INFO] [$0] [$@] execute succeed." >> /var/log/shell.log
70+
}
71+
72+
logWarn() {
73+
#($0脚本本身,$@将参数作为整体传输调用)
74+
echo "[${DATE}] [${USER}] [WARN] [$0] [$@] execute succeed." >> /var/log/shell.log
75+
}
76+
77+
logError() {
78+
#($0脚本本身,$@将参数作为整体传输调用)
79+
echo "[${DATE}] [${USER}] [ERROR] [$0] [$@] execute failed." >> /var/log/shell.log
80+
}
81+
82+
printInfo() {
83+
echo -e "${C_B_GREEN}[INFO] $@${C_RESET}"
84+
}
85+
86+
printWarn() {
87+
echo -e "${C_B_YELLOW}[WARN] $@${C_RESET}"
88+
}
89+
90+
printError() {
91+
echo -e "${C_B_RED}[ERROR] $@${C_RESET}"
92+
}
93+
94+
callAndLog () {
95+
$*
96+
if [[ $? -eq ${SUCCEED} ]]; then
97+
logInfo "$@ succeed"
98+
echo -e "${C_B_GREEN}[INFO] [$0] [$@] execute succeed.${C_RESET}"
99+
else
100+
logError "$@ failed"
101+
echo -e "${C_B_RED}[ERROR] [$0] [$@] execute failed.${C_RESET}"
102+
fi
103+
}

codes/linux/lib/git.sh

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/usr/bin/env bash
22

3+
# ------------------------------------------------------------------------------
4+
# Git 基本操作脚本
5+
# @author Zhang Peng
6+
# ------------------------------------------------------------------------------
7+
38
# 装载其它库
49
ROOT=`dirname ${BASH_SOURCE[0]}`
510
source ${ROOT}/env.sh
@@ -33,7 +38,7 @@ checkGit() {
3338
return ${NO}
3439
fi
3540

36-
printf "${C_B_B_YELLOW}${source} is invalid dir.${C_RESET}\n"
41+
printf "${C_B_YELLOW}${source} is invalid dir.${C_RESET}\n"
3742
return ${NO}
3843
}
3944

@@ -49,18 +54,18 @@ cloneOrPullGit() {
4954
local root=$5
5055

5156
if [[ ! ${repository} ]] || [[ ! ${group} ]] || [[ ! ${project} ]] || [[ ! ${branch} ]] || [[ ! ${root} ]]; then
52-
printf "${C_B_YELLOW}>>>> Please input root, group, project, branch.${C_RESET}\n"
57+
printf "${C_B_YELLOW}Please input root, group, project, branch.${C_RESET}\n"
5358
return ${FAILED}
5459
fi
5560

5661
if [[ ! -d "${root}" ]]; then
57-
printf "${C_B_YELLOW}>>>> ${root} is not directory.${C_RESET}\n"
62+
printf "${C_B_YELLOW}${root} is not directory.${C_RESET}\n"
5863
return ${FAILED}
5964
fi
6065

6166
local source=${root}/${group}/${project}
62-
printf "${C_B_CYAN}>>>> project directory is ${source}.${C_RESET}\n"
63-
printf "${C_B_CYAN}>>>> git url is ${repository}:${group}/${project}.git.${C_RESET}\n"
67+
printf "${C_B_MAGENTA}project directory is ${source}.${C_RESET}\n"
68+
printf "${C_B_MAGENTA}git url is ${repository}:${group}/${project}.git.${C_RESET}\n"
6469
mkdir -p ${root}/${group}
6570

6671
checkGit ${source}
@@ -73,21 +78,21 @@ cloneOrPullGit() {
7378
printf "${C_B_RED}<<<< git checkout ${branch} failed.${C_RESET}\n"
7479
return ${FAILED}
7580
fi
76-
printf "${C_B_GREEN}>>>> git checkout ${branch} succeed.${C_RESET}\n"
81+
printf "${C_B_GREEN}git checkout ${branch} succeed.${C_RESET}\n"
7782

7883
git reset --hard
7984
if [[ "${SUCCEED}" != "$?" ]]; then
8085
printf "${C_B_RED}<<<< git reset --hard failed.${C_RESET}\n"
8186
return ${FAILED}
8287
fi
83-
printf "${C_B_GREEN}>>>> git reset --hard succeed.${C_RESET}\n"
88+
printf "${C_B_GREEN}git reset --hard succeed.${C_RESET}\n"
8489

8590
git pull
8691
if [[ "${SUCCEED}" != "$?" ]]; then
8792
printf "${C_B_RED}<<<< git pull failed.${C_RESET}\n"
8893
return ${FAILED}
8994
fi
90-
printf "${C_B_GREEN}>>>> git pull succeed.${C_RESET}\n"
95+
printf "${C_B_GREEN}git pull succeed.${C_RESET}\n"
9196
else
9297
# 如果 ${source} 不是 git 项目,执行 clone 操作
9398

@@ -96,7 +101,7 @@ cloneOrPullGit() {
96101
printf "${C_B_RED}<<<< git clone ${project} failed.${C_RESET}\n"
97102
return ${FAILED}
98103
fi
99-
printf "${C_B_GREEN}>>>> git clone ${project} succeed.${C_RESET}\n"
104+
printf "${C_B_GREEN}git clone ${project} succeed.${C_RESET}\n"
100105

101106
cd ${source} || return ${FAILED}
102107

@@ -105,8 +110,9 @@ cloneOrPullGit() {
105110
printf "${C_B_RED}<<<< git checkout ${branch} failed.${C_RESET}\n"
106111
return ${FAILED}
107112
fi
108-
printf "${C_B_GREEN}>>>> git checkout ${branch} succeed.${C_RESET}\n"
113+
printf "${C_B_GREEN}git checkout ${branch} succeed.${C_RESET}\n"
109114
fi
110115

116+
printf "${C_B_GREEN}Clone or pull git project [$2/$3:$4] succeed.${C_RESET}\n"
111117
return ${SUCCEED}
112118
}

codes/linux/lib/java.sh

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#!/usr/bin/env bash
2+
3+
# ------------------------------------------------------------------------------
4+
# Java 应用运维脚本
5+
# @author Zhang Peng
6+
# ------------------------------------------------------------------------------
7+
8+
# ------------------------------------------------------------------------------ env preparation
9+
# load libs
10+
CURRENT_PATH=`dirname ${BASH_SOURCE[0]}`
11+
source ${CURRENT_PATH}/env.sh
12+
13+
# ------------------------------------------------------------------------------ functions
14+
15+
stopServer() {
16+
if [[ ! $1 ]]; then
17+
printError "please input java app name"
18+
return ${FAILED}
19+
fi
20+
21+
local javaAppName=$1
22+
local pid=`jps | grep ${javaAppName} | awk '{print $1}'`
23+
if [[ -n "${pid}" ]]; then
24+
kill -9 ${pid}
25+
if [[ $? -eq ${SUCCEED} ]]; then
26+
printInfo "stop ${javaAppName} succeed"
27+
return ${SUCCEED}
28+
else
29+
printError "stop ${javaAppName} failed"
30+
return ${FAILED}
31+
fi
32+
else
33+
printWarn "${javaAppName} is not running"
34+
return ${SUCCEED}
35+
fi
36+
}
37+
38+
startServer() {
39+
if [[ ! $1 ]]; then
40+
printError "please input java app name"
41+
return ${FAILED}
42+
fi
43+
44+
# >>>> 1. check java app is started or not
45+
# >>>> 1.1. exit script if the app is started
46+
local javaAppName=$1
47+
local pid=`jps | grep ${javaAppName} | awk '{print $1}'`
48+
if [[ -n "${pid}" ]]; then
49+
printInfo "${javaAppName} is started, PID: ${pid}"
50+
return ${SUCCEED}
51+
fi
52+
53+
# >>>> 2. package options
54+
# GC OPTS
55+
local javaOptions="-server -Xms1g -Xmx2g -Xss256k"
56+
javaOptions="${javaOptions} -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:NewRatio=4"
57+
58+
# GC LOG OPTS
59+
javaOptions="${javaOptions} -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps"
60+
javaOptions="${javaOptions} -verbose:gc -Xloggc:${LOG_PATH}/${javaAppName}.gc.log"
61+
javaOptions="${javaOptions} -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M"
62+
63+
# Heap Dump OPTS
64+
javaOptions="${javaOptions} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError"
65+
javaOptions="${javaOptions} -XX:HeapDumpPath=${LOG_PATH}/${javaAppName}.heapdump.hprof"
66+
67+
# APP OPTS
68+
javaOptions="${javaOptions} -Dsun.net.inetaddr.ttl=60 -Djava.net.preferIPv4Stack=true -Dfile.encoding=UTF-8"
69+
if [[ ${PROFILE} ]]; then
70+
javaOptions="${javaOptions} -Dspring.profiles.active=${PROFILE}"
71+
fi
72+
73+
# DEBUG OPTS
74+
if [[ "${DEBUG}" == "on" ]]; then
75+
# JMX OPTS
76+
local ip=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d '/')
77+
local jmxPort=$(expr 10000 + ${PORT})
78+
javaOptions="${javaOptions} -Dcom.sun.management.jmxremote=true"
79+
javaOptions="${javaOptions} -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"
80+
javaOptions="${javaOptions} -Djava.rmi.server.hostname=${ip} -Dcom.sun.management.jmxremote.port=${jmxPort}"
81+
82+
# Remote Debug
83+
local debugPort=$(expr 20000 + ${PORT})
84+
javaOptions="${javaOptions} -Xdebug -Xnoagent -Djava.compiler=NONE"
85+
javaOptions="${javaOptions} -Xrunjdwp:transport=dt_socket,address=${debugPort},server=y,suspend=n"
86+
fi
87+
88+
# CLASSPATH
89+
local appOptions="-classpath ${ROOT_PATH}/lib/* -Dlogging.config=file:${ROOT_PATH}/config/logback.dev.xml"
90+
appOptions="${appOptions} --spring.config.location=classpath:/,classpath:/config/,file:${ROOT_PATH},file:${ROOT_PATH}/config/"
91+
if [[ ${PORT} ]]; then
92+
appOptions="${appOptions} --server.port=${PORT}"
93+
fi
94+
95+
# >>>> 3. create log dir and console log file
96+
mkdir -p ${LOG_PATH}
97+
if [[ ! -f ${CONSOLE_LOG} ]]; then
98+
touch ${CONSOLE_LOG}
99+
fi
100+
101+
# >>>> 4. start java app
102+
printInfo "starting ${javaAppName}, execute cli: "
103+
printInfo "nohup java ${javaOptions} -jar ${ROOT_PATH}/${javaAppName}.jar ${appOptions} >> ${CONSOLE_LOG} 2>&1 &"
104+
nohup java ${javaOptions} -jar ${ROOT_PATH}/${javaAppName}.jar ${appOptions} >> ${CONSOLE_LOG} 2>&1 &
105+
106+
# >>>> 5. check java app is started or not
107+
local pid=`jps | grep ${javaAppName} | awk '{print $1}'`
108+
if [[ -n "${pid}" ]]; then
109+
printInfo "start ${javaAppName} succeed, PID: ${pid}"
110+
return ${SUCCEED}
111+
else
112+
printError "start ${javaAppName} failed"
113+
return ${FAILED}
114+
fi
115+
}
116+
117+
# ------------------------------------------------------------------------------ main
118+
export LANG="zh_CN.UTF-8"
119+
ROOT_PATH=$(cd ${CURRENT_PATH}/..; pwd)
120+
121+
APP_NAME=java-app
122+
LOG_PATH=/var/log/myapp
123+
CONSOLE_LOG=${LOG_PATH}/${APP_NAME}.console.log
124+
125+
PORT=8888
126+
PROFILE=dev
127+
DEBUG=off
128+
129+
startServer ${APP_NAME}
130+
#stopServer ${APP_NAME}
131+
if [[ $? -eq ${SUCCEED} ]]; then
132+
exit ${SUCCEED}
133+
else
134+
exit ${FAILED}
135+
fi

codes/linux/lib/maven.sh

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env bash
2+
3+
# ------------------------------------------------------------------------------
4+
# maven 项目操作脚本
5+
# @author Zhang Peng
6+
# ------------------------------------------------------------------------------
7+
8+
# 装载其它库
9+
ROOT=`dirname ${BASH_SOURCE[0]}`
10+
source ${ROOT}/env.sh
11+
12+
mavenBuild() {
13+
local source=$1
14+
mavenCheck $1
15+
if [[ "${SUCCEED}" != "$?" ]]; then
16+
return ${FAILED}
17+
fi
18+
19+
if [[ -d "${source}" ]]; then
20+
cd ${source}
21+
if [[ -f "${source}/settings.xml" ]]; then
22+
callAndLog "mvn clean install -B -U -s ${source}/settings.xml -Dmaven.test.skip=true"
23+
else
24+
callAndLog "mvn clean install -DskipTests=true -B -U"
25+
fi
26+
cd -
27+
return ${SUCCEED}
28+
else
29+
printf "${C_B_RED}please input valid maven project path.${C_RESET}\n"
30+
return ${FAILED}
31+
fi
32+
}
33+
34+
mavenCheck() {
35+
local source=$1
36+
if [[ -d "${source}" ]]; then
37+
cd ${source}
38+
if [[ -f "${source}/pom.xml" ]]; then
39+
return ${YES}
40+
else
41+
printf "${C_B_RED}pom.xml is not exists.${C_RESET}\n"
42+
return ${NO}
43+
fi
44+
cd -
45+
return ${YES}
46+
else
47+
printf "${C_B_RED}please input valid maven project path.${C_RESET}\n"
48+
return ${NO}
49+
fi
50+
}
51+
52+
##################################### MAIN #####################################
53+
printf "\n${C_B_GREEN}>>>> maven build begin.${C_RESET}\n\n"
54+
55+
printf "${C_B_MAGENTA}Current path is ${ROOT}.${C_RESET}\n"
56+
57+
mavenBuild ${ROOT}/..
58+
r1=$?
59+
60+
if [[ "${r1}" == "${SUCCEED}" ]]; then
61+
printf "\n${C_B_GREEN}<<<< maven build succeed.${C_RESET}\n\n"
62+
exit ${SUCCEED}
63+
else
64+
printf "\n${C_B_RED}<<<< maven build failed.${C_RESET}\n\n"
65+
exit ${FAILED}
66+
fi

0 commit comments

Comments
 (0)