Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have this txt files that contains IPs, one per line of file, that I want to block using ipset.

I have this bash script that essentially reads from the plain txt file and constructs an array. Then it iterates the array elements and add each one to the ipset I have created for that purpose.

The problem is this: if I execute the script manually from the terminal, it works perfectly, but when I add the script to run periodically using crontab, the script runs but the IPs are not added to the ipset.

This is the relevant part of the script.

index=0
while true; do
  ipset -quiet -A myIpset $[arrayOfIPS[$index]}
  index=$[$index + 1]
  if [ "$index" -gt "$lastIndexOfArray" ];
  then break
  fi 
done

This works perfectly from terminal but not running from a crontab task. why?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Your shell knows where to find executables (like ipset) by looking in your PATH, which is set by your environment. cron does not share the same environment. Adding this at the top of the crontab (or your script) should tell it where to find commands as you expect:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
share|improve this answer
    
that's it. Simply perfect!!!!!!!!!!!!!! THANKS! –  Digital Robot Feb 23 '13 at 9:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.