banner



How To Create A Log File In Shell Script

Home Forums Reviews Tutorials Articles Register
Go Back LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
Reload this Page How to create log file from script
User Name
Password

Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


Notices

Reply

Search this Thread
Old 08-09-2005, 07:28 AM #1

fotoguy

Senior Member

Registered: Mar 2003

Location: Brisbane Queensland Australia

Distribution: Custom Debian Live ISO's

Posts: 1,291

Rep: Reputation: 62

How to create log file from script


I'm currently making a bash script to untar and configure apache, mysql php4 and a few other programs, but i'm having trouble with the script and need to be able to find where the errors are.

But the problem is so much output information comes up on the screen and i can only move up a few screens lengths, is there some code i can put into the script that create a log file from the screen output.

Old 08-09-2005, 10:42 AM #2

macemoneta

Senior Member

Registered: Jan 2005

Location: Manalapan, NJ

Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android

Posts: 4,593

Blog Entries: 2

Rep: Reputation: 344 Reputation: 344 Reputation: 344 Reputation: 344

When you run your script, redirect the output to a file:

somescript &> somelogfile

If you need to see the output while it's writing to the file, you can 'tail -F' the file, or use tee:

somescript 2>&1 | tee somelogfile

Old 08-09-2005, 09:42 PM #3

fotoguy

Senior Member

Registered: Mar 2003

Location: Brisbane Queensland Australia

Distribution: Custom Debian Live ISO's

Posts: 1,291

Original Poster

Rep: Reputation: 62

Thanks for that i'll give that a go

Old 08-10-2005, 07:23 AM #4

Ephracis

Senior Member

Registered: Sep 2004

Location: Sweden

Distribution: Ubuntu, Debian

Posts: 1,109

Rep: Reputation: 50

To put output in logfile:
script > logfile

To put output and errors in logfile:
script 2>&1 > logfile

To separate:
script 1> logouput 2> logerror

Old 08-10-2005, 08:21 AM #5

theYinYeti

Senior Member

Registered: Jul 2004

Location: France

Distribution: Arch Linux

Posts: 1,897

Rep: Reputation: 66

Or, from inside the script itself:
exec 1>/path/to/logfile 2>&1

Yves.

Old 08-10-2005, 06:47 PM #6

fotoguy

Senior Member

Registered: Mar 2003

Location: Brisbane Queensland Australia

Distribution: Custom Debian Live ISO's

Posts: 1,291

Original Poster

Rep: Reputation: 62

Thanks for the replies everyone, macemoneta solution worked like a treat. I'll try the others as well, print a copy of this page to have as a reference, which will come in hand since i've Just started learning PHP programming.

theYinYeti, exec 1>/path/to/logfile 2>&1

I take it this goes at the very top of the script.

Old 08-11-2005, 03:29 AM #7

theYinYeti

Senior Member

Registered: Jul 2004

Location: France

Distribution: Arch Linux

Posts: 1,897

Rep: Reputation: 66

Oops sorry! I overlooked the "PHP" aspect of your first post
What I wrote only applies to shell (bash at least) scripts, not PHP. Other solutions do apply to any scripts however, because they happen in the bash side of things even if the script itself is PHP.

Yves.

Old 08-11-2005, 04:55 AM #8

fotoguy

Senior Member

Registered: Mar 2003

Location: Brisbane Queensland Australia

Distribution: Custom Debian Live ISO's

Posts: 1,291

Original Poster

Rep: Reputation: 62

theYinYeti

No you were right the first time, the script i'm working on is for a bash script. At college we are lerning how to install Apache, Mysql and PHP4 on one machine, and the other machine already has a webserver running were we upload are webpages and test a PHP scripts.

I just wanted to make a script to help with the installation process since it will be part of the practical test coming up in a few weeks. And since php can run linux console commands, I thought these would be a nice addition

Old 09-27-2009, 03:40 PM #9

unknown_friends

LQ Newbie

Registered: Sep 2009

Posts: 1

Rep: Reputation: 0

where should i put "script 2>&1 > logfile" ,"script 1> logouput 2> logerror" ? in script itself or cron job?

tq

Old 09-28-2009, 04:42 AM #10

fotoguy

Senior Member

Registered: Mar 2003

Location: Brisbane Queensland Australia

Distribution: Custom Debian Live ISO's

Posts: 1,291

Original Poster

Rep: Reputation: 62

Quote:

Originally Posted by unknown_friends View Post

where should i put "script 2>&1 > logfile" ,"script 1> logouput 2> logerror" ? in script itself or cron job?

tq

Depends, you can run it from inside the script, just place it at the very top of the script.

Quote:

exec 1>/path/to/logfile 2>&1

Or you can run it when you run a cron job from crontab like this.

Quote:

* 3 * * * /path/to/script 2>&1 > /path/to/logfile.txt

Old 02-20-2012, 08:48 AM #11

manos

LQ Newbie

Registered: Feb 2012

Posts: 2

Rep: Reputation: Disabled

self made log


This is a quite old thread but i will give my two cents anyway.
Many times I create a function that writes a logfile and I call it whenever I want:

Code:

logprint() {     echo "$(date +%T): $*" >> $LOGFILE }

I start my script with writing the date:

Code:

echo "---------- Log of $0 for $LOGDATE ----------" >> $LOGFILE

and then I call logprint whenever i want in the script:

Code:

... logprint "$1 : successfully compressed, Size: $thissize" ... logprint "$0: Checking for backups older than $DAYSTOKEEP days to delete"

So I get a logfile that is devided into days and each line has also the time.


Last edited by manos; 02-20-2012 at 08:49 AM. Reason: vocabulary

Old 03-26-2012, 06:33 AM #12

yashvanth

LQ Newbie

Registered: Mar 2012

Posts: 23

Rep: Reputation: Disabled

Post help needed


hi all
i am running the script and a cron to take the backup every day at some time.
i like to create logfile automatically all the time when cron runs and logfile name shpuld contain the date so we can easily locate it.
and also like to write into output of terminal (about backup process) into newly created log file

thank you

Old 03-27-2012, 07:39 AM #13

manos

LQ Newbie

Registered: Feb 2012

Posts: 2

Rep: Reputation: Disabled

Hello,

to create a new file every time the cronjob runs you can add it in your script.
For example

Code:

FILEDATE=`date +%F` LOGFILE=/var/log/backup.$FILEDATE.log touch $LOGFILE  echo "message $variable message"  >> $LOGFILE

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 Off

HTML code is Off


Forum Rules


Similar Threads
Thread Thread Starter Forum Replies Last Post
starting script, init by copy/create file in (samba) folder? muab Linux - General 7 06-22-2005 07:02 PM
Create text file with a script zael Programming 3 06-02-2004 04:27 AM
Run script during file copy or create in directory neranjana Linux - General 1 01-13-2004 07:57 AM
perl script to create a backwards file?! WorldBuilder Programming 16 10-30-2003 11:05 PM
Samba: how to create a logon script file when i add a system user. heero82 Linux - Software 2 06-19-2003 09:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:05 AM.


Contact Us - Advertising Info - Rules - Privacy - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -

Main Menu
  • Linux Forum
  • Search
  • LQ Tags
  • Linux Tutorials
  • LQ Job Marketplace
  • LQ Deals
  • Linux Wiki
  • Distro Reviews
  • Book Reviews
  • Download Linux
  • Social Groups
  • LQ Blogs

(Con't)

Advertisement

My LQ
  • Login
  • Register
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
  • LQ Calendar
  • LQ Rules
  • LQ Sitemap
  • Site FAQ
  • View New Posts
  • View Latest Posts
  • Zero Reply Threads
  • LQ Wiki Most Wanted
  • Jeremy's Blog
  • Report LQ Bug
Syndicate
RSS1  Latest Threads
RSS1  LQ News

Twitter: @linuxquestions

How To Create A Log File In Shell Script

Source: https://www.linuxquestions.org/questions/programming-9/how-to-create-log-file-from-script-351459/

Posted by: kerbereaceforehis.blogspot.com

Related Posts

0 Response to "How To Create A Log File In Shell Script"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel