zoukankan      html  css  js  c++  java
  • 解决命令行执行shell脚本成功,但crontab执行失败

    实际生产案例

    生产机房自建PPTP客户端通过拨号连接到生产机房,但是一旦客户端网络是意外断线再重新拨号

    会产生IP冲突,于是写了一个脚本监控PPTP的IP是否有多个(一般冲突以后会生成2个IP)

    #!/bin/bash
    #author Liuyueming
    . /etc/profile                 #开始没有加这个环境变量
    for i in {10..35}
    do
        n=`ifconfig|grep 10.13.0.${i}|wc -l`   #循环取ip并计数
    #   echo `ifconfig|grep 10.13.0.${i}`
            if [ $n -gt 1 ]                 #如果同一个ip出现两次及大于1说明ip冲突需要清除  
                then
                    for network in `ip add|grep 10.13.0.$i|sed 's#^.*global ##g'` #取出重复ip的网卡名删除掉
                      do
                        ip addr flush $network
                      done
            fi
    
    done
    

    手动执行没有报错,但是放入crontab执行就会报错提示命令ifconfig not find

    命令行执行脚本成,但crontab执行shell脚本不成功是由于两个原因导致 
    解决方法 
    1. 路径问题 
    查看crontab中执行该脚本的路径是否正确。例如:

    */1 * * * * cd /mypath/;./my_shell.sh > /dev/null 2>&1

    2. 环境变量问题导致 
    在shell脚本中加入环境变量文件生效的命令,一般加在脚本的第二行: 
    有两种方法可以使环境变量文件生效:

    • 方法1:
    .  /etc/profile

    注意: . 和 /etc/profile 空格

    • 方法2:
    source /etc/profile
  • 相关阅读:
    第三方登录原理
    django-rest-framework之 json web token方式完成用户认证
    HTTP Basic Authentication认证
    python 创建虚拟环境
    scrapy pipeline
    beautifulsoup 安装
    scrapy 安装
    Jmeter 安装
    css中的行高line-height
    html块级元素与行内元素
  • 原文地址:https://www.cnblogs.com/minseo/p/7448590.html
Copyright © 2011-2022 走看看