Trying to use crontab to make OpenWRT write persistent logs
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Trying to use crontab to make OpenWRT write persistent logs
I'm currently trying to use the following configuration, mostly through pipelines. This is to diagnose why the router sometimes breaks and requires a reboot which is quite frustrating due to my router's dual boot solution that leads to it falling back to the stock firmware after only one reboot on custom one, but requiring a triple reboot, which also requires waiting about 20 seconds before shutting down and then at least three before booting.
So the thing is, OpenWRT uses
Code:
logread
to display logs. But even after one attaches an external storage, the logs are(?) lost upon reboot. Therefore I came with a solution that utilizes the following crontab:
fwiw /var is symlinked to /tmp
The contents of the script:
Code:
if [ ! -f /var/log/logfull ] ; then
touch /var/log/logfull
else :
fi
logread | tail -n 30 | grep -Ev 'dnsmasq|simple-adblock' | tee -a /var/log/logfull
PS:
I know the uniq needs arguments or using cut in order to actually display unique entries that skip the timestamps at the start of each line. I've also found a solution that utilizes rev... but the packages providing it are nowhere to be found in OPKG repos... unless someone here knows if there is one?
but the packages providing it are nowhere to be found in OPKG repos... unless someone here knows if there is one?
Code:
for i in one two three; do
reverse=("$i" "${reverse[@]}")
done
echo "${reverse[@]}"
three two one
Code:
#!/usr/bin/bash
input="One Two Three Four"
reverse=""
len=${#input}
for (( i=$len-1; i>=0; i-- )); do
reverse="$reverse${input:$i:1}"
done
echo "Original: "$input""
echo "Reversed: "$reverse""
Original: One Two Three Four
Reversed: ruoF eerhT owT enO
ok my attempt to tame my chaotic way of explaining stuff and also stick to UNIX KISS philosophy. Nevermind using rev, that was more of a side remark anyway.
I have the following pipe:
Code:
logread | cut -b 26-255 | uniq -u | tee -a /var/log/logfile
I have tried redirection and placing uniq at 1st and 2nd position, but it still keeps printing duplicate lines, like that:
Code:
cron.err crond[2794]: user root: parse error at /*10
cron.err crond[2794]: user root: parse error at /*5
cron.err crond[2794]: user root: parse error at /*58
kern.debug kernel: [106229.461647] ieee80211 phy0: Mac80211 start BA *****
kern.debug kernel: [106231.344567] ieee80211 phy0: Stop BA ***
cron.err crond[2794]: user root: parse error at /*10
cron.err crond[2794]: user root: parse error at /*5
cron.err crond[2794]: user root: parse error at /*58
Optimally I'd like to retain the timestamp, but only get the first occurence of an unique error in a nice, decluttered log file, I assume using uniq -s 5 and changing to cut -b 12-16,25-255 would be enough?
Last edited by windowsuser; 06-13-2022 at 11:31 AM.
Do you have a sample of logread | tail -n 30 that you can post? So that someone can see what you are trying to achieve.
This is what it looks like. I want to filter out repetitive errors stemming from stuff I just haven't managed to find enough time for and make me able to search for important critical errors that could help me figure out why the router keeps crashing, why it drops 2.4 GHz radio etc. Because normally this log gets cleared once the router reboots and I want to write them into a file on the 64 GB pendrive (enough to write a really big, dirty log file, but I also want it to be easily legible for obvious reasons and not trash the router's ram once opened) to which some of the sysdirs are mounted, incl. /var/log. Sensitive data such as MACs is censored with (...):
Code:
Wed Jun 15 22:34:45 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused Is the server running on host "192.168.1.1" and accepting TCP/IP connections on port (...)
Wed Jun 15 22:35:01 2022 kern.debug kernel: [304285.120658] ieee80211 phy0: Mac80211 start BA (...)
Wed Jun 15 22:38:05 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused Is the server running on host "192.168.1.1" and accepting TCP/IP connections on port (...)
Wed Jun 15 22:41:26 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused Is the server running on host "192.168.1.1" and accepting TCP/IP connections on port (...)
Wed Jun 15 22:44:47 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused Is the server running on host "192.168.1.1" and accepting TCP/IP connections on port (...)
Wed Jun 15 22:48:08 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused Is the server running on host "192.168.1.1" and accepting TCP/IP connections on port (...)
Wed Jun 15 22:51:29 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused Is the server running on host "192.168.1.1" and accepting TCP/IP connections on port (...)
Wed Jun 15 22:54:50 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused Is the server running on host "192.168.1.1" and accepting TCP/IP connections on port (...)
Wed Jun 15 22:58:11 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused Is the server running on host "192.168.1.1" and accepting TCP/IP connections on port (...)
Wed Jun 15 23:01:32 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused Is the server running on host "192.168.1.1" and accepting TCP/IP connections on port (...)
Wed Jun 15 23:04:53 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused Is the server running on host "192.168.1.1" and accepting TCP/IP connections on port (...)
Wed Jun 15 23:08:14 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused Is the server running on host "192.168.1.1" and accepting TCP/IP connections on port (...)
Wed Jun 15 23:08:17 2022 daemon.notice hostapd: wlan0: AP-STA-DISCONNECTED (...)
Wed Jun 15 23:08:17 2022 daemon.info hostapd: wlan0: STA (...) IEEE 802.11: disassociated
Wed Jun 15 23:08:18 2022 daemon.info hostapd: wlan0: STA (...) IEEE 802.11: deauthenticated due to inactivity (timer DEAUTH/REMOVE)
Wed Jun 15 23:19:00 2022 cron.err crond[2794]: user root: parse error at /*10
Wed Jun 15 23:19:00 2022 cron.err crond[2794]: user root: parse error at /*5
Wed Jun 15 23:19:00 2022 cron.err crond[2794]: user root: parse error at /*58
Wed Jun 15 23:21:11 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused Is the server running on host "192.168.1.1" and accepting TCP/IP connections on port (...)
Wed Jun 15 23:21:25 2022 daemon.info hostapd: wlan0: STA (...) IEEE 802.11: authenticated
Wed Jun 15 23:21:25 2022 daemon.notice hostapd: wlan0: STA-OPMODE-N_SS-CHANGED (...)
Wed Jun 15 23:21:25 2022 daemon.info hostapd: wlan0: STA (...) IEEE 802.11: associated (aid 1)
Wed Jun 15 23:21:25 2022 daemon.notice hostapd: wlan0: AP-STA-CONNECTED (...)
Wed Jun 15 23:21:25 2022 daemon.info hostapd: wlan0: STA (...) WPA: pairwise key handshake completed (RSN)
Wed Jun 15 23:21:27 2022 daemon.info dnsmasq-dhcp[5019]: DHCPDISCOVER(br-lan) 192.168.1.(...)
Wed Jun 15 23:21:27 2022 daemon.info dnsmasq-dhcp[5019]: DHCPOFFER(br-lan) 192.168.1.(...)
Wed Jun 15 23:21:27 2022 daemon.info dnsmasq-dhcp[5019]: DHCPREQUEST(br-lan) 192.168.1.(...)
Wed Jun 15 23:21:27 2022 daemon.info dnsmasq-dhcp[5019]: DHCPACK(br-lan) 192.168.1.(...)
Wed Jun 15 23:21:40 2022 authpriv.info dropbear[3194]: Child connection from 192.168.1.(...)
Wed Jun 15 23:21:40 2022 authpriv.notice dropbear[3194]: Pubkey auth succeeded for 'root' with key sha1!! (...) from 192.168.1.(...)
So for example I want to show repetitive errors once or if possible twice – first and most recent occurence, with a timestamp. The rest needs to be filtered out. Some, like the bandwidthd error might also be trunctated, but that's risky. The most primitive way of achieving what I need that I think of is of course with grep -v, but I want something more neat.
Last edited by windowsuser; 06-15-2022 at 06:41 PM.
So for example I want to show repetitive errors once or if possible twice
Code:
txt="
Wed Jun 15 22:38:05 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused Is the server running on host "192.168.1.1" and accepting TCP/IP connections on port (...)
Wed Jun 15 22:41:26 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused Is the server running on host "192.168.1.1" and accepting TCP/IP connections on port (...)
Wed Jun 15 22:44:47 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused Is the server running on host "192.168.1.1" and accepting TCP/IP connections on port (...)
Wed Jun 15 22:48:08 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused Is the server running on host "192.168.1.1" and accepting TCP/IP connections on port (...)
Wed Jun 15 22:51:29 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused Is the server running on host "192.168.1.1" and accepting TCP/IP connections on port (...)
"
grep -nFx "$(sort <<< "$txt" | uniq -d)" <<< "$txt"
1:
7:
Only lines 1 and 7 match. Because they are empty lines. All the rest are different.(Time stamps)
So, get rid of the time stamps and compare the rest.
Example:
Code:
while read line; do
[[ "$line" =~ 2022(.*) ]] && echo "${BASH_REMATCH[1]}"
done <<< "$txt"
Give you a couple more examples for you to reference while building your script.
Code:
txt="
Wed Jun 15 22:38:05 2022 Mary had a little lamb
Wed Jun 15 22:38:06 2022 Mary had a little lamb
Wed Jun 15 22:41:26 2022 Whose fleece was white as snow
Wed Jun 15 22:41:27 2022 Whose fleece was white as snow
Wed Jun 15 22:44:47 2022 And everywhere that Mary went
Wed Jun 15 22:44:48 2022 And everywhere that Mary went
Wed Jun 15 22:44:49 2022 And everywhere that Mary went
Wed Jun 15 22:48:08 2022 The lamb was sure to go
Wed Jun 15 22:51:29 2022 The end
Wed Jun 15 22:51:30 2022 The end
Wed Jun 15 22:51:31 2022 The end
Wed Jun 15 22:51:32 2022 The end
"
while read line; do
[[ "$line" =~ 2022(.*) ]] && echo "${BASH_REMATCH[1]}"
done <<< "$txt" | uniq
while read line; do
echo "${line%%2022 *}"
done <<< "$txt"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.