Posts Tagged ‘linux’

Sometimes I love the CLI.

At work today, I had someone ask me to run a script for them.  Now, this script is usually run automagically via cron, but the information is needed on the xth business day of every month, rather than just the xth day.  This has been a periodic thorn in my side ever since I wrote the script quite some time ago.

But recently the linux server our cron jobs run on mysteriously allowed me to run the at command.  This is kind of like an ad-hoc cron.  You tell it when you want to do something, and it does it at that time.

So I asked the business user who is the point person for this kind of stuff how business days were determined, and then I wrote a script to figure them out.  I tossed them into at after passing the dates through a regex, wrapping the command I wanted around them.

Only I did it wrong.  My logic was messed up, and now I had like 120 at entries and only one atrm command to remove them.  I did not despair, however.  Instead, I did this:

atq | awk ‘{ print $1; }’ | xargs atrm

And then atq was empty.  atq displays a job number and then some other information.  Piping this information into awk allows me to extract and print that first value, and passing it to xargs allows me to use it as a parameter in a command.  This little snippet is like me going through the list one by one and typing in atrm 106; atrm 104; […].

I get this great feeling of elation when I’m able to do away with a bunch of manual work with a nice, tight solution.

Read Full Post »