LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Create log when execute a bash script? (https://www.linuxquestions.org/questions/linux-software-2/create-log-when-execute-a-bash-script-4175445884/)

miros84 01-16-2013 11:03 AM

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?

Kustom42 01-16-2013 11:05 AM

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.

colucix 01-16-2013 11:45 AM

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.

unSpawn 01-16-2013 11:58 AM

...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


miros84 01-16-2013 12:33 PM

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?

Kustom42 01-16-2013 01:03 PM

Quote:

Originally Posted by miros84 (Post 4871566)
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.

rtmistler 01-16-2013 02:01 PM

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

miros84 01-16-2013 02:49 PM

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?

miros84 01-19-2013 11:16 AM

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?

chrism01 01-20-2013 07:15 PM

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 ...

jpollard 01-21-2013 08:59 AM

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...)


All times are GMT -5. The time now is 08:39 AM.