Home > OS >  Logging all commands of Shell Script to log file
Logging all commands of Shell Script to log file

Time:10-06

Hello all after hours of google searching hoping somebody here can provide direction.

Currently I have a .sh file which I run in putty to kick off a process. The current way I do this is:

sh /filepath/file.sh >> /filepath/file_date.log

While this works; I find it cumbersome to make sure I do the >> and logfile path correct for each of the .sh files I invoke.

The current contents of my .sh file are as such:

#!/bin/bash
set -x

frstdt=`date -d "last monday - 2 week"  "%m%d%Y"`;
scnddt=`date -d "last sunday - 1 week"  "%m%d%Y"`;
tday=`date -d "$date"  "%m_%d_%Y"`;
frstdtem=`date -d "last monday - 2 week"  "%m/%d/%Y"`;
scnddtem=`date -d "last sunday - 1 week"  "%m/%d/%Y"`

cd /filepath/
/filepath/cloak-hive -f query.hql

--Here I use hive -e to get some results from the query and write them to a .csv file.

ehco "Contents of email" | mailx -s "Subject" -a "Attchement" [email protected]

What I am trying to understand is what I can add to the beginning of the .sh file to log all of the actions that take place, the map/reduce updates HIVE etc. I can see all this now when using the >> command when calling the .sh but am lookign to simplify this for others on the team. I have tried using exec trap exec; | tee and other version of that but can not get anything to work. I do not need to see anything in the console after I enter sh /filepath/filename.sh I just want everthing to show up in the log with the log file name having the dat it runs in the file name(which is why I have tday as a variable; I would like it to show as /filepath/filename_$tday.log

CodePudding user response:

I don't advise this praxis, but if you really want to do this, you can do a

exec >> /filepath/file_date.log 2>&1

as the first statement in your script.

CodePudding user response:

Running the script with sh is an error. See Difference between sh and bash

If I am guessing correctly what you are trying to ask, my suggestion would be to create a simple wrapper, like

#!/bin/sh
/filepath/file.sh >> /filepath/file_$(date  %F).log

Your scripts should have a valid shebang and you need to chmod x both to be able to use that facility instead of spelling out sh or bash. Then you also don't need to keep track of which shell to use for which script.

I would also refactor your Bash script slightly.

#!/bin/bash
set -x

# Use modern POSIX syntax for command substitution
tday=$(date -d "$date"  "%m_%d_%Y")
frstdtem=$(date -d "last monday - 2 week"  "%m/%d/%Y")
scnddtem=$(date -d "last sunday - 1 week"  "%m/%d/%Y")
# Don't call the same date commands again; instead, just remove slashes
frstdt=${frstdtem//\//}
scnddt=$(scnddtem//\//}

# Probably avoid cd, but depends
# See https://stackoverflow.com/questions/45591428/what-exactly-is-current-working-directory
cd /filepath/
/filepath/cloak-hive -f query.hql

--Here I use hive -e to get some results from the query and write them to a .csv file.

# Don't misspell echo or Attachment
echo "Contents of email" | mailx -s "Subject" -a "Attachment" [email protected]

Using the most insane possible date format will create troubles down the road; I would recommend using ISO 8601 machine-readable YYYY-MM-DD date format for everything except possibly human display (but better yet, train your humans) but I did not change that part.

  • Related