sys·ad·min·ol·o·gy [sis-ad-mih-nol-uh-jee]

noun

  1. The scientific study of system administration and related phenomena.

Thursday, 19 June 2014

Running sort in cron: Order from the chaos


It turns out that sort behaves differently depending on the environment in which its run.

The background

We have a number of web sites which are monitored for changes by checksumming all of the files in the document root. We then checksum the results to end up with a site check sum, which changes if there are any files added, deleted or modified in anyway (well, the contents or filename at any rate)

To achieve this, we generate two file lists using find. One is recorded remotely and doesn't change (recorded) the other is generated by a cron job every five minutes (generated)

Running the command to create the generated list resulted in the alert going away, but when cron ran the command, alerts ensued.

The problem

Cron runs with its own environment variables (not necessarily the same as your user). Being as the sort command was used to order the file list, sort was returning one order for a normal user, and another order when run from cron.

In a nutshell

sort uses the locale environment variable LC_ALL to decide how a set of data should be sorted.

The solution

This is from the man page of sort

"*** WARNING *** The locale specified by the environment affects sort order.  Set LC_ALL=C to get the traditional sort order that uses native byte values."

as such, adding LC_ALL=en_GB.UTF-8 before the command which created the generated script solved the problem

(LC_ALL=en_GB.UTF-8 cd /var/www/somesite; find . - .....)



No comments:

Post a Comment