分类 linux 下的文章

运行了几个python后台守护进程,需要重启的时候不能简单 killall python 很容易会误杀其他服务
所以备忘以下的脚本

已验证有效

#!/bin/sh

function PidFind()  
{
    PIDCOUNT=`ps -ef | grep $1 | grep -v "grep" | grep -v $0 | awk '{print $2}' | wc -l`;  

    if [ ${PIDCOUNT} -gt 1 ] ; then  
        echo "There are too many process contains name[$1]"  
    elif [ ${PIDCOUNT} -le 0 ] ; then  
        echo "No such process[$1]!"  
    else  
        PID=`ps -ef | grep $1 | grep -v "grep" | grep -v ".sh" | awk '{print $2}'` ;  
        echo "Find the PID of this progress!--- process:$1 PID=[${PID}] ";  

        echo "Kill the process $1 ...";          
        kill -9  ${PID};  
        echo "kill -9 ${PID} $1 done!";  
    fi  
}
#查找并kill掉 带有 newdoc.daemon 名称的进程
PidFind newdoc.daemon

#restart daemon
/path/to/startscript/chkdaemon.sh

当搜索结果不唯一时,脚本也会拒绝执行杀进程

参考文:https://jingyan.baidu.com/article/b7001fe1a5dbc40e7382dd75.html

需要加一条白名单规则让远程机器连mysql端口 3306

iptables -A INPUT -p tcp --dport 3306 -s 45.83.151.250 -j ACCEPT

添加完后检查,并保存生效

iptables -L
iptables-save

规则内原本已经有 DROP 3306 端口的规则,所以新增的这条无效
直接进去改配置文件(实际情况路径和文件名可能略有不同)

vi /etc/iptables/rules.v4

把新增的规则调整在 drop规则之前,保存, 并使之生效

iptables-restore < /etc/iptables/rules.v4

研究在已经部署lnmp的服务器上搭建以nginx做直播推流分发的服务端,需要给一键部署的nginx 添加rtmp第三方模块。
先说总结,lnmp一键部署脚本考虑得非常周到,只要简单操作即可无缝编译升级。只是文档说明有点语焉不详,网上流传的其他日志也错漏颇多,跟着别人的日志做差点翻车,把在线的网站给炸down了,还好直接覆盖安装没有影响任何配置。


- 阅读剩余部分 -

按客户要求校正某些数据

要点备忘

数组

array_name=(item1 item2 ... itemN)
使用时 array_name[N]

循环

用到了 for 和 while,格式分别是

for [表达式];do
done

while [表达式];do
done

变量类型转换 string to int

有awk之类几种方法,我用了最偷懒的。
先确保字符串变量本身没有奇怪的字符,只有阿拉伯数字(参考下文lines)
在进行逻辑运算的关键位置用 $(()) 包裹变量声明为数值

随机读取若干行

随机读10行

shuf -n 10 input_filename






- 阅读剩余部分 -

bash 是win10下挺方便的linux子系统,但我发现它不会自动挂载 U盘和移动硬盘。哪怕是开机前就已经插在电脑上的U盘,也不能识别。在 /mnt/ 下映射的只有内置硬盘的win盘符

要实现挂载也不难,稍微搜一下就找到方法了,备忘如下:

挂载脚本 mountdish.sh

#!/bin/bash
# mount U盘或移动硬盘
# 用法 挂载 J盘
# ./mountdish.sh j


typeset -u targetDrv
targetDrv=$1
targetPath="/mnt/$1"

if [ ! -x "$targetPath" ]; then
    mkdir "$targetPath"
    echo "new path created $targetPath"
fi

sudo mount -t drvfs ${targetDrv}: ${targetPath}

echo "done ${targetDrv}:"

卸载U盘 umountdisk.sh

#!/bin/bash
# umount U盘或移动硬盘
# 用法 卸载 J盘
# ./umountdisk.sh j

targetPath="/mnt/$1"

sudo umount ${targetPath}

echo "done umount ${targetPath}:"

用完记得要 umount,否则win会锁定读写状态不给安全移除。


参考文:
https://www.cnblogs.com/tl542475736/p/8486440.html