LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-16-2013, 11:03 AM   #1
miros84
Member
 
Registered: Aug 2009
Location: Spain
Distribution: Debian stable, squeeze
Posts: 501

Rep: Reputation: 31
Create log when execute a bash script?


I have created a bash script with cronjob that run everyday.
Is there any way to create a log file and see later if everything was ok?
 
Old 01-16-2013, 11:05 AM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
this is a good guide for you to read over.

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html

In the linux world you can redirect your stdout, stderr or even stdin in most cases to wherever you would like. The link has some examples, talks about the differences and should be enough to get you there. If you have any specific questions please let me know.
 
1 members found this post helpful.
Old 01-16-2013, 11:45 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You can either add redirection in the crontab entry, e.g.
Code:
30 8 * * * /path/to/script.sh > /path/to/file.log 2>&1
or inside the script. If you put the following at the beginning of your script:
Code:
#!/bin/bash
#
exec > /path/to/file.log
exec 2> /path/to/file.err
the standard output and the standard error of all the subsequent commands will be redirected to their respective files. Use something like:
Code:
#!/bin/bash
#
exec > /path/to/file.log 2>&1
to redirect both to the same file. Hope this helps.
 
1 members found this post helpful.
Old 01-16-2013, 11:58 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
...also if you don't need complete execution logs you can syslog if all went OK:
Code:
# ...whatever
/some/command --with-args; RET=$?
case $RET in
 0) RET=OK;;
 *) RET=FAILED;;
esac
logger -t "scriptname" "Exit: ${RET}"
exit 0
 
1 members found this post helpful.
Old 01-16-2013, 12:33 PM   #5
miros84
Member
 
Registered: Aug 2009
Location: Spain
Distribution: Debian stable, squeeze
Posts: 501

Original Poster
Rep: Reputation: 31
I choose that option.
Code:
30 8 * * * /path/to/script.sh > /path/to/file.log 2>&1
I read the tutorial that Kustom42 linked, and I am not sure if I need something more then stdout. As I understand it will create a file and it will contain what you would see on the screen if you type the some command and execute it.

In terminal when I type some command and execute it, if there is a error, it print it in terminal.
Why actually I need stderr?
 
Old 01-16-2013, 01:03 PM   #6
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Quote:
Originally Posted by miros84 View Post
Why actually I need stderr?
It comes down to how much info you want, 99% of the time I only redirect standard output and forget about stderr. It's really something you have to decide based upon what you are going to be logging/redirecting and if you want to capture any stderr.

stderr would only be a problem with executing the script from BASH's side. So if the script was not executable it would generate a stderr saying that, however, if the script itself errors out and generates an error that it was programmed to that would actually be stdout as it is the output of the script.

I hope that clarifies a bit.
 
1 members found this post helpful.
Old 01-16-2013, 02:01 PM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Another thing to do from inside your bash script if you wish to write your own log information; such as

Quote:
completed blah-blah action with result x
or

Quote:
Daily cron job blah-blah started
You can send to your log and either create new or concatenate.

To create a new log file

Code:
echo "Daily cron job my-job.sh started." > /home/mylogin/daily-cron-ddmmyyyy.log
And then to concatenate more to that log

Code:
echo "Daily cron failed to complete archive of such-and-such directory. Result: %?" >> /home/mylogin/daily-cron-ddmmyyyy.log
 
Old 01-16-2013, 02:49 PM   #8
miros84
Member
 
Registered: Aug 2009
Location: Spain
Distribution: Debian stable, squeeze
Posts: 501

Original Poster
Rep: Reputation: 31
I understand it now. Thank you for your explication.

What about if I want to send this log to my gmail mail? It is very difficult that? Do I need to install mail server on my machine?
 
Old 01-19-2013, 11:16 AM   #9
miros84
Member
 
Registered: Aug 2009
Location: Spain
Distribution: Debian stable, squeeze
Posts: 501

Original Poster
Rep: Reputation: 31
I noted that log file remember only last error or last movements. What about if I need to remember last 5 movements? Is that possible?
 
Old 01-20-2013, 07:15 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Remember that
Code:
>  # creates a new file

>> # as above or appends if file already exists
If you only want the last N iterations it gets more fiddly.
I'd go with appending the date/time to the filename as above and get the logrotate tool to auto remove the older copies.


Re post #6; I'd recommend tracking stdout & stderr; you can't get those error msgs back, and the next run may not exhibit the same issue. You may also need(!) to know before the next run.
Your choice though ...
 
Old 01-21-2013, 08:59 AM   #11
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Cron automatically sends you a mail message IF there is any output to stderr or stdout.

If you want a detailed log of the commands/responses you need a "set -v" (verbose) mode for the shell. It will then output each command line as read. If you also want to know exactly what expansions the shell makes during execution, use "set -x". If you want both use "set -vx"

You can also redirect stderr/stdout if you don't want a mail message (redirecting removes any output, thus cron has nothing to send...)
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute bash script in C waqasdaar Programming 15 10-27-2015 02:54 PM
(Bash) Redirect all output from script to all.log and copy of errors to err.log hmsdefender Programming 5 03-05-2010 01:52 PM
Can't execute Bash script. zbe Linux - Software 4 10-17-2008 08:05 AM
bash log and execute function ninj4fly Linux - Newbie 5 09-20-2008 10:31 AM
Bash script for server log (namely var/log/messages) tenaciousbob Programming 17 05-24-2007 10:43 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:55 PM.

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
Open Source Consulting | Domain Registration