/ bash

Adding centralised logging to a bash script

Sometimes I run a bash script on many computers at once, and I want to collect an output. I'm sure there's a more elegant way of doing this, but here's my work-around:

Minimum Equipment List

  1. Web server, serving two files: success and failure
  2. A network connection between the client and that web server
  3. Back-end logging of access to those two files
  4. An extra line for each outcome in your bash script

curl -A "user: $(who | grep console | awk '{print $1}') ran command" http://centralLogServerURL/success

This will cause a line similar to the following to appear in your log file:

04 May 2018 14:59:57.787<14>1 2018-05-04T21:57:38.640071Z servername - [04/May/2018:21:57:38 +0000] "GET /success HTTP/1.1" 200 0 "-" "user: zstuart ran command"

(I use success and failure files, as it's less to pass in the curl command: one could conceivably use one file, say, report.)

When I want to look at the activity of the script, I can:

cat /var/log/access.log | grep success

This will show me all the recent succesful executions.