Resolve command line execution shell script succeeds, but crontab execution fails

Source: Internet
Author: User

Actual production case

The production room self-built PPTP client is connected to the production room by dial-up, but once the client network is accidentally disconnected and then dialed again

Generates an IP conflict, so a script is written to monitor whether PPTP IP is multiple (2 IPs will be generated after a general conflict)

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

Manual execution without error, but put crontab execution will error prompt command Ifconfig not find

the command line executes the script, but the crontab execution of the shell script is unsuccessful due to two causes
Workaround
1. Path issues
See if the path to execute the script in Crontab is correct. For example:

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

2. Environmental variable Problems cause
The command to add an environment variable file to the shell script is typically added to the second line of the script:
There are two ways to make an environment variable file effective:

    • Method 1:
.  /etc/profile

Attention:. and/etc/profile spaces

    • Method 2:
source /etc/profile

Resolve command line execution shell script succeeds, but crontab execution fails

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.