Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script commands not working in cron

Tags:

linux

bash

cron

I have the following bash script to read logs and check for brute force then block violating IP using iptables.

#!/bin/bash
#blah blah run some commands to get the IP
iptables -A INPUT -s $p -j REJECT --reject-with icmp-host-prohibited
echo "BANNED $p FOR $COUNT ATTEMPTS" |wall

I did chmod 755. When I run the command from terminal it works fine. But when I setup a cronjob using crontab -e as root, it gets the IP and echos the "BANNED ..." message to the wall but nothing is added to the iptables list.

PS. I tried both #!/bin/bash and #!/bin/sh but no luck.

like image 259
Tarek Sawah Avatar asked Apr 10 '14 09:04

Tarek Sawah


People also ask

Why crontab scripts are not working?

One of the most frequent causes for the crontab job not being correctly executed is that a cronjob does not run under the user's shell environment. Another reason can be – not specifying the absolute path of the commands used in the script.

Why is my cron not working?

Check permissions Another user might have created the cron job and you may not be authorized to execute it. Note that the root must own jobs added as files in a /etc/cron. */ directory. That means that if you don't have root permissions, you might not be able to access the job.

Can cron run commands?

Cron allows Linux and Unix users to run commands or scripts at a given date and time. You can schedule scripts to be executed periodically. Cron is one of the most useful tool in a Linux or UNIX like operating systems. It is usually used for sysadmin jobs such as backups or cleaning /tmp/ directories and more.


1 Answers

Try to provide full path to iptables e.g.

$ which iptables
/sbin/iptables

and than modify your script like that:\

#!/bin/bash
#blah blah run some commands to get the IP
/sbin/iptables -A INPUT -s $p -j REJECT --reject-with icmp-host-prohibited
echo "BANNED $p FOR $COUNT ATTEMPTS" |wall
like image 146
kpopovbg Avatar answered Oct 11 '22 20:10

kpopovbg