Scripting, cron and obsessive compulsive programming...

Status
Not open for further replies.

concurrent

Smack-Fu Master, in training
81
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.
 

concurrent

Smack-Fu Master, in training
81
chalex":230x706s said:
I typically write a bash wrapper and then have cron run that, as you did in your second attempt.

Editing the shell script is more desirable than modifying the crontab, IMO, so I agree with you.

However, if the ultimate end-user thinks that's too complicated or whatever, you have to go with their requirements, as you did.

Thank you for your reply.

But I was kinda hoping you'd say something along the lines of:

"Tell them that it is wasted effort to write a second interface for a script just because they don't know how to deal with script options if they're on the end of a long bash command"

or something similar. I guess this just isn't one of those battles that are worth fighting. I have to do something similar with another script, and I imagine they'll want all my scripts to have a cron wrapper of some kind, if they intend to run them with cron... or just have code which finds files. I like to think that I'm so pained by this because I'm a perfectionist, and not because I'm lazy. I mean, I am lazy, but dammit, isn't it bad coding practice to write information on the files you run in your scripts?

Meh... does anyone else find themselves so torn over things like this?
 

kperrier

Ars Legatus Legionis
21,507
I think the shell script option is the best there too. I fail to see how editing the shell script is less reasonable than editing the crontab.

Perhaps you modify the original script, adding a -c option so that it knows it is being called from cron and have it accept the modified or limited set of command line options. Still looks like more work than just editing the shell script.
 

ronelson

Ars Legatus Legionis
21,399
Subscriptor
You could also have a .conf file that the shell script reads. Not sure exactly what they would need to change, but you could do:
Code:
foo.conf:
---------
PATTERN=<blah>

foo.sh
------
#/bin/env sh

. ./foo.conf

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]
This gives a little flexibility to your user, in that they do not need to edit a 100 character line and possibly fuck it up, and lets you run the simple shell script from cron. You do have to admit that chances are pretty good someone will fuck up that piped command very quickly on you. To prevent them from fucking things up with the config file, make sure that any vars set in the conf file are quoted, so that if they are null it resolves to null instead of trying to read the next option as the argument to the current option. i.e. read '-file "$FILE" -verbose" with an option of "" instead of "-verbose". Hope that makes sense.

Or, you could have a perl wrapper that uses one of the many config CPAN modules and then makes a system/exec call with the right vars. Much more comprehensive error checking that way IMO. This is what I do. Remember, nothing wrong with "my @files = `find blah blah`;" instead of a shitload of readdir() or File::Find calls when you really just need to emulate the unix find command.
 

dredphul

Ars Tribunus Angusticlavius
6,342
Subscriptor++
Your coworkers sound like a huge pain in the ass.

How often does the configuration of your script changes? If it changes every day or every week, then modifying the cron job or shell script might be a problem. If the configuration doesn't change often (if at all) then you have a version of the bicycle shed problem (aka Parkinson's Law of Triviality): http://en.wikipedia.org/wiki/Parkinson' ... Triviality

If the configuration does change frequently, consider taking a look at Config-General: http://search.cpan.org/dist/Config-General/General.pm
 

concurrent

Smack-Fu Master, in training
81
dredphul":2djvx7ex said:
Your coworkers sound like a huge pain in the ass.

How often does the configuration of your script changes? If it changes every day or every week, then modifying the cron job or shell script might be a problem. If the configuration doesn't change often (if at all) then you have a version of the bicycle shed problem (aka Parkinson's Law of Triviality): http://en.wikipedia.org/wiki/Parkinson' ... Triviality

If the configuration does change frequently, consider taking a look at Config-General: http://search.cpan.org/dist/Config-General/General.pm

I work on an old project, dating from at least the mid 90s. I think they got a lot of basic stuff wrong (they use an in house ad-hoc system for both logging and documentation, I've flat out refused to use their documentation system and use Perldoc instead. I'm undecided on their logging system, but have avoided it for at least one Catalyst project).

They don't follow best practices IMO, and they don't test their programmers before hiring so we get some people that code like its 1989 (no functions, just 800 lines of procedural code in a huge block, they get stuff done so management loves them, but there is a maintenance cost there).

I'm constantly pointing out things that have been done wrong, and in many cases do things differently than the rest of the people here.

So I think I've been labeled a complainer by management...

As I said, everyone else writes their scripts to go out and get the files. Very few of our scripts use something like script options. The configuration files we have are all just shell scripts listing out environment variables, which I think is a pretty horrible way to do it. I do think they're getting sick of me going "we should do it this way". My boss tried to pressure me into writing the script to be environment variable configurable... I resisted and I believe it was the correct choice.

The configuration will not change frequently. I think the biggest problem here is that the configuration manager doesn't want to deal with this stuff. Everything she installs for me is installed differently that other people's stuff (my stuff installs in a typical Linux fashion, other people either work in compiled languages or have root access and get to install everything themselves) and every time she sees something she thinks is complicated she screws it up. She screwed up the original cron command I gave her by not replacing <directory> with something more meaningful. The second time I gave her a cron command she screwed it up despite me writing the thing exactly as she was supposed to install it. I'm seriously starting to think she's doing it on purpose. Then she complained to my boss, and he said I needed to do better... hence a complete wrapper for the scripts interface in a second Perl script.

I think I'm going to try to do option #2, and if no one complains (not likely) then that will be it. Otherwise I'll create a Perl wrapper.
 

topham

Ars Legatus Legionis
13,756
Subscriptor
Nothing goes into a cron file that can't be explained in 30 seconds or less.

Besides, many scripts have lots of required parameters that are difficult to jam on a command line (environment variables) and since cron inherits pretty much nothing it's best to set them all up in a wrapper anyway.

It also means when you need to test a cron version of the task you don't need to drag the options around, just run that version of the wrapper.
 

doornail

Ars Scholae Palatinae
1,238
concurrent":2l8rrwj6 said:
My second attempt at resolving this was to simply put this information in a shell script with something similar to [...]

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

This is the right approach. Those arguments are stupid. Scripting languages, as opposed to programming languages, exist to fill exactly this kind of need.
 

concurrent

Smack-Fu Master, in training
81
ronelson":wck47f4g said:
I think I'm going to try to do option #2, and if no one complains (not likely) then that will be it. Otherwise I'll create a Perl wrapper.
I think you may also want to look at option #42 - start looking for another job. There are plenty of places that will actually let you do what they hired you for.


I'm planning on it, but they have a great location. I can bike to work. When you can bike to work, you're willing to put up with a bit more BS than you usually would. I'll be here at least until mid summer. ;)
 

concurrent

Smack-Fu Master, in training
81
dredphul":3rgl7eh7 said:
Your coworkers sound like a huge pain in the ass.

How often does the configuration of your script changes? If it changes every day or every week, then modifying the cron job or shell script might be a problem. If the configuration doesn't change often (if at all) then you have a version of the bicycle shed problem (aka Parkinson's Law of Triviality): http://en.wikipedia.org/wiki/Parkinson' ... Triviality

If the configuration does change frequently, consider taking a look at Config-General: http://search.cpan.org/dist/Config-General/General.pm

I found this to be hilarious, because we ended up having a meeting about this script, and like 5 people worked together to decide how to name each report.

I mean, with the amount of complexity that goes into how you generate such a report... I found it absurd that no one gave me input on how it should be generated, but everyone had a different idea for how to name the damn thing. I ended up having to change the report name several times.

That... uh... eventually became part of the effort btw. I ended up having to write that into the wrapper Perl script. I left this detail out because it happened after I'd already moved to a Perl script wrapper based solution.
 

ronelson

Ars Legatus Legionis
21,399
Subscriptor
I'm planning on it, but they have a great location. I can bike to work. When you can bike to work, you're willing to put up with a bit more BS than you usually would. I'll be here at least until mid summer.
I feel you. I work from home and moved to an area with very few companies in the same business, they know they have me over a barrel and prove it every year come raise time :) At least everything *else* about the job is good.
 

dredphul

Ars Tribunus Angusticlavius
6,342
Subscriptor++
concurrent":7lj8cmy1 said:
dredphul":7lj8cmy1 said:
Your coworkers sound like a huge pain in the ass.

How often does the configuration of your script changes? If it changes every day or every week, then modifying the cron job or shell script might be a problem. If the configuration doesn't change often (if at all) then you have a version of the bicycle shed problem (aka Parkinson's Law of Triviality): http://en.wikipedia.org/wiki/Parkinson' ... Triviality

I found this to be hilarious, because we ended up having a meeting about this script, and like 5 people worked together to decide how to name each report.

I mean, with the amount of complexity that goes into how you generate such a report... I found it absurd that no one gave me input on how it should be generated, but everyone had a different idea for how to name the damn thing. I ended up having to change the report name several times.

I had thought that technical knowledge would be really important in doing my job, but understanding human nature and how to deal with people's quirks is more important. Your coworkers may give a little if you do so yourself. I've run into coworkers who are only reasonable when they feel they have some sort of personal relationship with you. Otherwise, they are sources of major frustration as they object to little, minor issues and continue to be a roadblock.
 
Status
Not open for further replies.