LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-20-2006, 03:56 PM   #1
macadam
Member
 
Registered: Sep 2003
Posts: 72

Rep: Reputation: 15
cron job - strange behaviour


Hi,

I run a bash file as a cron job.
The content of the file is:
#!/bin/bash

/etc/rc.d/init.d/Blacklist stop
/etc/rc.d/init.d/iptables stop
/etc/rc.d/init.d/iptables start
/etc/rc.d/init.d/Blacklist start

The Blacklist start runs 2 iptables xxxxx commands.
When I execute the bash file 'manually', everything runs perfectly.
But when ran from the cron job, I get an error: iptables command not found

Help!!!!

Thanks a lot

macadam
 
Old 07-20-2006, 04:21 PM   #2
madluther
Member
 
Registered: Aug 2004
Distribution: LFS
Posts: 350

Rep: Reputation: 31
Add a path statement to the begining of the script

ie

Code:
#!/bin/bash

export PATH="/bin:/usr/bin:/sbin:/usr/sbin"

/etc/rc.d/init.d/Blacklist stop
/etc/rc.d/init.d/iptables stop
/etc/rc.d/init.d/iptables start
/etc/rc.d/init.d/Blacklist start
HTH

Mad
 
Old 07-20-2006, 04:25 PM   #3
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668
"/etc/rc.d/init.d/iptables" is itself a script (called an init script). It runs a binary called iptables. iptables not found is letting you know it has no path to the binary.

It works in command line because the script inherits the user's environment including the PATH. However from cron it is not inheriting an environment. To solve it you just need to put a PATH statement in your bash script.

"which iptables" (run as the user where the script works) will give you the path where the iptables binary is. Something like:

/sbin/iptables

You can then either edit the init script (which I wouldn't do) to have /sbin/iptables instead of just iptables where it calls it.

-OR-

Modify your bash script to include the PATH statment:

Code:
PATH=$PATH:/sbin
The above tells it to add /sbin to the existing PATH declaration.
Then when your bash script runs the init script that is asking for the iptables binary it will look in all the directories of PATH including the /sbin you just added to find the binary. When it finds it the command will run as it did from the command line.
 
Old 07-20-2006, 11:45 PM   #4
macadam
Member
 
Registered: Sep 2003
Posts: 72

Original Poster
Rep: Reputation: 15
Thanks a lot...it works.

Quick answer as usual!

Regards,

macadam
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
CRON Job tommytomato Fedora 12 09-13-2004 12:38 AM
cron job working2hard Programming 6 07-28-2004 09:12 PM
Cron job rajasekarvr Linux - General 4 05-03-2004 05:35 PM
cron job ? johnyy Linux - Software 3 12-10-2003 06:00 PM
cron job thesnaggle Linux - Newbie 1 09-19-2003 10:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration