Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to capture ALL the logs data, both with error messages, from my script output, and redirect them all to log file.

I have script like below:

echo " `date` : part 1 - start "
ssh -f 'bash /www/htdocs/ logout exit'
echo " `date` : sleep 120"
sleep 120
echo " `date` : part 2 - start"
ssh 'bash /www/htdocs/ logout exit'
echo " `date` : part 3 - start"
ssh 'bash /www/htdocs/ logout exit'
echo " `date` : END"
) | tee -a /home/scripts/cron/logs

I want to see all actions in file "/home/scripts/cron/logs"

But i see only this what i put after echo command.

How to check in logs is SSH command was successful?

I need to gather all the logs data. I need this to monitor result of every command in my script, to better analyse what's going on while script fail.

share|improve this question
You can also use the "script" to perform this. See this post: [here][1] [1]:… – xX0v0Xx Jun 3 '14 at 12:57
up vote 28 down vote accepted

I generally put something similar to the following at the beginning of every script (especially if it'll run as a daemon):

exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>log.out 2>&1
# Everything below will go to the file 'log.out':


  1. exec 3>&1 4>&2

    Saves file descriptors so they can be restored to whatever they were before redirection or used themselves to output to whatever they were before the following redirect.

  2. trap 'exec 2>&4 1>&3' 0 1 2 3

    Restore file descriptors for particular signals. Not generally necessary since they should be restored when the sub-shell exits.

  3. exec 1>log.out 2>&1

    Redirect stdout to file log.out then redirect stderr to stdout. Note that the order is important when you want them going to the same file. stdout must be redirected before stderr is redirected to stdout.

From then on, to see output on the console (maybe), you can simply redirect to &3. For example,

echo "$(date) : part 1 - start" >&3

will go to wherever stdout was directed, presumably the console, prior to executing line 3 above.

share|improve this answer
<<niceroot, Thanks! I added those threelines (exec,trap,exec) at the beginning of the script and I am able to get both stdout and stderr. But One issue: I have seen "File descriptor 3 (pipe:XXX) leaked on some_command" ... Please let me know how can I avoid getting these errors. I using busybox based sh in custom board. – kumar Nov 16 '11 at 15:52
Great KungFu here !!! - Even better is to use trap 'exec 2>&4 1>&3' 0 1 2 3 RETURN. The RETURN pseudo sigspec restore file descriptors each time a shell function or a script executed with the . or source builtins finishes executing. For this (and for other reasons) in my scripts I add as the last 2 lines : return & exit 0 - Just my 2 cents, kudos to you @nicerobot ! – DavAlPi Mar 5 '15 at 15:25
There's good amount of detail on logging for shell scripts via global varaibles of shell. We can emulate the similar kind of logging in shell script: The post has details on introdducing log levels like INFO , DEBUG, ERROR. Tracing details like script entry, script exit, function entry, function exit. – Piyush Chordia Mar 10 at 4:56

to get the ssh output to your logfile, you have to redirect stderr to stdout. you can do this by appending 2>&1 after your bash script.

it should look like this:

) 2>&1 | tee ...

when this does not display the mesages in the right order, try to add another subshell:

) 2>&1) | tee ...
share|improve this answer

As I read your question, you don't want to log the output, but the entire sequence of commands, in which case, the other answers won't help you.

Invoke shell scripts with -x to output everything:

sh -x

Log to the file you want with:

sh -x >> /home/scripts/cron/logs

share|improve this answer
+1 for -x option – Coc Jan 26 '13 at 11:35

In bash, you can put set -x and it will print off every command that it executes (and the bash variables) after that. You can turn it off with set +x.

If you want to be paranoid, you can put set -o errexit in your script. This means the script will fail and stop if one command returned a non-zero exit code, which is the unix standard way to signal that something went wrong.

If you want to get nicer logs, you should look at ts in the moreutils debian/ubuntu package. It will prefix each line with a timestamp and print it out. So you can see when things were happening.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.