Jump to content



Photo

How to log the output of a linux script

howto

  • Please log in to reply
5 replies to this topic

#1 Shaun N.

Shaun N.

    Perfidious

  • Tech Issues Solved: 1
  • Joined: 09-November 04
  • Location: Northants, England
  • OS: Work: Windows 8/Ubuntu Home: Ubuntu

Posted 25 October 2012 - 12:16

Ok, So I have a script and I want to output whatever it puts on the screen to a log file, I know this is really simple and I've googled and got some bits but I am unsure as to where in the script I should put them.

Any help would be appreciated.

#!/bin/bash
#
# Data copy script for training product data refresh
# 25/10/12
#
# Data copy.
echo "__________________________________________________________________"
echo Training system update STARTED `date`
echo TIMECHECK started Refreshing server product data on stuff `date`
scp -Cr /snapshot/data/stuff/PROD.FILE server:/data/stuff
scp -Cr /snapshot/data/stuff/PROD.FILE server:/data/stuff
scp -Cr /snapshot/data/stuff/STOCK.FILE server:/data/stuff
echo TIMECHECK completed server product update `date`
ssh server chmod -R 777 /data
echo "__________________________________________________________________"
~



#2 Haggis

Haggis

    Neowinian Senior

  • Tech Issues Solved: 7
  • Joined: 13-June 07
  • Location: Near Stirling, Scotland
  • OS: Debian 7
  • Phone: Samsung Galaxy S3 LTE (i9305)

Posted 25 October 2012 - 12:23

you can usee tee

for example

ls -al | tee blah.txt

this would display ls-al output on the screen but also echo it to the blah.txt too

what of the above do you want logged?

#3 OP Shaun N.

Shaun N.

    Perfidious

  • Tech Issues Solved: 1
  • Joined: 09-November 04
  • Location: Northants, England
  • OS: Work: Windows 8/Ubuntu Home: Ubuntu

Posted 25 October 2012 - 12:27

This script will be run manually but I need any output it shows on the screen to be output to a log also.

This is what I have been suggested I do.

echo Tar file created as $ZIPFILENAME >> $LOGFILE

Is this the best way to have it logged?

#4 Farstrider

Farstrider

    The Clash

  • Joined: 16-March 03
  • Location: Somerset West, Cape Town, SA
  • OS: Arch Linux

Posted 25 October 2012 - 12:30

This to see the output and log the result to a file.

$ update-client > my.log

update-client 2>&1 | tee my.log

2>&1 redirects standard error to standard output, and tee sends its standard input to standard output and the file.

#5 OP Shaun N.

Shaun N.

    Perfidious

  • Tech Issues Solved: 1
  • Joined: 09-November 04
  • Location: Northants, England
  • OS: Work: Windows 8/Ubuntu Home: Ubuntu

Posted 25 October 2012 - 12:34

This to see the output and log the result to a file.

$ update-client > my.log
update-client 2>&1 | tee my.log
2>&1 redirects standard error to standard output, and tee sends its standard input to standard output and the file.


Command not found in the log using update-client

#6 OP Shaun N.

Shaun N.

    Perfidious

  • Tech Issues Solved: 1
  • Joined: 09-November 04
  • Location: Northants, England
  • OS: Work: Windows 8/Ubuntu Home: Ubuntu

Posted 25 October 2012 - 12:47

This worked perfectly :D

exec > >(tee /var/log/my_logfile.txt)




Click here to login or here to register to remove this ad, it's free!