下面的方式常用于:脚本中有的命令是在当前路径执行,当移动目录路径的时候,会找不到当前目录的命令,下面可以解决这个问题,不管脚本在哪,都可以到当前路径再执行
[root@master ~]#cat /opt/abc/def/a.sh #!/bin/bash func_a() { echo cd $(dirname $0) echo $0 echo $(dirname $0) echo $(pwd) } func_a &>> /tmp/a.log [root@master ~]#/opt/abc/def/a.sh [root@master ~]#cat /tmp/a.log /opt/abc/def/a.sh /opt/abc/def /opt/abc/def
–
检测进程是否存在
/usr/local/alibaba-rocketmq/bin/runserver.sh ps aux |grep runserver | grep -v grep &> /dev/null echo $? 0表示有,1表示没有
移位shift用法
https://blog.csdn.net/zhu_xun/article/details/24796235
–
进程定时检测重启
下面两个脚本作为模拟进程来测试,往日志中打印时间
[root@master /data/script]#cat program_echo.sh #!/bin/bash cd $(dirname $0) func_echo(){ for i in {1..9999};do echo echo "echo $(date +%F_%T)" sleep 5 done } func_echo &>> program_echo.log [root@master /data/script]#cat program_test.sh #!/bin/bash cd $(dirname $0) func_test(){ for i in {1..9999};do echo echo "test $(date +%F_%T)" sleep 5 done } func_test &>> program_test.log
下面的脚本是放到定时任务中检测进程是否存在的脚本
[root@master /data/script]#cat check_program.sh #!/bin/bash PATH= XXXXX #此处需要修改 cd $(dirname $0) func_check_program_echo(){ echo echo echo "$(date +%F_%T)" ps aux | grep -v grep | grep -q program_echo if [[ $? -ne 0 ]]; then ps aux | grep -v grep | grep program_echo | xargs kill -9 nohup /bin/bash /data/script/program_echo.sh & echo "restart program_echo.sh" fi } func_check_program_echo &>> check_program.log func_check_program_test(){ echo echo echo "$(date +%F_%T)" ps aux | grep -v grep | grep -q program_test if [[ $? -ne 0 ]]; then ps aux | grep -v grep | grep program_test | xargs kill -9 nohup /bin/bash /data/script/program_test.sh & echo "restart program_test.sh" fi } func_check_program_test &>> check_program.log
模拟正常程序运行的命令如下,也应该将其放到开机启动中
nohup /bin/bash /data/script/program_echo.sh & nohup /bin/bash /data/script/program_test.sh & echo "nohup /bin/bash /data/script/program_echo.sh &" >> /etc/rc.local echo "nohup /bin/bash /data/script/program_echo.sh &" >> /etc/rc.local
定时检测拉起任务的定时任务
[root@master /data/script]#cat /etc/crontab */10 * * * * root /bin/bash /data/script/check_program.sh
–
–
–
–
[root@localhost sh-1.4.4]# cat abc.sh #!/bin/bash fun_a() { echo "abc" } fun_b() { echo "def" ls /dwfe/few } fun_c() { fun_a fun_b } fun_c >> d.log 2>&1
shell:读取文件的每一行内容并输出
写法一: ---------------------------------------------------------------------------- #!/bin/bash while read line do echo $line done < file(待读取的文件) ---------------------------------------------------------------------------- 写法二: ---------------------------------------------------------------------------- #!/bin/bash cat file(待读取的文件) | while read line do echo $line done
–
–
–
评论前必须登录!
注册