nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

shell script output to log file in /var/log/

This is a discussion on shell script output to log file in /var/log/ within the Shell scripting forums, part of the Development/Scripting category; Hi. I've created a script e.g., #!/bin/bash echo "test" script name is "test12.sh" How can i redirect its output to ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-11-2009, 08:00 PM
Senior Member
User
 
Join Date: Jul 2006
Posts: 145
Thanks: 0
Thanked 2 Times in 2 Posts
Rep Power: 4
asim.mcp is on a distinguished road
Smile shell script output to log file in /var/log/

Hi.

I've created a script e.g.,

#!/bin/bash
echo "test"

script name is "test12.sh"

How can i redirect its output to /var/log/test12.log file and also on screen?

Can i anyone help me please.



Regards
Asim
Reply With Quote
  #2 (permalink)  
Old 05-11-2009, 11:02 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,697
Thanks: 11
Thanked 243 Times in 183 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Try something as follows:
Code:
#!/bin/bash
LOGFILE=/var/log/test12.log 

log(){
    message="$@"
    echo $message
    echo $message >>$LOGFILE
}

log "This is a test!"
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #3 (permalink)  
Old 05-12-2009, 10:54 AM
Senior Member
User
 
Join Date: Jul 2006
Posts: 145
Thanks: 0
Thanked 2 Times in 2 Posts
Rep Power: 4
asim.mcp is on a distinguished road
Default

Dear

Thanks for your reply.

Actually i've a file called script.sh
I want to log its error and output into /var/log/test12.log file, same like other program logs are logged into /var/log/message email log in /var/log/maillog with hostname username and date/time etc.

Can you please let me know how can i do this?
Reply With Quote
  #4 (permalink)  
Old 05-12-2009, 01:37 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,697
Thanks: 11
Thanked 243 Times in 183 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Above script does the same. Otherwise use logger command:
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #5 (permalink)  
Old 05-12-2009, 06:29 PM
Senior Member
User
 
Join Date: Jul 2006
Posts: 145
Thanks: 0
Thanked 2 Times in 2 Posts
Rep Power: 4
asim.mcp is on a distinguished road
Default

I'm very confiuse.
Can you please a little bit guide me how can i use it with my script?
Reply With Quote
  #6 (permalink)  
Old 05-13-2009, 11:05 AM
Senior Member
User
 
Join Date: Jul 2006
Posts: 145
Thanks: 0
Thanked 2 Times in 2 Posts
Rep Power: 4
asim.mcp is on a distinguished road
Default

Dear

I've tried it and it works but i've to create another file.

e.g.,
Code:
#!/bin/bash

LOGFILE=test12.log

q=`/path/to/script.sh`


log(){

     echo "$(date +%c) $*" >>$LOGFILE
}

log "$q"

Last edited by nixcraft; 05-13-2009 at 04:02 PM.
Reply With Quote
  #7 (permalink)  
Old 05-13-2009, 04:04 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,697
Thanks: 11
Thanked 243 Times in 183 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

A better approach may be
HTML Code:
/path/to/script.sh > /path/to/log.file
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
Reply

Tags
linux , log a message , logger , shell script log file , shell scripting , unix


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
[Solved] Shell script for ftp the file vishal_titre Shell scripting 4 08-21-2009 03:50 PM
How to Redirect OutPut of Shell Command to a text file Hadi Shell scripting 1 04-29-2009 07:27 PM
Shell Script To Outputs File Permissions of Most Recently Modified File glen_4455 Shell scripting 1 08-25-2008 02:39 PM
shell script to search specific file from txt file inside zip file and extract it aasif.shaikh Shell scripting 2 05-31-2008 06:44 PM
error shell script no such file or directory /bin/sh Linux software 1 01-08-2006 08:34 PM


All times are GMT +5.5. The time now is 12:05 PM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2009 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38