LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Cron job with privileged commands - redhat linux (https://www.linuxquestions.org/questions/linux-newbie-8/cron-job-with-privileged-commands-redhat-linux-819423/)

Abhy 07-12-2010 10:49 AM

Cron job with privileged commands - redhat linux
 
Hi,

I want to create a cron job which has a few commands (to restore generic iptables with minimum level security) that need sudo privileges. The file has the following commands :

...
/sbin/iptables-save > /home/user1/iptables_bkp.txt
/sbin/iptables-restore > /home/user1/original_iptables.txt
/sbin/service iptables save
/sbin/service iptables restart
...

I have scheduled this as the root's crontab. It does execute at every timeout, however the iptables_bkp.txt file is blank, and the original iptables file is not uploaded.

Is there something missing that should be done to make this work ?

Regards,
~Abhi

smoker 07-12-2010 11:46 AM

You say "the file" has the following commands. Is it a script ? If so, you need to make sure you have chmodded it 755, and it probably won't hurt to have a shebang line at the top. Then you call the script from the crontab.

Code:

#!/bin/bash
/sbin/iptables-save > /home/user1/iptables_bkp.txt
/sbin/iptables-restore > /home/user1/original_iptables.txt
/sbin/service iptables save
/sbin/service iptables restart

save as my_script.sh
chmod 755 my_script.sh
chown root:root my_script.sh


All times are GMT -5. The time now is 08:55 AM.