LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-05-2014, 01:32 AM   #1
VijayB
LQ Newbie
 
Registered: Jun 2014
Posts: 21

Rep: Reputation: Disabled
How to generate a log file of the shell file being Executed


Hi,

I have a shell file with the name say test.sh. I want that whenever that shell file is executed the logs of execution of that shell file should be saved automatically to a directory say Logs on my home path , with date & time.

Is there something that i can add in the starting of that shell script that would solve this purpose?

PS: I don't want my script calling another script , just want to add something to my current script.

Thanks,

BR\\Vijay
 
Old 07-05-2014, 01:59 AM   #2
kooru
Senior Member
 
Registered: Sep 2012
Posts: 1,385

Rep: Reputation: 273Reputation: 273Reputation: 273
You could add a thing like this:

Code:
TIMEXEC=$(date)
LOG=/tmp/logtest
echo "Starting script: $TIMEXEC" >> $LOG
 
Old 07-05-2014, 02:13 AM   #3
VijayB
LQ Newbie
 
Registered: Jun 2014
Posts: 21

Original Poster
Rep: Reputation: Disabled
Hi Kooru,

Thanks for the code but there are no log files generated with this code.

I tried it with simple shell script:

#!/bin/bash


TIMEXEC=$(date)
LOG=/tmp/logtest
echo "Starting script: $TIMEXEC" >> $LOG

echo ok
date


but nothing was created in LOG directory. Can you please look into it.

BR\\Vijay
 
Old 07-05-2014, 03:37 AM   #4
kooru
Senior Member
 
Registered: Sep 2012
Posts: 1,385

Rep: Reputation: 273Reputation: 273Reputation: 273
The log is saved into /tmp directory, into a file named logtest, in according with LOG variable.
It works because I just tried now.
You can try changing the path with your home and/or write set -x under #!bin/bash to turn on the debug and to undesrtand the problem.

Last edited by kooru; 07-05-2014 at 03:39 AM.
 
Old 07-05-2014, 03:54 AM   #5
VijayB
LQ Newbie
 
Registered: Jun 2014
Posts: 21

Original Poster
Rep: Reputation: Disabled
Thanks kooru , i can see the time of the execution of the script in logtestfile. But the requirement is that whatever was executed by the shell file ,that should have been logged in a file & name of the file should be something like log_date_time.


Starting script: Sat, Jul 05, 2014 11:37:44 AM
Starting script: Sat, Jul 05, 2014 11:39:05 AM
Starting script: Sat, Jul 05, 2014 11:40:40 AM
Starting script: Sat, Jul 05, 2014 11:41:45 AM
Starting script: Sat, Jul 05, 2014 1:18:34 PM


The file should contain all the things that user did while running the shell script. Can you please help me with the rwequirement?

BR\\Vijay
 
Old 07-05-2014, 04:56 AM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Perhaps you are looking for something like:
Code:
#!/bin/bash
if [ "$LOG" == "" ]; then
   export LOG=1
   date >> log & exec bash -x $0 $* 2>>log
   exit $?
fi
# log should be going
echo testing1
echo testing2
This will create a log file looking like:
Code:
Sat Jul  5 04:45:23 EDT 2014
+ '[' 1 == '' ']'
+ echo testing1
+ echo testing2
You can change the detail of the log by changing the "-x" on the bash command line to "-vx".

Warning: because the bash command redirects stderr, any error messages will be redirected to the log.

The first line (following the date) will always show - it is the test to whether to create a log output or not, and is caused by the recursive use of bash.

The purpose of the "exec bash..." is to reduce the depth of active processes. Using exec is not required, the only thing saved by its use is the parent shell (that starts the subprocess) doesn't have to actually interpret/execute the "exit $?" command. The parent shell only sets up the log (with the date) and then restarts the shell with the log file getting the commands (from the "-x") and all the error messages.

One last thing you might like to have... The "LOG" environment variable must not be defined by default. Its value can be set to anything by the script. If the value is set to the log file name, then it is possible for the script to use that to put its own messages in the log (things like "echo 'this shouldn't happen' >>$LOG").

The log will have the date as the first line, then all command recording will have a "+" (or ++ if you use the "-vx" options). So any special messages will be in there twice, once with the "+" followed by the command, then a line with the special message. This could make it easy to quickly identify special messages.

One final warning - the destination of the log will need a LOT of maintenance. ESPECIALLY if a script logs like this and is put into a loop by a different script (that doesn't set the LOG variable)... You could get thousands of log files rather quickly.

Last edited by jpollard; 07-05-2014 at 05:11 AM.
 
1 members found this post helpful.
Old 07-05-2014, 05:22 AM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530
Why not just use 'logger'? It provides syslog-like time stamps without having to format date strings.
 
Old 07-05-2014, 06:17 AM   #8
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Quote:
Originally Posted by unSpawn View Post
Why not just use 'logger'? It provides syslog-like time stamps without having to format date strings.
It doesn't appear to satisfy the requirement of logging the commands executed... Useful for adding entries to syslog though.
 
Old 07-05-2014, 06:43 AM   #9
kooru
Senior Member
 
Registered: Sep 2012
Posts: 1,385

Rep: Reputation: 273Reputation: 273Reputation: 273
Quote:
Originally Posted by VijayB View Post
The file should contain all the things that user did while running the shell script. Can you please help me with the rwequirement?
BR\\Vijay
Ah ok, now it's clear.
I just thought the starting date.
jpollard's solution is what you want.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How do you get cdrdao to generate a log file? Holering Linux - Software 2 05-24-2013 06:22 AM
request to generate a awk file for NS2 .tr file vin.18 Linux - Networking 1 02-09-2013 10:07 AM
Shell script to monitor the log file & kill the process if log is not updating. milu_k Programming 5 07-19-2012 09:23 AM
wanna generate log file of inode creation under a directory qamarhafeez Linux - Server 1 01-20-2012 04:11 PM
Perl that generate log file casualzone Programming 1 12-21-2011 09:58 AM


All times are GMT -5. The time now is 10:30 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration