From my script output I want to capture ALL the logs data with error messages and redirect them all to log file.
I have script like below:
#!/bin/bash
(
echo " `date` : part 1 - start "
ssh -f admin@server.com 'bash /www/htdocs/server.com/scripts/part1.sh logout exit'
echo " `date` : sleep 120"
sleep 120
echo " `date` : part 2 - start"
ssh admin@server.com 'bash /www/htdocs/server.com/scripts/part2.sh logout exit'
echo " `date` : part 3 - start"
ssh admin@server.com 'bash /www/htdocs/server.com/scripts/part3.sh 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 only see this what I put after echo
command.
How to check in logs that SSH
command was successful?
I need to gather all logs. I need this to monitor result of every command in my script, to better analyse what's going on while script fails.
Best Answer
I generally put something similar to the following at the beginning of every script (especially if it'll run as a daemon):
Explanation:
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.
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.
exec 1>log.out 2>&1
Redirect
stdout
to filelog.out
then redirectstderr
tostdout
. Note that the order is important when you want them going to the same file.stdout
must be redirected beforestderr
is redirected tostdout
.From then on, to see output on the console (maybe), you can simply redirect to
&3
. For example,will go to wherever
stdout
was directed, presumably the console, prior to executing line 3 above.