Home | Forums | Reviews | Tutorials | Articles | Register | Search | | | LinuxQuestions.org > Forums > Non-*NIX Forums > Programming | How to create log file from script | | | 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 | Welcome to LinuxQuestions.org, a friendly and active Linux Community. You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today! Note that registered members see fewer ads, and ContentLink is completely disabled once you log in. Are you new to LinuxQuestions.org? Visit the following links: Site Howto | Site FAQ | Sitemap | Register Now If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here. Having a problem logging in? Please visit this page to clear all LQ-related cookies. Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use. Exclusive for LQ members, get up to 45% off per month. Click here for more info. | 08-09-2005, 07:28 AM | #1 | Senior Member Registered: Mar 2003 Location: Brisbane Queensland Australia Distribution: Custom Debian Live ISO's Posts: 1,291 Rep: | 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. | | | 08-09-2005, 10:42 AM | #2 | 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: | 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 | | | 08-09-2005, 09:42 PM | #3 | Senior Member Registered: Mar 2003 Location: Brisbane Queensland Australia Distribution: Custom Debian Live ISO's Posts: 1,291 Original Poster Rep: | Thanks for that i'll give that a go | | | 08-10-2005, 07:23 AM | #4 | Senior Member Registered: Sep 2004 Location: Sweden Distribution: Ubuntu, Debian Posts: 1,109 Rep: | 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 | | | 08-10-2005, 08:21 AM | #5 | Senior Member Registered: Jul 2004 Location: France Distribution: Arch Linux Posts: 1,897 Rep: | Or, from inside the script itself: exec 1>/path/to/logfile 2>&1 Yves. | | | 08-10-2005, 06:47 PM | #6 | Senior Member Registered: Mar 2003 Location: Brisbane Queensland Australia Distribution: Custom Debian Live ISO's Posts: 1,291 Original Poster Rep: | 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. | | | 08-11-2005, 03:29 AM | #7 | Senior Member Registered: Jul 2004 Location: France Distribution: Arch Linux Posts: 1,897 Rep: | 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. | | | 08-11-2005, 04:55 AM | #8 | Senior Member Registered: Mar 2003 Location: Brisbane Queensland Australia Distribution: Custom Debian Live ISO's Posts: 1,291 Original Poster Rep: | 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 | | | 09-27-2009, 03:40 PM | #9 | LQ Newbie Registered: Sep 2009 Posts: 1 Rep: | where should i put "script 2>&1 > logfile" ,"script 1> logouput 2> logerror" ? in script itself or cron job? tq | | | 09-28-2009, 04:42 AM | #10 | Senior Member Registered: Mar 2003 Location: Brisbane Queensland Australia Distribution: Custom Debian Live ISO's Posts: 1,291 Original Poster Rep: | Quote: Originally Posted by unknown_friends 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 | | | 02-20-2012, 08:48 AM | #11 | LQ Newbie Registered: Feb 2012 Posts: 2 Rep: | 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 | | | 03-26-2012, 06:33 AM | #12 | LQ Newbie Registered: Mar 2012 Posts: 23 Rep: | 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 | | | 03-27-2012, 07:39 AM | #13 | LQ Newbie Registered: Feb 2012 Posts: 2 Rep: | 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 | | | Posting Rules | You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | 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. | 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
| 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 | Latest Threads LQ News | |
0 Response to "How To Create A Log File In Shell Script"
Post a Comment