LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-26-2019, 02:24 PM   #1
funkytwig
Member
 
Registered: Jun 2016
Posts: 46

Rep: Reputation: Disabled
bash functon to run command and output to log file


I am trying to write a function in bash that takes a Linux command and runs it sending output to a log file. For each line the command outputs I want to add date/time in front of it in the log file. I almost got it working but some Linux commands don't work.

Code:
function log {
  timestamp=`date +%D_%R`
  log_text="${timestamp} $1"
  printf "${log_text}\n" >> $LOG
}

function log_file {
  while read line
  do
    log "${line}"
  done < "$1"
}

function run_cmd {
  tmp_log=/tmp/$$_cmd.log
  log "$1"
  $1 > ${tmp_log} 2>&1
  ret=$?

  if [ -f ${tmp_log} ]; then
    log_file ${tmp_log}
  fi

  if [ ${ret} -ne 0 ]; then
    log "${ret} : $1"
  fi

  return ${ret}
}
This works most of the time, for example if I do

run_cmd "ls -l /tmp"

The log file has each line of the output from ls on a separate line of the logfile with date/time before it.

However, if I do

du -hc --time --max-depth=1 /home/back/tvpp/ | sort -k2

I get the following (error)

03/26/19_19:03 du -hc --time --max-depth=1 /home/back/tvpp/ | sort -k2
03/26/19_19:03 du: invalid option -- '2'
03/26/19_19:03 Try 'du --help' for more information.
03/26/19_19:03 1 : du -hc --time --max-depth=1 /home/back/tvpp/ | sort -k2

Its something to do with using pipes but can quite figure it out.
 
Old 03-26-2019, 04:48 PM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
You can’t execute the pipeline like this. Try this line:
Code:
bash -c “$1” > ${tmp_log} 2>&1
(not tested)

In addition, you will have to handle double quotes in the command, I guess.
 
1 members found this post helpful.
Old 03-26-2019, 05:39 PM   #3
funkytwig
Member
 
Registered: Jun 2016
Posts: 46

Original Poster
Rep: Reputation: Disabled
Wink That worked

Quote:
Originally Posted by berndbausch View Post
You can’t execute the pipeline like this. Try this line:
Code:
bash -c “$1” > ${tmp_log} 2>&1
(not tested)

In addition, you will have to handle double quotes in the command, I guess.
Thanks, works great.

Last edited by funkytwig; 03-26-2019 at 05:40 PM. Reason: Forgot title
 
Old 03-26-2019, 06:10 PM   #4
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by funkytwig View Post
Thanks, works great.
Excellent. If you wish, you can mark this thread as "Solved" (see "Thread Tools" at the top of the thread).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to log internal-sftp chroot jailed users access log to /var/log/sftp.log file LittleMaster Linux - Server 0 09-04-2018 03:45 PM
Wrote Shell in C - piping output from first command to second command, sending to output file issue KarenWest Programming 2 03-31-2016 05:27 PM
[SOLVED] BASH - Log specific output to a log.txt file Rownzy Linux - Software 2 06-17-2015 04:21 AM
(Bash) Redirect all output from script to all.log and copy of errors to err.log hmsdefender Programming 5 03-05-2010 01:52 PM
how can I control the module functon? frankli Programming 5 05-31-2004 12:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration