1

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?

1

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
  • that's it. Simply perfect!!!!!!!!!!!!!! THANKS! – SpaceDog Feb 23 '13 at 9:10
1

I know the answer is different, but is there a reason you don't use the built-in commands?

such as this is crontab

@reboot /usr/sbin/ipset restore -f /ip/ipall.txt

and

ipset save -f /ip/ipall.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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