As usual, I can inspect the contents of syslog entries in this way:
cat /var/log/syslog | grep myentry
I need to append all myentry rows to a specific file. Of course just redirecting the output of the command above to the file will not work, because it will append all the rows, even if they were already added last time.
The first solution that comes to mind is to cycle among all the rows in syslog until I find the last row of the target file. Then I can append all the following ones. Doing this periodically (i.e. using a cronjob or even easier a timed cycle in bash) should do the trick.
Is there something smarter or more elegant to do the same job?
EDIT
I add what terdon requested:
Example of my syslog:
Jan 17 13:03:18 stm32mp1-abc local2.info chat[15782]: CONNECT
Jan 17 13:03:18 stm32mp1-abc local2.info chat[15782]: -- got it
Jan 17 13:03:18 stm32mp1-abc user.info myentry[14300]: Ready
Jan 17 13:03:18 stm32mp1-abc local2.info chat[15782]: send (^M)
Jan 17 13:03:18 stm32mp1-abc user.info myentry[14300]: Init complete
Jan 17 13:03:18 stm32mp1-abc daemon.info pppd[14362]: Serial connection established.
Jan 17 13:03:18 stm32mp1-abc daemon.info pppd[14362]: Using interface ppp0
Example of the existing file I want to append to:
Jan 17 13:03:18 stm32mp1-abc user.info myentry[14300]: Ready
Final output I expect from those two files:
Jan 17 13:03:18 stm32mp1-abc user.info myentry[14300]: Ready
Jan 17 13:03:18 stm32mp1-abc user.info myentry[14300]: Init complete
UPDATE
Ok, it seems I need to be very specific with the example, regardless my description. So new examples:
syslog
Jan 17 13:03:18 stm32mp1-abc local2.info chat[15782]: CONNECT
Jan 17 13:03:18 stm32mp1-abc local2.info chat[15782]: -- got it
Jan 17 13:03:18 stm32mp1-abc user.info myentry[14300]: Ready
Jan 17 13:03:18 stm32mp1-abc local2.info chat[15782]: send (^M)
Jan 17 13:03:18 stm32mp1-abc user.info myentry[14300]: Init complete
Jan 17 13:03:18 stm32mp1-abc daemon.info pppd[14362]: Serial connection established.
Jan 17 13:03:18 stm32mp1-abc user.info myentry[14300]: Start operations
Jan 17 13:03:18 stm32mp1-abc daemon.info pppd[14362]: Using interface ppp0
current output
Jan 17 13:03:18 stm32mp1-abc user.info myentry[14300]: Init complete
new output
Jan 17 13:03:18 stm32mp1-abc user.info myentry[14300]: Init complete
Jan 17 13:03:18 stm32mp1-abc user.info myentry[14300]: Start operations
It should be clear:
- if the
outputfile is empty, append all thesyslog/myentriesrows - if the
outputfile is not empty, append all the nextsyslog/myentriesrows (from the matching one) - if the
outputfile is not empty and there's no matching, still append all the nextsyslog/myentriesrows (after the last timestamp)
As said, I'm able to do this using the brute force: cycle all rows, check criteria and append if needed.
I'm looking for an easier solution, like the proposal to split the syslog entries automatically, but I didn't find a way to do it.
syslogfromBusybox v1.29.3and I didn't find how to specify a separate logfile for my entries. If you can elaborate a bit your answer I think it might be a good solution.busybox-syslogdexplicitly states that/etc/syslog.confis ignored here. Your options thus are: 1) the one you are doing now; 2) write to named pipe instead of a file and have some infinite loop read the other end and save the output; 3) Busybox's syslog can log over the network when using-R host[:port]so it is basically like option 2 but saves you the hassle of named pipes. manpages.debian.org/jessie/busybox-syslogd/syslogd.8.en.html