Create Log File in ksh itself


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create Log File in ksh itself
# 1  
Old 06-08-2012
CPU & Memory Create Log File in ksh itself

Hi,

I want to create a log file for a running ksh , and the log file absolute path I want to give in ksh itself.
To elaborate this - Say I have a ksh - timer.ksh and I want to create a log timer_log.log when I run, to trace this. I am aware of the fact that this can be done using redirection parameters. Something like this while executing this script:

Code:
ksh timer.ksh 2>&1 timer_log.log

What I want to know if there is possibility that I could put this 'timer_log.log' file path in 'timer.ksh' itself and then I simply have to run

Code:
ksh timer.ksh

I might be asking very unpractical question here Smilie .
Could you please advise if this is possible.

Thanks,
Vinay

Last edited by radoulov; 06-08-2012 at 05:53 PM..
# 2  
Old 06-08-2012
Yes, you can. The exec builtin can redirect file descriptors for an entire script, just like you'd redirect for external programs.

Code:
#!/bin/ksh

# Redirect the current stderr into stdout
exec 2>&1
# Redirect the current stdout into the log file
exec 1>timer_log.log

# Should end up in timer_log.log
echo "This is a message"
# Should also end up in timer_log.log
echo "This is an error message!" >&2

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 06-11-2012
Thanks Corona !

I was simply using 2>&1 to redirect previously (was not aware of exec).

I have not tried this solution yet , however I assume after using this nothing will printed on console when I run that script.

Is it also possible to have it redirected in the log file ( for future reference ) as well as have it printed on the console also to get a quick view.

Thanks,
Vinay
# 4  
Old 06-11-2012
You cannot print to one file and get it to appear in two places without using an external program like tee or the like to make one line print twice. So not easily, not cleanly, and the output you get won't necessarily be in the order you expect.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 06-11-2012
Quote:
Originally Posted by vinay4889
Is it also possible to have it redirected in the log file ( for future reference ) as well as have it printed on the console also to get a quick view.
One common way is to run the script command before launching the commands you want the output to be both displayed and saved.

eg:
Code:
$ script
Script started, file is typescript
$ date
Tue Jun 12 02:04:59 CEST 2012
$ exit
Script done, file is typescript
$ cat typescript
Script started on Tue Jun 12 02:04:57 2012
$ date
Tue Jun 12 02:04:59 CEST 2012
$ exit

Script done on Tue Jun 12 02:05:01 2012
$

This User Gave Thanks to jlliagre For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ksh: How to write the log file simultaneously when .sql file executes by UNIX process

Hello Team- we would like to implement an approach which has to write the log file simultaneously when .sql file is executing by Unix process. At present,it is writing the log file once the process is completed. I've tested the current process with the below approaches and none of them... (1 Reply)
Discussion started by: Hima_B
1 Replies

2. Shell Programming and Scripting

How to create a log file that simply shows the name of a file and what directory it was moved to.

Newbie...Thank you for your help. I am creating my first script that simply generates subdirectories to organize video, music, and text files within those subdirectories from my current working directory. PROBLEM: I am trying to write a log file that contains, for each file, the original file... (0 Replies)
Discussion started by: BartleDoo
0 Replies

3. Shell Programming and Scripting

create csv in ksh from tricky log file

hi guys, trying to create a csv from a tricky log file in ksh, using 'awk '{print $1" "$14" "$15" "$16" "$17" "$18" "$19}' >> $TMP_FILE' on another set of files I have an output file with hundreds of lines in which looks like so: ABC_DEFGHI_16_JKLMNP11.20101115_095412_374.log:09:54:29.579... (3 Replies)
Discussion started by: rich@ardz
3 Replies

4. Shell Programming and Scripting

ksh script to create a generic csv file from different source formats

Hi all, I have a requirement to create a "superset" file out of a number of different sources with some different and some same columns. We intend to have a manually updateable SuperSetCols.csv which would look like "ColA","ColB","ColC","ColD","ColE","ColF","ColG" so someday we may add... (3 Replies)
Discussion started by: Leedor
3 Replies

5. Shell Programming and Scripting

Rename a log file to old, then create a new log file.

Hi all, I have about 15 directories all with exactly the same structure. I have a file in each of them called log.txt. This file sits in /home/ftp/*usernamehere*/ftptransfer/log/ Username here is the only change in each of the 15 directories. I want to create a SIMPLE shell script that... (5 Replies)
Discussion started by: mokachoka
5 Replies

6. Shell Programming and Scripting

How to create file in ksh scripting with permission(rw-rw-rw)

Hi, Please provide your inputs.. Thanks in Advance, Mansa (1 Reply)
Discussion started by: mansa
1 Replies

7. UNIX for Dummies Questions & Answers

create log file

Accept command line input of a directory keep a .logfile of the contents of the directory inputed if a .logfile does not exist create one for the file that is missing a .logfile and create a message stating .logfile was updated if all .logfiles are there display message stating all files are... (1 Reply)
Discussion started by: knc9233
1 Replies

8. Shell Programming and Scripting

How to create file execution in KSH shell

Dear all, I'm new in Unix system, I need help about how to create execution file. for example in my system i have file fg* when i open fg file i get : cmd='basename$0' $cmd "$@" how to create file like that (execution file) in KSH shell thank you for your help heru (4 Replies)
Discussion started by: heru_90
4 Replies

9. UNIX for Dummies Questions & Answers

Need to create .bat file to store log file

Hi guys. Can someone point me to a resource that explains this? Basically these are websphere logs that need to be stored daily, I'm on sunOS 5.8. Each new file stored could have the current time as its filename. The script could be run on a cron which I can set up. I'm just not sure how to write... (0 Replies)
Discussion started by: dirtybrown
0 Replies

10. Shell Programming and Scripting

Posting to a log file in KSH

Hello, I am writing this script in ksh (consisting of various functions such as -> read_file, write_file, pront_output and initialise) which basically process input files based on the parameters it contains. My task now is to write any error conditions trapped by any of the function above to... (2 Replies)
Discussion started by: sidamin810
2 Replies
Login or Register to Ask a Question