OpenWRT log to file with bash script

Today I had quite strange experience – in the evening I saw webcam connected to my router has it’s LED lit up meaning it’s working. Yet – it should be disabled as I left mjpeg-stream turned off and haven’t configured other surveillance method yet. First thought – have I been hacked? CPU usage graph shows around 6PM it spiked so it must have started then.

Fortunately I quickly found out that at 6:22PM my USB hub disconnected and connected again and when USB camera is connected mjpeg-streamer automatically starts. Also power strip powering all devices around my router fell down from the wall… So probably hub’s power supply disconnected causing it to start working as standard hub without external power. Mystery solved.

This made me think about logs. I’ve checked last entries using


This has shown past 2 days of logs. But what if I would need more?

I decided to try out simplest method with in-build packages as my router is already quite filled. To save logs to file I’ve edited system config file and added lines:

root@WnekoWrt:~# vim /etc/config/system

config system
        option log_file '/mnt/sda1/logs/wnekowrt_log'
        option log_remote '0'

After that log service needed to be restarted:

/etc/init.d/log restart
/etc/init.d/system restart

Quick test:

root@WnekoWrt:~# logger test "test message"

I’ve checked log file through WinSCP and yes – it was working. And saving to USB disk removes any potential problems with storage space.

Yet there was still one problem – having one big file is not a good idea for obvious reasons. I wanted to tackle this issue completely on my own. That’s how my first bash script came to existence:

date=$(date +"%d_%m_%Y")
mv $1 $1_$date
touch $1
/etc/init.d/log restart
  • First line – shebang – is different on OpenWRT, standard Linux “#!/bin/bash” won’t work.
  • Second line reads current date into variable “date”
  • Third line renames current log file, provided by command line argument $1, to name with current date appended, for example wnekowrt_log to wnekowrt_log_28_07_2020
  • Fourth line creates new empty file with the same name as previous one for next logs
  • Last line restarts log service, as without that it was still writing to original file even after restart

I’ve done some tests and it looks to be working as intended – after running it current file becomes renamed with current date and log is written to new one.

Next I needed to run this periodically. I decided to run it everyday at 23:59 to save current day logs to file with current date in name. For that i created entry in crontab:

root@WnekoWrt:/scripts# crontab -e
59 23 * * * /scripts/ /mnt/sda1/logs/wnekowrt_log

I first tested it with another time – and it worked perfectly.

Now, with 500GB of disk space my router is ready for heavy logging! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.