这里写目录标题
一级目录
root@localhost:~$ date +"%Y%m%d%H%M%S"
20201216102243
nohup启动java jar包
#!/bin/bash
#JAVA_OPTS="-server -Xms2048M -Xmx2048M -Xmn512m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -Xverify:none -XX:+DisableExplicitGC"
# debug方式
nohup /usr/local/jdk1.8.0_171/bin/java -Xdebug -Xrunjdwp:transport=dt_socket,address=5005,server=y,suspend=n -jar Boot_V1.0.0.jar &
Linux使用systemctl设置程序开机自启动
linux systemctl 指令 —— 阮一峰
systemctl设置程序开机自启动
systemctl设置程序开机自启动
添加http-test.service 文件:
vim /usr/lib/systemd/system/http-test.service
[Unit]
Description= http-test
Wants=http-test.service
After=syslog.target network.target
[Service]
Type=forking
ExecStart=/home/service-no-delete/run.sh #
ExecStop=/home/service-no-delete/stop.sh #
Restart=always # kill掉会自动重启,systemctl stop http-test.service不会自动重启
RestartSec=5s
[Install]
WantedBy=multi-user.target
修改完odl.service文件后,更新daemon启动服务:
systemctl daemon-reload
systemctl enable http-test.service
systemctl start http-test.service ( service http-test start )
systemctl stop http-test.service
systemctl status http-test.service 日志保存在 /var/log/messages
systemctl list-unit-files|grep http-test.service
systemctl show http-test.service
vim run.sh
chmod +x run.sh
#!/bin/bash # bin/bash要有
cd /home/service-no-delete # 先cd到jar包绝对路径
nohup java -jar http-0.0.1-SNAPSHOT.jar -Dserver.port=8997 &
vim stop.sh
chmod +x stop.sh
#!/bin/bash
cd /home/service-no-delete
JAR_NAME=http-0.0.1-SNAPSHOT.jar
PID=`ps aux | grep $JAR_NAME | awk '{print $2}'`
if [ -n "$PID" ]; then
ps -ef|grep -v grep|grep $JAR_NAME | awk '{print "kill -9 "$2}'| sh
echo "kill process success :):):)"
fi
ps并kill某个进程
PID=`ps aux | grep test_V1.0.0.jar | awk '{print $2}'`
if [ -n "$PID" ]; then
sleep 1
ps -ef|grep -v grep|grep test_V1.0.0.jar | awk '{print "kill -9 "$2}'| sh
fi
current=`date +"%Y%m%d%H%M%S"`
echo $current
if [ -d "/root/logdir" ]; then
mv logdir logdir${current}
fi
if [ -f "/root/nohup.out" ]; then
mv nohup.out nohup${current}.out
fi
for 循环执行curl命令
!/bin/bash
host_ips=(151.15.166.211 151.15.166.212 151.15.166.213 )
for(( i=0; i<${#host_ips[@]}; i++))
do
echo "porcessing ${i}th, hostip=${host_ips[i]}"
urlStr="http://151.15.127.109:8181/restconf/httt-rpc:create-xxx"
dataStr="{\"input\":{\"host-ip\": \"${host_ips[i]}\",\"host-type\":\"compute\"}}"
curl --user admin:admin -l -H "Content-type: application/json" -X POST -d "${dataStr}" ${urlStr}
echo -e "\n"
done
tail
tail -20000 log.out >ff.txt
grep
查找当前目录下所有文件中的文本:grep “text” * -nR
查找当前目录下所有文件中的文本:grep “text” fileaaa.txt > filebbb.txt
awk
sed
怎么截取巨大日志里某一天的log(截取大文件中两个行数之间的数据)
截取a的第八行到b(末尾)
sed -n ‘8p’ a >> b
如果要覆盖则为 a > b
比如从第3行到第10行
sed -n ‘3,10p’ myfile > newfile
find
find -name *.* | xargs grep “edit-config” | wc -l
du -sh
du -sh # 查询当前目录总大小可以使用du -sh,其中s代表统计汇总的意思
du -h --max-depth=1 * # 可以查看当前目录下各文件、文件夹的大小,子目录大小等等,这个比较实用,max-depth=1,表示几级子目录,如果不需要子目录,=0,即可。
split
split提供两种方式对文件进行切割:
根据行数切割,通过-l参数指定需要切割的行数
根据大小切割,通过-b参数指定需要切割的大小
通过/dev/urandom设备产生随机密码
操作过程:
1.通过/dev/urandom工具生成随机密码
[root@testvm01 ~]# cat /dev/urandom | tr -dc a-zA-Z0-9#@ | head -c 13;echo
OjX3Nst381U@R
[root@testvm01 ~]# cat /dev/urandom | tr -dc a-zA-Z0-9#@ | head -c 13;echo
SlfnYBxUiOERS
[root@testvm01 ~]# cat /dev/urandom | tr -dc '0-9' | head -c 10 | sed 's/./=/g';echo
============
备注:通过cat命令查看/dev/urandom就能够获取随机数,然后,通过tr命令进行字符的转换.
tr命令说明:
tr -dc a-zA-Z0-9#@ #a-z和A-Z之间没有其他的符合.0-9后面的是自己指定的特殊符号
备注:tr命令是替换或者删除字符的命令.-d的意思是删除后面集合中的字符,-c的意思是取反,就是说,除了后面的字符集合其他的都删除掉.我们可以通过后面的字符的集合来指定自己的密码复杂度.
head命令说明:
head -c 13
备注:head命令的作用就是取多少字符,-c指的是取多少字节,13可以自定义,这样就定义了密码的长度.
<< EOF
在shell编程中,遇大段文本或代码时,经常会用到eof。本文就介绍下其用法,供初学的朋友参考。 在shell脚本中,通常将EOF与 << 结合使用,表示后续的输入作为子命令或子Shell的输入,直到遇到EOF为止,再返回到主Shell。
EOF只是一个分界符,当然也可以用abcde替换。
当shell遇到<<时,它知道下一个词是一个分界符。在该分界符以后的内容都被当作输入,直到shell又看到该分界符(位于单独的一行)。
此分界符可以是所定义的任何字符串,其实,不一定要用EOF,只要是“内容段”中没有出现的字符串,都可以用来替代EOF,完全可以换成abcde之类的字符串,只是一个起始和结束的标志罢了。
nl (Number of Lines)
nl (Number of Lines) 将指定的文件添加行号标注后写到标准输出。如果不指定文件或指定文件为"-" ,程序将从标准输入读取数据。
批量下载git代码
#!/bin/bash
function getListByKey() {
json=$1
key=$2
echo $json | grep -Po '"$key":(.+?),' | grep -Po '\d+'
}
function groupProjects() {
groupList=''
groupList=$(curl -H "PRIVATE-TOKEN: $privateToken" https://gitlabxxx/api/v4/groups)
#echo $groupList
idList=($(echo $groupList | grep -Po '"id":(.+?),' | grep -Po '\d+'))
nameList=($(echo $groupList | grep -Po '"name":(.+?),' | awk -F '"' '{print $4}'))
#遍历 group id list
echo "您有 ${#idList[@]} 个分组 "
for ((i = 0; i < ${#idList[@]}; i++)); do
echo "目录 ${nameList[i]}"
mkdir ${nameList[i]}
cd ${nameList[i]}
echo "traverse group ${idList[i]}"
projectsList=$(curl -H "PRIVATE-TOKEN: $privateToken" https://gitlabxxx/api/v4/groups/${idList[i]}/projects)
#echo $projectsList
sshList=($(echo $projectsList | grep -Po '(ssh_url_to_repo":")[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]' | awk -F '"' '{print $3}'))
for ((j = 0; j < ${#sshList[@]}; j++)); do
echo "准备克隆第 $(($i + 1)) 分组下的第 $(($j + 1)) 个项目${sshList[j]}"
echo "git clone ${sshList[j]}"
git clone ${sshList[j]}
echo ""
done
cd ..
done
}
function allProjects() {
allList=''
allList=$(curl -H "PRIVATE-TOKEN: $privateToken" https://gitlabxxx/api/v4/projects)
#echo $groupList
sshList=($(echo $allList | grep -Po '(ssh)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]'))
for ((j = 0; j < ${#sshList[@]}; j++)); do
echo "准备克隆第$(($j + 1)) 个项目${sshList[j]}"
git clone ${sshList[j]}
done
}
#JNdfgrTMcqertKTzghyytmUxRaWwsx
echo -e "请输入你的私密令牌:
如果没有,请前往https://gitlabxxx/ 项目--个人资料设置--个人访问令牌--创建个人令牌(该令牌再次刷新就无法查看,请记得保存)"
#Private token
read privateToken
echo $privateToken
while true; do
#Individual group List
echo "下载个人所有项目请输入1,下载所属群组下的项目请输入2"
read putKey
if [ $putKey = "1" ]; then
allProjects
break
elif [ $putKey = "2" ]; then
groupProjects
break
else
echo "请输入正确的指令"
fi
done