脚本使用
判断进程是否存在,不存在则启动
#!/bin/bash
while [ 1 ]
do
count=`ps -ef|grep projectname|grep -v "grep"| wc -l`
if [ $count -le 0 ]; then
nohup sh /root/project/bin/run.sh
time=`date`
outmsg="restart project"
echo $time$outmsg >> ./restart.txt
sleep 3
else
sleep 5
fi
done
判断多进程中的僵尸进程数量
如果多进程中的僵尸进程数量到一定值,则重启主程序
#!/bin/bash
while [ 1 ]
do
count=`ps -ef|grep projectname|grep defunct| wc -l`
if [ $count -ge 2 ]; then
killall -9 projectname
sleep 2
nohup bash /root/project/bin/run.sh
time=`date`
outmsg="restart Project"
echo $time$outmsg >> ./restart.txt
sleep 3
else
sleep 5
fi
done
用top命令查询指定进程,并将结果输入到文件
#!/bin/bash
while [ 1 ]
do
top_print=`top -n 1 -b|grep runprojectname`
time=`date`
space=" "
outmsg=$time$space$top_print
echo $outmsg>> ./topprint.txt
sleep 5
done
一些其他命令说明
GDB调试
1.gdb 程序名称 core文件名称
2.r(运行)
3.bt (崩溃地址)
linux一些系统命令
ldd 库名称 //查看依赖库
查看gpu使用情况:watch -n 1 nvidia-smi //1秒刷新一次
top -p pid //查看指定进程的使用情况
查找文件:find . -name “*.txt” | xargs grep “content”
grep -rn “content” *
添加环境变量:
export LD_LIBRARY_PATH=/home/path:$LD_LIBRARY_PATH
压缩和解压缩文件:
tar cvf filename.tar dirpath //压缩文件
tar xvf filename.tar //解压文件
查看磁盘空间:
df -h
du -sh path/*
后台运行并将输出内容保存到文件:
nohup python3 -u file.py>log.txt 2>&1 &
tail -f filename //动态查看刷新的文件
netstat -n ip:port
netstat -t(tcp)
netstat -l(listen)
iostat -d -k 1 10 // 每1秒刷新一次,共10次,内存读写速度