Append to the top in a log file

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a crontab script which monitor a DR process between 2 machines and this script generate a log file. What i have been asked to do is basically append the new log generated on the top of the previous (as log i use the same file name) and not on the bottom.
I've seen already few options, but all my tentative failed.
I tried with
cat $LOGFILE >> $TEMPLOG
rm $LOGFILE
mv -i $TEMPLOG $LOGFILE
and also with
cat - $LOGFILE > $TEMPLOG && mv $TEMPLOG $LOGFILE
the variable $LOGFILE is where the script append every single statement of the process.
Thanks :)
Basically want I'd like to to do is generate the right log with on top the last run before it is sent by mail.
DATE=`date "+%d%m%y_%H%M"`
PRIMARY_HOSTNAME=`hostname`
LOGFILE=/dba/logs/monitor_sync_FM2.log
TEMPLOG=/dba/logs/monitor_sync_LOG.log
SERVER=`hostname`
SITE=mycompany
EMAILTO="email@company.com"
DBOPS="oracle@$SERVER.$SITE"
export PRIMARY_HOSTNAME LOGFILE TEMPLOG SERVER SITE EMAILTO DBOPS DATE
echo "nn### monitor DR sync started @ `date` ###" >> $LOGFILE
echo "Running SQL command to verify latest SCN.." >> $LOGFILE
echo "The current SCN of the Primary DB server is: $PRIMARY_CURRENT_SCN" >> $LOGFILE
echo "Connecting now to the secondary standby database server..." >> $LOGFILE
SECONDARY_CURRENT_SCN=`ssh oracle@xxx.xxx.xxx.xxx /home/oracle/script_sync2.sh` >> $LOGFILE
export SECONDARY_CURRENT_SCN
echo "Secondary SCN output returned as: $SECONDARY_CURRENT_SCN" >> $LOGFILE
grep ORA- /dba/scripts/output.txt
if [ $? = 0 ]; then
echo "Remote ssh command to Secondary server failed..Exiting" >> $LOGFILE
echo "### monitor DR sync failed @ `date` ###" >> $LOGFILE
echo "PROBLEM" >> $LOGFILE
mailx -r $DBOPS -s "PROBLEM" $EMAILTO < $LOGFILE
exit
else
echo "The current SCN of the Secondary DB server is: $SECONDARY_CURRENT_SCN" >> $LOGFILE
DIFF=`expr $PRIMARY_CURRENT_SCN - $SECONDARY_CURRENT_SCN` ; export DIFF
if [ $PRIMARY_CURRENT_SCN -ne $SECONDARY_CURRENT_SCN ]; then
echo "The difference is $DIFF" >> $LOGFILE
if [ `echo $DIFF` -gt 3 ]; then
echo "Log Gap: $DIFF" >> $LOGFILE
echo "PROBLEM" >> $LOGFILE
echo "### script finished @ `date` ###nn" >> $LOGFILE
mailx -r $DBOPS -s "PROBLEM" $EMAILTO < $LOGFILE
else
echo "SUCCESS" >> $LOGFILE
mailx -r $DBOPS -s "SUCCESS" $EMAILTO < $LOGFILE
echo "Log Gap: $DIFF" >> $LOGFILE
fi
else
echo "Log Gap: $DIFF" >> $LOGFILE
echo "SUCCESS" >> $LOGFILE
mailx -r $DBOPS -s "SUCCESS" $EMAILTO < $LOGFILE
fi
fi
shell-script scripting
 |Â
show 2 more comments
up vote
0
down vote
favorite
I have a crontab script which monitor a DR process between 2 machines and this script generate a log file. What i have been asked to do is basically append the new log generated on the top of the previous (as log i use the same file name) and not on the bottom.
I've seen already few options, but all my tentative failed.
I tried with
cat $LOGFILE >> $TEMPLOG
rm $LOGFILE
mv -i $TEMPLOG $LOGFILE
and also with
cat - $LOGFILE > $TEMPLOG && mv $TEMPLOG $LOGFILE
the variable $LOGFILE is where the script append every single statement of the process.
Thanks :)
Basically want I'd like to to do is generate the right log with on top the last run before it is sent by mail.
DATE=`date "+%d%m%y_%H%M"`
PRIMARY_HOSTNAME=`hostname`
LOGFILE=/dba/logs/monitor_sync_FM2.log
TEMPLOG=/dba/logs/monitor_sync_LOG.log
SERVER=`hostname`
SITE=mycompany
EMAILTO="email@company.com"
DBOPS="oracle@$SERVER.$SITE"
export PRIMARY_HOSTNAME LOGFILE TEMPLOG SERVER SITE EMAILTO DBOPS DATE
echo "nn### monitor DR sync started @ `date` ###" >> $LOGFILE
echo "Running SQL command to verify latest SCN.." >> $LOGFILE
echo "The current SCN of the Primary DB server is: $PRIMARY_CURRENT_SCN" >> $LOGFILE
echo "Connecting now to the secondary standby database server..." >> $LOGFILE
SECONDARY_CURRENT_SCN=`ssh oracle@xxx.xxx.xxx.xxx /home/oracle/script_sync2.sh` >> $LOGFILE
export SECONDARY_CURRENT_SCN
echo "Secondary SCN output returned as: $SECONDARY_CURRENT_SCN" >> $LOGFILE
grep ORA- /dba/scripts/output.txt
if [ $? = 0 ]; then
echo "Remote ssh command to Secondary server failed..Exiting" >> $LOGFILE
echo "### monitor DR sync failed @ `date` ###" >> $LOGFILE
echo "PROBLEM" >> $LOGFILE
mailx -r $DBOPS -s "PROBLEM" $EMAILTO < $LOGFILE
exit
else
echo "The current SCN of the Secondary DB server is: $SECONDARY_CURRENT_SCN" >> $LOGFILE
DIFF=`expr $PRIMARY_CURRENT_SCN - $SECONDARY_CURRENT_SCN` ; export DIFF
if [ $PRIMARY_CURRENT_SCN -ne $SECONDARY_CURRENT_SCN ]; then
echo "The difference is $DIFF" >> $LOGFILE
if [ `echo $DIFF` -gt 3 ]; then
echo "Log Gap: $DIFF" >> $LOGFILE
echo "PROBLEM" >> $LOGFILE
echo "### script finished @ `date` ###nn" >> $LOGFILE
mailx -r $DBOPS -s "PROBLEM" $EMAILTO < $LOGFILE
else
echo "SUCCESS" >> $LOGFILE
mailx -r $DBOPS -s "SUCCESS" $EMAILTO < $LOGFILE
echo "Log Gap: $DIFF" >> $LOGFILE
fi
else
echo "Log Gap: $DIFF" >> $LOGFILE
echo "SUCCESS" >> $LOGFILE
mailx -r $DBOPS -s "SUCCESS" $EMAILTO < $LOGFILE
fi
fi
shell-script scripting
Why does it needed to be added to the beginning of the file?
â Ignacio Vazquez-Abrams
Nov 6 '17 at 16:02
@IgnacioVazquez-Abrams Because the will be sent to a machine with a monitoring agent which check from the top of the file, so i'd like to have the last run/checking on the top
â FaMontyN2
Nov 6 '17 at 16:04
@MarkPlotnick sorry, i don't get your comment
â FaMontyN2
Nov 6 '17 at 16:05
Does each line of the logfile have a timestamp? If so, and you want newer portions of the logfile added at the top, does that mean that the timestamps will not all be in order?
â Mark Plotnick
Nov 6 '17 at 16:14
@MarkPlotnick Yes, there is a time stamp on the top of the portion of log generate every run. And i don't mind that won't be in order, it won't be a problem.
â FaMontyN2
Nov 6 '17 at 16:16
 |Â
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a crontab script which monitor a DR process between 2 machines and this script generate a log file. What i have been asked to do is basically append the new log generated on the top of the previous (as log i use the same file name) and not on the bottom.
I've seen already few options, but all my tentative failed.
I tried with
cat $LOGFILE >> $TEMPLOG
rm $LOGFILE
mv -i $TEMPLOG $LOGFILE
and also with
cat - $LOGFILE > $TEMPLOG && mv $TEMPLOG $LOGFILE
the variable $LOGFILE is where the script append every single statement of the process.
Thanks :)
Basically want I'd like to to do is generate the right log with on top the last run before it is sent by mail.
DATE=`date "+%d%m%y_%H%M"`
PRIMARY_HOSTNAME=`hostname`
LOGFILE=/dba/logs/monitor_sync_FM2.log
TEMPLOG=/dba/logs/monitor_sync_LOG.log
SERVER=`hostname`
SITE=mycompany
EMAILTO="email@company.com"
DBOPS="oracle@$SERVER.$SITE"
export PRIMARY_HOSTNAME LOGFILE TEMPLOG SERVER SITE EMAILTO DBOPS DATE
echo "nn### monitor DR sync started @ `date` ###" >> $LOGFILE
echo "Running SQL command to verify latest SCN.." >> $LOGFILE
echo "The current SCN of the Primary DB server is: $PRIMARY_CURRENT_SCN" >> $LOGFILE
echo "Connecting now to the secondary standby database server..." >> $LOGFILE
SECONDARY_CURRENT_SCN=`ssh oracle@xxx.xxx.xxx.xxx /home/oracle/script_sync2.sh` >> $LOGFILE
export SECONDARY_CURRENT_SCN
echo "Secondary SCN output returned as: $SECONDARY_CURRENT_SCN" >> $LOGFILE
grep ORA- /dba/scripts/output.txt
if [ $? = 0 ]; then
echo "Remote ssh command to Secondary server failed..Exiting" >> $LOGFILE
echo "### monitor DR sync failed @ `date` ###" >> $LOGFILE
echo "PROBLEM" >> $LOGFILE
mailx -r $DBOPS -s "PROBLEM" $EMAILTO < $LOGFILE
exit
else
echo "The current SCN of the Secondary DB server is: $SECONDARY_CURRENT_SCN" >> $LOGFILE
DIFF=`expr $PRIMARY_CURRENT_SCN - $SECONDARY_CURRENT_SCN` ; export DIFF
if [ $PRIMARY_CURRENT_SCN -ne $SECONDARY_CURRENT_SCN ]; then
echo "The difference is $DIFF" >> $LOGFILE
if [ `echo $DIFF` -gt 3 ]; then
echo "Log Gap: $DIFF" >> $LOGFILE
echo "PROBLEM" >> $LOGFILE
echo "### script finished @ `date` ###nn" >> $LOGFILE
mailx -r $DBOPS -s "PROBLEM" $EMAILTO < $LOGFILE
else
echo "SUCCESS" >> $LOGFILE
mailx -r $DBOPS -s "SUCCESS" $EMAILTO < $LOGFILE
echo "Log Gap: $DIFF" >> $LOGFILE
fi
else
echo "Log Gap: $DIFF" >> $LOGFILE
echo "SUCCESS" >> $LOGFILE
mailx -r $DBOPS -s "SUCCESS" $EMAILTO < $LOGFILE
fi
fi
shell-script scripting
I have a crontab script which monitor a DR process between 2 machines and this script generate a log file. What i have been asked to do is basically append the new log generated on the top of the previous (as log i use the same file name) and not on the bottom.
I've seen already few options, but all my tentative failed.
I tried with
cat $LOGFILE >> $TEMPLOG
rm $LOGFILE
mv -i $TEMPLOG $LOGFILE
and also with
cat - $LOGFILE > $TEMPLOG && mv $TEMPLOG $LOGFILE
the variable $LOGFILE is where the script append every single statement of the process.
Thanks :)
Basically want I'd like to to do is generate the right log with on top the last run before it is sent by mail.
DATE=`date "+%d%m%y_%H%M"`
PRIMARY_HOSTNAME=`hostname`
LOGFILE=/dba/logs/monitor_sync_FM2.log
TEMPLOG=/dba/logs/monitor_sync_LOG.log
SERVER=`hostname`
SITE=mycompany
EMAILTO="email@company.com"
DBOPS="oracle@$SERVER.$SITE"
export PRIMARY_HOSTNAME LOGFILE TEMPLOG SERVER SITE EMAILTO DBOPS DATE
echo "nn### monitor DR sync started @ `date` ###" >> $LOGFILE
echo "Running SQL command to verify latest SCN.." >> $LOGFILE
echo "The current SCN of the Primary DB server is: $PRIMARY_CURRENT_SCN" >> $LOGFILE
echo "Connecting now to the secondary standby database server..." >> $LOGFILE
SECONDARY_CURRENT_SCN=`ssh oracle@xxx.xxx.xxx.xxx /home/oracle/script_sync2.sh` >> $LOGFILE
export SECONDARY_CURRENT_SCN
echo "Secondary SCN output returned as: $SECONDARY_CURRENT_SCN" >> $LOGFILE
grep ORA- /dba/scripts/output.txt
if [ $? = 0 ]; then
echo "Remote ssh command to Secondary server failed..Exiting" >> $LOGFILE
echo "### monitor DR sync failed @ `date` ###" >> $LOGFILE
echo "PROBLEM" >> $LOGFILE
mailx -r $DBOPS -s "PROBLEM" $EMAILTO < $LOGFILE
exit
else
echo "The current SCN of the Secondary DB server is: $SECONDARY_CURRENT_SCN" >> $LOGFILE
DIFF=`expr $PRIMARY_CURRENT_SCN - $SECONDARY_CURRENT_SCN` ; export DIFF
if [ $PRIMARY_CURRENT_SCN -ne $SECONDARY_CURRENT_SCN ]; then
echo "The difference is $DIFF" >> $LOGFILE
if [ `echo $DIFF` -gt 3 ]; then
echo "Log Gap: $DIFF" >> $LOGFILE
echo "PROBLEM" >> $LOGFILE
echo "### script finished @ `date` ###nn" >> $LOGFILE
mailx -r $DBOPS -s "PROBLEM" $EMAILTO < $LOGFILE
else
echo "SUCCESS" >> $LOGFILE
mailx -r $DBOPS -s "SUCCESS" $EMAILTO < $LOGFILE
echo "Log Gap: $DIFF" >> $LOGFILE
fi
else
echo "Log Gap: $DIFF" >> $LOGFILE
echo "SUCCESS" >> $LOGFILE
mailx -r $DBOPS -s "SUCCESS" $EMAILTO < $LOGFILE
fi
fi
shell-script scripting
edited Nov 6 '17 at 16:34
asked Nov 6 '17 at 15:58
FaMontyN2
639
639
Why does it needed to be added to the beginning of the file?
â Ignacio Vazquez-Abrams
Nov 6 '17 at 16:02
@IgnacioVazquez-Abrams Because the will be sent to a machine with a monitoring agent which check from the top of the file, so i'd like to have the last run/checking on the top
â FaMontyN2
Nov 6 '17 at 16:04
@MarkPlotnick sorry, i don't get your comment
â FaMontyN2
Nov 6 '17 at 16:05
Does each line of the logfile have a timestamp? If so, and you want newer portions of the logfile added at the top, does that mean that the timestamps will not all be in order?
â Mark Plotnick
Nov 6 '17 at 16:14
@MarkPlotnick Yes, there is a time stamp on the top of the portion of log generate every run. And i don't mind that won't be in order, it won't be a problem.
â FaMontyN2
Nov 6 '17 at 16:16
 |Â
show 2 more comments
Why does it needed to be added to the beginning of the file?
â Ignacio Vazquez-Abrams
Nov 6 '17 at 16:02
@IgnacioVazquez-Abrams Because the will be sent to a machine with a monitoring agent which check from the top of the file, so i'd like to have the last run/checking on the top
â FaMontyN2
Nov 6 '17 at 16:04
@MarkPlotnick sorry, i don't get your comment
â FaMontyN2
Nov 6 '17 at 16:05
Does each line of the logfile have a timestamp? If so, and you want newer portions of the logfile added at the top, does that mean that the timestamps will not all be in order?
â Mark Plotnick
Nov 6 '17 at 16:14
@MarkPlotnick Yes, there is a time stamp on the top of the portion of log generate every run. And i don't mind that won't be in order, it won't be a problem.
â FaMontyN2
Nov 6 '17 at 16:16
Why does it needed to be added to the beginning of the file?
â Ignacio Vazquez-Abrams
Nov 6 '17 at 16:02
Why does it needed to be added to the beginning of the file?
â Ignacio Vazquez-Abrams
Nov 6 '17 at 16:02
@IgnacioVazquez-Abrams Because the will be sent to a machine with a monitoring agent which check from the top of the file, so i'd like to have the last run/checking on the top
â FaMontyN2
Nov 6 '17 at 16:04
@IgnacioVazquez-Abrams Because the will be sent to a machine with a monitoring agent which check from the top of the file, so i'd like to have the last run/checking on the top
â FaMontyN2
Nov 6 '17 at 16:04
@MarkPlotnick sorry, i don't get your comment
â FaMontyN2
Nov 6 '17 at 16:05
@MarkPlotnick sorry, i don't get your comment
â FaMontyN2
Nov 6 '17 at 16:05
Does each line of the logfile have a timestamp? If so, and you want newer portions of the logfile added at the top, does that mean that the timestamps will not all be in order?
â Mark Plotnick
Nov 6 '17 at 16:14
Does each line of the logfile have a timestamp? If so, and you want newer portions of the logfile added at the top, does that mean that the timestamps will not all be in order?
â Mark Plotnick
Nov 6 '17 at 16:14
@MarkPlotnick Yes, there is a time stamp on the top of the portion of log generate every run. And i don't mind that won't be in order, it won't be a problem.
â FaMontyN2
Nov 6 '17 at 16:16
@MarkPlotnick Yes, there is a time stamp on the top of the portion of log generate every run. And i don't mind that won't be in order, it won't be a problem.
â FaMontyN2
Nov 6 '17 at 16:16
 |Â
show 2 more comments
2 Answers
2
active
oldest
votes
up vote
1
down vote
If you want to "append" (insert) the current run's logs to the beginning of the existing log file, then follow through with your apparent attempt at using a TEMPLOG file. Write all of the script's output/data to that $TEMPLOG file, then do:
ed -s $LOGFILE <<< "0r $TEMPLOG"$'n'wq
To read $TEMPLOG into $LOGFILE after line zero.
As an example:
$ cat logfile
previous
entries
here
$ cat templog
New
Entry goes
Here
$ ed -s "$LOGFILE" <<< "0r $TEMPLOG"$'n'wq
$ cat logfile
New
Entry goes
Here
previous
entries
here
If your shell does not support here-strings, print the instructions to ed's stdin instead:
printf "0r $TEMPLOGnwqn" | ed -s logfile
If you only need the contents of the email to be changed, and not the $LOGFILE itself, just prepend it in the pipe to mail:
cat "$TEMPLOG" "$LOGFILE" | mailx -r $DBOPS -s "SUCCESS" $EMAILTO
Hi Jeff, i tried your suggestion with ed but it doesn't work: ./monitor_sync_1_FMn2.sh[41]: syntax error at line 45 : `<' unexpected Should i use a specific shell for it?
â FaMontyN2
Nov 7 '17 at 9:10
Yes; I think bash, zsh, and ksh support it (itâÂÂs a here-string as distinguished from a here-document)
â Jeff Schaller
Nov 7 '17 at 10:19
Hi Jeff, i just tried to run that command out of the script with 2 random files in KSH but still no joy: scripts $ ed -s FM2.test <<< "0r IG.test"$'n'wq ksh: syntax error: `<' unexpected
â FaMontyN2
Nov 8 '17 at 12:02
Are you working with ksh v88 maybe?
â Jeff Schaller
Nov 8 '17 at 12:41
@FaMontyN2, I've added a non-here-string option to the answer; see if that works any better.
â Jeff Schaller
Nov 8 '17 at 15:35
add a comment |Â
up vote
0
down vote
Since your script appends to $LOGFILE, I think what you want is
mv $LOGFILE $TEMPLOG
at the beginning and
cat $TEMPLOG >>$LOGFILE
just before mailing.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
If you want to "append" (insert) the current run's logs to the beginning of the existing log file, then follow through with your apparent attempt at using a TEMPLOG file. Write all of the script's output/data to that $TEMPLOG file, then do:
ed -s $LOGFILE <<< "0r $TEMPLOG"$'n'wq
To read $TEMPLOG into $LOGFILE after line zero.
As an example:
$ cat logfile
previous
entries
here
$ cat templog
New
Entry goes
Here
$ ed -s "$LOGFILE" <<< "0r $TEMPLOG"$'n'wq
$ cat logfile
New
Entry goes
Here
previous
entries
here
If your shell does not support here-strings, print the instructions to ed's stdin instead:
printf "0r $TEMPLOGnwqn" | ed -s logfile
If you only need the contents of the email to be changed, and not the $LOGFILE itself, just prepend it in the pipe to mail:
cat "$TEMPLOG" "$LOGFILE" | mailx -r $DBOPS -s "SUCCESS" $EMAILTO
Hi Jeff, i tried your suggestion with ed but it doesn't work: ./monitor_sync_1_FMn2.sh[41]: syntax error at line 45 : `<' unexpected Should i use a specific shell for it?
â FaMontyN2
Nov 7 '17 at 9:10
Yes; I think bash, zsh, and ksh support it (itâÂÂs a here-string as distinguished from a here-document)
â Jeff Schaller
Nov 7 '17 at 10:19
Hi Jeff, i just tried to run that command out of the script with 2 random files in KSH but still no joy: scripts $ ed -s FM2.test <<< "0r IG.test"$'n'wq ksh: syntax error: `<' unexpected
â FaMontyN2
Nov 8 '17 at 12:02
Are you working with ksh v88 maybe?
â Jeff Schaller
Nov 8 '17 at 12:41
@FaMontyN2, I've added a non-here-string option to the answer; see if that works any better.
â Jeff Schaller
Nov 8 '17 at 15:35
add a comment |Â
up vote
1
down vote
If you want to "append" (insert) the current run's logs to the beginning of the existing log file, then follow through with your apparent attempt at using a TEMPLOG file. Write all of the script's output/data to that $TEMPLOG file, then do:
ed -s $LOGFILE <<< "0r $TEMPLOG"$'n'wq
To read $TEMPLOG into $LOGFILE after line zero.
As an example:
$ cat logfile
previous
entries
here
$ cat templog
New
Entry goes
Here
$ ed -s "$LOGFILE" <<< "0r $TEMPLOG"$'n'wq
$ cat logfile
New
Entry goes
Here
previous
entries
here
If your shell does not support here-strings, print the instructions to ed's stdin instead:
printf "0r $TEMPLOGnwqn" | ed -s logfile
If you only need the contents of the email to be changed, and not the $LOGFILE itself, just prepend it in the pipe to mail:
cat "$TEMPLOG" "$LOGFILE" | mailx -r $DBOPS -s "SUCCESS" $EMAILTO
Hi Jeff, i tried your suggestion with ed but it doesn't work: ./monitor_sync_1_FMn2.sh[41]: syntax error at line 45 : `<' unexpected Should i use a specific shell for it?
â FaMontyN2
Nov 7 '17 at 9:10
Yes; I think bash, zsh, and ksh support it (itâÂÂs a here-string as distinguished from a here-document)
â Jeff Schaller
Nov 7 '17 at 10:19
Hi Jeff, i just tried to run that command out of the script with 2 random files in KSH but still no joy: scripts $ ed -s FM2.test <<< "0r IG.test"$'n'wq ksh: syntax error: `<' unexpected
â FaMontyN2
Nov 8 '17 at 12:02
Are you working with ksh v88 maybe?
â Jeff Schaller
Nov 8 '17 at 12:41
@FaMontyN2, I've added a non-here-string option to the answer; see if that works any better.
â Jeff Schaller
Nov 8 '17 at 15:35
add a comment |Â
up vote
1
down vote
up vote
1
down vote
If you want to "append" (insert) the current run's logs to the beginning of the existing log file, then follow through with your apparent attempt at using a TEMPLOG file. Write all of the script's output/data to that $TEMPLOG file, then do:
ed -s $LOGFILE <<< "0r $TEMPLOG"$'n'wq
To read $TEMPLOG into $LOGFILE after line zero.
As an example:
$ cat logfile
previous
entries
here
$ cat templog
New
Entry goes
Here
$ ed -s "$LOGFILE" <<< "0r $TEMPLOG"$'n'wq
$ cat logfile
New
Entry goes
Here
previous
entries
here
If your shell does not support here-strings, print the instructions to ed's stdin instead:
printf "0r $TEMPLOGnwqn" | ed -s logfile
If you only need the contents of the email to be changed, and not the $LOGFILE itself, just prepend it in the pipe to mail:
cat "$TEMPLOG" "$LOGFILE" | mailx -r $DBOPS -s "SUCCESS" $EMAILTO
If you want to "append" (insert) the current run's logs to the beginning of the existing log file, then follow through with your apparent attempt at using a TEMPLOG file. Write all of the script's output/data to that $TEMPLOG file, then do:
ed -s $LOGFILE <<< "0r $TEMPLOG"$'n'wq
To read $TEMPLOG into $LOGFILE after line zero.
As an example:
$ cat logfile
previous
entries
here
$ cat templog
New
Entry goes
Here
$ ed -s "$LOGFILE" <<< "0r $TEMPLOG"$'n'wq
$ cat logfile
New
Entry goes
Here
previous
entries
here
If your shell does not support here-strings, print the instructions to ed's stdin instead:
printf "0r $TEMPLOGnwqn" | ed -s logfile
If you only need the contents of the email to be changed, and not the $LOGFILE itself, just prepend it in the pipe to mail:
cat "$TEMPLOG" "$LOGFILE" | mailx -r $DBOPS -s "SUCCESS" $EMAILTO
edited Nov 8 '17 at 15:35
answered Nov 6 '17 at 17:13
Jeff Schaller
32k849109
32k849109
Hi Jeff, i tried your suggestion with ed but it doesn't work: ./monitor_sync_1_FMn2.sh[41]: syntax error at line 45 : `<' unexpected Should i use a specific shell for it?
â FaMontyN2
Nov 7 '17 at 9:10
Yes; I think bash, zsh, and ksh support it (itâÂÂs a here-string as distinguished from a here-document)
â Jeff Schaller
Nov 7 '17 at 10:19
Hi Jeff, i just tried to run that command out of the script with 2 random files in KSH but still no joy: scripts $ ed -s FM2.test <<< "0r IG.test"$'n'wq ksh: syntax error: `<' unexpected
â FaMontyN2
Nov 8 '17 at 12:02
Are you working with ksh v88 maybe?
â Jeff Schaller
Nov 8 '17 at 12:41
@FaMontyN2, I've added a non-here-string option to the answer; see if that works any better.
â Jeff Schaller
Nov 8 '17 at 15:35
add a comment |Â
Hi Jeff, i tried your suggestion with ed but it doesn't work: ./monitor_sync_1_FMn2.sh[41]: syntax error at line 45 : `<' unexpected Should i use a specific shell for it?
â FaMontyN2
Nov 7 '17 at 9:10
Yes; I think bash, zsh, and ksh support it (itâÂÂs a here-string as distinguished from a here-document)
â Jeff Schaller
Nov 7 '17 at 10:19
Hi Jeff, i just tried to run that command out of the script with 2 random files in KSH but still no joy: scripts $ ed -s FM2.test <<< "0r IG.test"$'n'wq ksh: syntax error: `<' unexpected
â FaMontyN2
Nov 8 '17 at 12:02
Are you working with ksh v88 maybe?
â Jeff Schaller
Nov 8 '17 at 12:41
@FaMontyN2, I've added a non-here-string option to the answer; see if that works any better.
â Jeff Schaller
Nov 8 '17 at 15:35
Hi Jeff, i tried your suggestion with ed but it doesn't work: ./monitor_sync_1_FMn2.sh[41]: syntax error at line 45 : `<' unexpected Should i use a specific shell for it?
â FaMontyN2
Nov 7 '17 at 9:10
Hi Jeff, i tried your suggestion with ed but it doesn't work: ./monitor_sync_1_FMn2.sh[41]: syntax error at line 45 : `<' unexpected Should i use a specific shell for it?
â FaMontyN2
Nov 7 '17 at 9:10
Yes; I think bash, zsh, and ksh support it (itâÂÂs a here-string as distinguished from a here-document)
â Jeff Schaller
Nov 7 '17 at 10:19
Yes; I think bash, zsh, and ksh support it (itâÂÂs a here-string as distinguished from a here-document)
â Jeff Schaller
Nov 7 '17 at 10:19
Hi Jeff, i just tried to run that command out of the script with 2 random files in KSH but still no joy: scripts $ ed -s FM2.test <<< "0r IG.test"$'n'wq ksh: syntax error: `<' unexpected
â FaMontyN2
Nov 8 '17 at 12:02
Hi Jeff, i just tried to run that command out of the script with 2 random files in KSH but still no joy: scripts $ ed -s FM2.test <<< "0r IG.test"$'n'wq ksh: syntax error: `<' unexpected
â FaMontyN2
Nov 8 '17 at 12:02
Are you working with ksh v88 maybe?
â Jeff Schaller
Nov 8 '17 at 12:41
Are you working with ksh v88 maybe?
â Jeff Schaller
Nov 8 '17 at 12:41
@FaMontyN2, I've added a non-here-string option to the answer; see if that works any better.
â Jeff Schaller
Nov 8 '17 at 15:35
@FaMontyN2, I've added a non-here-string option to the answer; see if that works any better.
â Jeff Schaller
Nov 8 '17 at 15:35
add a comment |Â
up vote
0
down vote
Since your script appends to $LOGFILE, I think what you want is
mv $LOGFILE $TEMPLOG
at the beginning and
cat $TEMPLOG >>$LOGFILE
just before mailing.
add a comment |Â
up vote
0
down vote
Since your script appends to $LOGFILE, I think what you want is
mv $LOGFILE $TEMPLOG
at the beginning and
cat $TEMPLOG >>$LOGFILE
just before mailing.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Since your script appends to $LOGFILE, I think what you want is
mv $LOGFILE $TEMPLOG
at the beginning and
cat $TEMPLOG >>$LOGFILE
just before mailing.
Since your script appends to $LOGFILE, I think what you want is
mv $LOGFILE $TEMPLOG
at the beginning and
cat $TEMPLOG >>$LOGFILE
just before mailing.
answered Nov 8 '17 at 16:51
David
645
645
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f402859%2fappend-to-the-top-in-a-log-file%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Why does it needed to be added to the beginning of the file?
â Ignacio Vazquez-Abrams
Nov 6 '17 at 16:02
@IgnacioVazquez-Abrams Because the will be sent to a machine with a monitoring agent which check from the top of the file, so i'd like to have the last run/checking on the top
â FaMontyN2
Nov 6 '17 at 16:04
@MarkPlotnick sorry, i don't get your comment
â FaMontyN2
Nov 6 '17 at 16:05
Does each line of the logfile have a timestamp? If so, and you want newer portions of the logfile added at the top, does that mean that the timestamps will not all be in order?
â Mark Plotnick
Nov 6 '17 at 16:14
@MarkPlotnick Yes, there is a time stamp on the top of the portion of log generate every run. And i don't mind that won't be in order, it won't be a problem.
â FaMontyN2
Nov 6 '17 at 16:16