/images/avatar.png

Schaepher's Blog

init 脚本模板

其中,PROG变量为所要运行的可执行程序的名称, PROG_PATH为可执行文件所在的目录,PROG_ARGS为执行程序的各个参数。

#!/bin/bash
### BEGIN INIT INFO
#
# Provides:	 location_server
# Required-Start:	$local_fs  $remote_fs
# Required-Stop:	$local_fs  $remote_fs
# Default-Start: 	2 3 4 5
# Default-Stop: 	0 1 6
# Short-Description:	initscript
# Description: 	This file should be used to construct scripts to be placed in /etc/init.d.
#
### END INIT INFO
 
## Fill in name of program here.
PROG="location_server"
PROG_PATH="/opt/location_server" ## Not need, but sometimes helpful (if $PROG resides in /opt for example).
PROG_ARGS="" 
PID_PATH="/var/run/"
 
start() {
    if [ -e "$PID_PATH/$PROG.pid" ]; then
        ## Program is running, exit with error.
        echo "Error! $PROG is currently running!" 1>&2
        exit 1
    else
        ## Change from /dev/null to something like /var/log/$PROG if you want to save output.
        $PROG_PATH/$PROG $PROG_ARGS 2>&1 >/var/log/$PROG &
	$pid=`ps ax | grep -i 'location_server' | sed 's/^\([0-9]\{1,\}\).*/\1/g' | head -n 1`
 
        echo "$PROG started"
        echo $pid > "$PID_PATH/$PROG.pid"
    fi
}
 
stop() {
    echo "begin stop"
    if [ -e "$PID_PATH/$PROG.pid" ]; then
        ## Program is running, so stop it
	pid=`ps ax | grep -i 'location_server' | sed 's/^\([0-9]\{1,\}\).*/\1/g' | head -n 1`
	kill $pid
        
        rm -f  "$PID_PATH/$PROG.pid"
        echo "$PROG stopped"
    else
        ## Program is not running, exit with error.
        echo "Error! $PROG not started!" 1>&2
        exit 1
    fi
}
 
## Check to see if we are running as root first.
## Found at http://www.cyberciti.biz/tips/shell-root-user-check-script.html
if [ "$(id -u)" != "0" ]; then
    echo "This script must be run as root" 1>&2
    exit 1
fi
 
case "$1" in
    start)
        start
        exit 0
    ;;
    stop)
        stop
        exit 0
    ;;
    reload|restart|force-reload)
        stop
        start
        exit 0
    ;;
    **)
        echo "Usage: $0 {start|stop|reload}" 1>&2
        exit 1
    ;;
esac

transfer-script-and-run

filename=""
base64_script=$(base64 /usr/local/src/${filename})

cmd="echo '---run---'; mkdir -p /usr/local/src; echo '${base64_script}' | base64 -di > /usr/local/src/${filename}; bash /usr/local/src/${filename}; echo '---end---'"

bash -c "${cmd}"

Bash 脚本模板

这是一个较为通用的脚本模板。

#!/bin/bash
##############################################################
# Filename    : template.sh
# Author      : xxx(xxx@outlook.com)
# Date        : xxxx-xx-xx
# Function    :
#   Bash Template
# Changes     :
# - 20191217 by xxx
#   Create Template
##############################################################

readonly _filename=$(basename $0)

function usage()
{
	cat <<EOF
Description:
  Echo var0 and var1
Usage:
  template.sh --var0=<value> [--var1=<value>]
Options:
  -h --help       Show this help.
  --var0=<value>  Assign <value> to var0
  --var1=<value>  Assign <value> to var1
EOF
    exit 0
}

if [[ "$#" -eq 0 ]]; then usage; fi

# Usage: log <log-level> <log-text>...
# log-level: DEBUG INFO ERROR
function log() { local prefix="[$(date +%Y-%m-%d\ %H:%M:%S)]: "; echo "${prefix} $@" >> /tmp/${_filename}.log; }

# Exit if value is empty
# Usage: required "--var1" "${var1}"
function required() { if [[ ! -n "${2}" ]]; then echo "${1} is required!"; exit 1; fi }

# Usage: output "${key}" "${value}"
# Double Quote is required to preserve new line character.
function output() { local k=$1; shift; local v=$(echo -n "$@" | base64 -w 0); [[ "$debug" -eq 1 ]] && v="$@"; echo ${k}=${v}; }

set -e
_args=$(getopt --option h --long "help",debug,var0:,var1: -- "$@")
eval set -- "${_args}"

# common the next line to avoid exit immediately if a simple command exits with a non-zero status
set +e

while true
do
    case "$1" in
        -h|--help)
            usage
            break
            ;;
        --var0)
            var0=$2
            shift 2
            ;;
        --var1)
            var1=$2
            shift 2
            ;;
        --)
            shift
            break
            ;;
    esac
done

restParams="$@"

required "--var0" "${var0}"

function main()
{
    output var0 "${var0}"
    output var1 "${var1}"
}

log 'INFO' 'script start'
main
log 'INFO' 'script end'

执行脚本

$commands = [
    ['echo', '"-----script-start-----"'],
    ['mkdir', '-p', '/usr/local/src/scripts'],
    ['cd', '/usr/local/src/scripts'],
    [sprintf('echo "%s" | base64 -di > %s.env', base64_encode(implode("\n", $envArr)), $scriptName)],
    [sprintf('echo "%s" | base64 -di > %s', base64_encode($scriptContent), $scriptName)],
    ['chmod', '+x', $scriptName],
    array_merge(['bash', $scriptName], array_map(CommandEscapeTool::class, 'escape'], $params), ['2>&1']),
    ['echo', 'script_exit_code=$?'],
    ['echo', '-----script-end-----'],
];

$commandStr = sprintf('echo "%s" | base64 -di | bash', base64_encode($commandStr));

java

mybatis plus 查询构造器

https://mybatis.plus/guide/wrapper.html#alleq

spring boot 配置文件多环境

https://blog.csdn.net/davis2015csdn/article/details/75220046

响应体 Header

https://www.baeldung.com/spring-rest-http-headers

注解全介绍

https://cloud.tencent.com/developer/article/1507070

分页 Header 构造

https://www.baeldung.com/rest-api-pagination-in-spring
https://www.javadevjournal.com/spring/rest-pagination-in-spring/

对 Mybatis 的吐槽

https://zhuanlan.zhihu.com/p/45044649

决策:

  • 包名中有多个单词组成的,要用下划线连接吗?
    全部用小写,并且不包含下划线(参考谷歌)。例如 helloworld