编译
#!/usr/bin/env bash
SOURCE_FILE_NAME=main
TARGET_FILE_NAME=reskd
rm -fr ${TARGET_FILE_NAME}*
build(){
echo $GOOS $GOARCH
tname=${TARGET_FILE_NAME}_${GOOS}_${GOARCH}${EXT}
env GOOS=$GOOS GOARCH=$GOARCH \
go build -o ${tname} \
-v ${SOURCE_FILE_NAME}.go
chmod +x ${tname}
mv ${tname} ${TARGET_FILE_NAME}${EXT}
if [ ${GOOS} == "windows" ];then
zip ${tname}.zip ${TARGET_FILE_NAME}${EXT} config.ini ../public/
else
tar --exclude=*.gz --exclude=*.zip --exclude=*.git -czvf ${tname}.tar.gz ${TARGET_FILE_NAME}${EXT} config.ini *.sh ../public/ -C ./ .
fi
mv ${TARGET_FILE_NAME}${EXT} ${tname}
}
CGO_ENABLED=0
#mac os 64
GOOS=darwin
GOARCH=amd64
build
#linux 64
GOOS=linux
GOARCH=amd64
build
#windows
#64
GOOS=windows
GOARCH=amd64
build
GOARCH=386
build
重定向标准输出:> file.log 2>&1
- > 创建和重写文件,>>创建和追加文件
- 使用文件描述符: 空/dev/null,输入0,输出1,错误2
- > file.log 或者 1>file.log 标准输出写入到file中
重定向标准和错误输出: &来合并输出
- 2>&1将错误重定向并合并标准输出上
- > file.log 2>&1 标准输出和错误信息写入到文件
nohup ./cmd args ... > file.log 2>&1 &
进行终止: kill+信号名称或编号+PID或PGID
- 通过进程名称查询出要终止的进程PID
- 系统启动时将PID写入一个文件,停止时读取文件
通过进程名称查询进程ID: ps -ef|grep 进程名称
- ps -ef 获取进程列表
- grep 进程名称, 过滤出合适的进程
- grep -v 'grep' 过滤掉grep进程自身
ps -ef|grep 进程名称|grep -v 'grep'
#例如查询到进程列表如下:shell 解析出pid
$ pi=`ps -ef|grep reskd |grep -v 'grep'`
$ pis = (${pi// / })
$ pid = ${pis[1]}
$ echo $pid
通过awk从进程列表中过滤进程ID: awk '{print $2}'
kill命令中的信号量
- kill -l 查询系统支持的信号量
- 3个重要的信号量:3,9,15
- 3 退出
- 9 强制终止
- 15 终止
#!/usr/bin/env bash
RUN_FILE_NAME=reskd_darwin_amd64
run(){
echo running... ./${RUN_FILE_NAME} $2
./${RUN_FILE_NAME} $2
}
start(){
echo "starting $2"
nohup ./${RUN_FILE_NAME} $2 > ../resk.log 2>&1 &
echo "started ${RUN_FILE_NAME} $2"
}
stop(){
PIDS=`ps -ef|grep "${RUN_FILE_NAME}"|grep -v 'grep'|awk '{print $2}'`
echo "$PIDS"
for pid in $PIDS
do
echo kill $pid
kill -15 $pid
done
}
restart(){
stop && start $@
}
rerun(){
stop && run $@
}
#./run.sh run 1 3
action="$1"
if [ "${action}" == "" ]
then
action="run"
fi
case "${action}" in
start)
start "$@";;
stop)
stop "$@";;
restart)
reatart "$@";;
run)
run "$@";;
rerun)
rerun "$@";;
*)
echo "Usage: $0 {start|stop|restart|rerun|run} {dev|test|prod|...}";
echo " eg: ./${RUN_FILE_NAME} run dev";
exit 1;
esac
exit 0