http://www.perlmonks.org?node_id=484828

jimbus has asked for the wisdom of the Perl Monks concerning the following question:

Bretheren

So with scripts in crontab, you are supposed to explicit paths. But whats a good way to specify the path to a config file?

In a previous thread it was noted that I had several versions of a script running and that I running a single script (or links from a single script) and using config files to differentiate.

Well only 2 scripts of the 4 were copies of each other, but I see the wisdom in centerally locating them and maintaining one piece of code. (also, the wisdom of using distinct script names :) I found a sub for reading java style properties into a hash and added it to my local Utils package. Now I'm wondering what the SOP is for getting the conf file to the cron script.

I suppose this is a style thing, but it seems kind of hokey to hardcode a config path into it (I'm passing the node name to use as the finally subdir). I was wondering if I could assume the directory was local to the script once it was running or if I should just specify the full path and file name as an option on the commandline.

Thanks!


a ravenous Jimbus mumbles, "Never moon a werewolf!"

PS: I've been following the seeker's page for a couple days and I think its superb that you guys can/will tackle anything. I've yet to see anyone left hanging. I use other forums for differnt products and sometimes it like they saw me coming and left. (insert witty barb here) I wonder sometimes if I have issues communicating or I'm that out of touch with the intended usage of the product... anyhow, thanks again.

Replies are listed 'Best First'.
Re: Of crontab and paths
by DrWhy (Chaplain) on Aug 18, 2005 at 16:07 UTC
    Well, I've just been satisfied in the past to be 'hokey' and hardcode all paths in the crontab entry. It works great!

    But you could go other ways. For instance you could add code in your script that assumes the config file is in the same dir as the script you are executing, e.g.:

    use File::Basename; $config = shift @ARGV; $dir = dirname $0; chdir $dir; open F, "<$config" or die "Can't open $config: $!\n"; ... parse $config file here ...

    --DrWhy

    "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

Re: Of crontab and paths
by Anonymous Monk on Aug 18, 2005 at 16:33 UTC
    Just follow the UNIX way. Many application/programs use the first config file they find, in the following order:
    • The one given as a command line argument.
    • The one given in an environment variable.
    • Fixed name in the current directory.
    • Fixed name in the users home directory.
    • One or more fixed paths (fixed paths can often be set at compile time).
    • Compiled in defaults.
    Not all applications use all points, for instance, many applications will not look in the current directory (unless pointed to by the first two options) - but an editor like vi does.

    I tend to follow that strategy myself as well when writing applications.

      I decided not to be as thorough as above, but that was good info for me to keep squirelled away. I did couple google searches and ran across Getopt and did the following:
      use Utils; use Getopt::Long; my $conf_file = ''; GetOptions ('conf=s' => \$conf_file); if ( length($conf_file) == 0) { print "\nUsage: loadSMSC -conf=/path/to/config.properties\n"; exit; } %props = getProperties("$conf_file"); $node_name = $props{"node"}; $machine = $props{"machine"}; $username = $props{"username"}; $passwd = $props{"passwd"}; $directory = $props{"directory"}; $localpath = $props{"localpath"}; $log_name = $props{"log.name"}; $numRows = $props{"rows"};
      getProperties is in Utils.pm and goes a little something like:
      sub getProperties($) { my ($propfile) = @_; my (%props); # Return value: a hash of property values. my ($ln,$name,$value,@nv); open(PROPFILE,"$propfile") or die "Unable to open $propfile"; while ($ln = <PROPFILE>) { chomp $ln; $ln =~ s/#(.*$)//; # Remove line comments in the properties + file. @nv = split /s*=/,$ln,2; $value = pop @nv; $value =~ s/^\s*=?\s*//; $value =~ s/\s*$//; $name = pop @nv; $name =~ s/^\s*//; $name =~ s/\s*$//; if ($name ne "") { $props{$name} = $value; } } close(PROPFILE); return %props; } sub dumpProperties { my (%props) = @_; foreach $prop (keys %props) { #print "\t\t$prop\n"; print "\t\t$prop:$props{$prop}\n"; } 1; }
      Thanks for the input!

      Jimbus

      A sniveling Jimbus advices, "Never moon a werewolf!"