I've never really worked with cron till very recently.
I have however written lots of stuff in Perl. Recently my boss asked me to write him a script that parses some log files and outputs a report. He wants it run under cron periodically.
The problem I'm having is that I write my scripts to take files in the following manner:
foo.pl [options] file1 file2 file3... fileN
This is the way I've always done it.
Unfortunately, this makes things difficult for a cron command, because my cron command has to know what files to use.
I thought about this, and decided to keep my script the way it was. I don't think I should be coding things like the format of my script's input files or their location into my script. This seems to be problematic, because then if the format of the files change, I have to change my script. I also have to add a configuration variable to tell it what directory to pull the files from, which again seems like something that the caller should know. I figured that this should be true whether my caller was a person, or a cron command.
My first attempt at resolving this led to a cron command similar to the following:
cd $SRC_DIR; find <some_dir> -regex=PATTERN -type f -print | ls -ltr | tail -n NUM_FILES | xargs perl -Mlocal::lib=support foo.pl [options]
The above command should find files in <some_dir> that match PATTERN and are the NUM_FILES files that were most recently modified.
This caused me problems with my boss, and my configuration manager. Scripts where I work are usually written to go out and get the files from the correct directory. That is what they were expecting, and they don't want any complicated cron commands in their crontab. They feel that cron is difficult enough to deal with, and that we should keep it as simple as possible. I should mention here that there were several options necessary to run the script, so the command that went into cron was somewhat longer than the one above. There were like 4 additional script options needed, I want to say that so you know it wasn't something horribly bad where I was requiring 100 script options.
My second attempt at resolving this was to simply put this information in a shell script with something similar to:
foo.sh
---
#/bin/env sh
cd $SRC_DIR;
find <some_dir> -regex=PATTERN -type f -print | ls -ltr | tail -n NUM_FILES | xargs perl -Mlocal::lib=support foo.pl [options]
---
with a crontab entry like:
* * * * * foo.sh
Then there were complaints stating that if someone wanted to change the options to the script, they'd have to edit this shell script. So they didn't think this was reasonable. It was decided that people that want to modify the way my script is run shouldn't have to modify a shell script to do it
I ended up writing a wrapper to my script called foo_cron.pl. It was written in Perl, took about 2 hours to write, found the files needed, and ran the original script to get results taking the same options as the original script. My original script still has the simple interface, so if someone wants to test my script, it won't break just because the test files weren't in the right directory or didn't match the correct pattern.
My reason for writing this post is that I think this is probably all just bat shit insane. Perhaps having a 200 character cron command is a bad idea. I can understand that, but I think the second option should have been okay. I definitely think that I shouldn't be writing a separate cron interface for each script. At the same time, I don't want to add file information to my scripts, as I see that as something that should be separated out somehow.
What do you think? Should I just write such scripts to know what directory and file name pattern they need to get files? Should I fight to get to just use that command, or should the simple shell script be enough? Have I just gone insane, was the obvious solution to just write code that goes and gets the files?
EDIT: Added a cd statement to that cron command.
I have however written lots of stuff in Perl. Recently my boss asked me to write him a script that parses some log files and outputs a report. He wants it run under cron periodically.
The problem I'm having is that I write my scripts to take files in the following manner:
foo.pl [options] file1 file2 file3... fileN
This is the way I've always done it.
Unfortunately, this makes things difficult for a cron command, because my cron command has to know what files to use.
I thought about this, and decided to keep my script the way it was. I don't think I should be coding things like the format of my script's input files or their location into my script. This seems to be problematic, because then if the format of the files change, I have to change my script. I also have to add a configuration variable to tell it what directory to pull the files from, which again seems like something that the caller should know. I figured that this should be true whether my caller was a person, or a cron command.
My first attempt at resolving this led to a cron command similar to the following:
cd $SRC_DIR; find <some_dir> -regex=PATTERN -type f -print | ls -ltr | tail -n NUM_FILES | xargs perl -Mlocal::lib=support foo.pl [options]
The above command should find files in <some_dir> that match PATTERN and are the NUM_FILES files that were most recently modified.
This caused me problems with my boss, and my configuration manager. Scripts where I work are usually written to go out and get the files from the correct directory. That is what they were expecting, and they don't want any complicated cron commands in their crontab. They feel that cron is difficult enough to deal with, and that we should keep it as simple as possible. I should mention here that there were several options necessary to run the script, so the command that went into cron was somewhat longer than the one above. There were like 4 additional script options needed, I want to say that so you know it wasn't something horribly bad where I was requiring 100 script options.
My second attempt at resolving this was to simply put this information in a shell script with something similar to:
foo.sh
---
#/bin/env sh
cd $SRC_DIR;
find <some_dir> -regex=PATTERN -type f -print | ls -ltr | tail -n NUM_FILES | xargs perl -Mlocal::lib=support foo.pl [options]
---
with a crontab entry like:
* * * * * foo.sh
Then there were complaints stating that if someone wanted to change the options to the script, they'd have to edit this shell script. So they didn't think this was reasonable. It was decided that people that want to modify the way my script is run shouldn't have to modify a shell script to do it
I ended up writing a wrapper to my script called foo_cron.pl. It was written in Perl, took about 2 hours to write, found the files needed, and ran the original script to get results taking the same options as the original script. My original script still has the simple interface, so if someone wants to test my script, it won't break just because the test files weren't in the right directory or didn't match the correct pattern.
My reason for writing this post is that I think this is probably all just bat shit insane. Perhaps having a 200 character cron command is a bad idea. I can understand that, but I think the second option should have been okay. I definitely think that I shouldn't be writing a separate cron interface for each script. At the same time, I don't want to add file information to my scripts, as I see that as something that should be separated out somehow.
What do you think? Should I just write such scripts to know what directory and file name pattern they need to get files? Should I fight to get to just use that command, or should the simple shell script be enough? Have I just gone insane, was the obvious solution to just write code that goes and gets the files?
EDIT: Added a cd statement to that cron command.