FOR TIPS, gUIDES & TUTORIALS

subscribe to our Youtube

GO TO YOUTUBE

14455 questions

17168 answers

28195 comments

0 members

We are migrating to our new platform at https://community.teltonika.lt. Moving forward, you can continue discussions on this new platform. This current platform will be temporarily maintained for reference purposes.
0 votes
232 views 7 comments
by anonymous
Trying to write some custom scripts but not very familiar with BusyBox embedded linux as installed on Teltonika routers.

Does anyone know how to mimic truncate or fallocate (both missing in BusyBox)? I have a tmp file which varies in size every time it is modified, and I'd like to crop or pad the file so that it's *always* the same size on disk i.e: always the same number of characters. Any suggestions?

I could try dd count=N to copy a cropped version of the tmp file each it's modified, but this seems inefficient. Thanks!

1 Answer

0 votes
by anonymous

Hello,

Install coreutils-truncate:

opkg update
opkg install coreutils-truncate
truncate --help

Regards,

 

Best answer
by anonymous
Awesome, thank you!
by anonymous
One slight issue after doing this – the router became very unresponsive, high CPU, RAM nearly maxed out. My original script caused nothing like this, but adding one truncate line seemed to choke things. Even deleting the truncate line from my script and rebooting the router didn't seem to fix things, so I re-installed the original firmware.

Maybe I'll try again, but perhaps truncate was omitted from BusyBox for a reason, or the 'opkg upgrade' borked something? I'm not knowledgeable enough to figure out what happened, just rolled back to my last config.
by anonymous
Did you truncate something resulting in a large file in /tmp ? /tmp is a mount point for a tmpfs filesystem it is easy to swallow all the available ram.
by anonymous
root@lgrrutx:/# truncate -s 10K /tmp/tst
root@lgrrutx:/# l /tmp/tst
-rw-r--r--    1 root     root         10240 May 22 13:17 /tmp/tst
root@lgrrutx:/# truncate -s 9K /tmp/tst
root@lgrrutx:/# l /tmp/tst
-rw-r--r--    1 root     root          9216 May 22 13:17 /tmp/tst

Still working normally ...

To remove a package: opkg remove xxxx, no need to reset the device.

by anonymous

Thanks for replying, noted on all your points. 

My script generates a very small file /tmp/regfile – anywhere from ~0 to 1K bytes, but my target file size is 250 bytes. In reality I want the file to contain a maximum of 250 *characters* (it's a modbus register file) but I've not figured out a more graceful way to do this than simply truncating the file.
 

#! /bin/ash
while true
do
gsmctl -CA 'AT+QCAINFO' -o --modemtime 2 grep pattern file | tr '\n\r,\"' ',' > /tmp/regfile
truncate --size=250 /tmp/regfile
sleep 60
done
by anonymous

What "grep pattern file" does here ?

root@lgrrutx:~# gsmctl -CA 'AT+QCAINFO' -o --modemtime 2 grep pattern file | tr '\n\r,\"' ',' > /tmp/regfile
root@lgrrutx:~# l /tmp/regfile
-rw-r--r--    1 root     root            42 May 22 21:54 /tmp/regfile
root@lgrrutx:~# truncate -s 256 /tmp/regfile
root@lgrrutx:~# l /tmp/regfile
-rw-r--r--    1 root     root           256 May 22 21:54 /tmp/regfile
root@lgrrutx:~# hexdump -C /tmp/regfile 
00000000  31 30 35 34 31 36 30 30  37 2c 4f 4b 2c 2c 46 72  |105416007,OK,,Fr|
00000010  65 65 20 46 72 65 65 2c  32 33 2f 30 35 2f 32 32  |ee Free,23/05/22|
00000020  2c 32 31 3a 35 34 3a 30  36 2c 00 00 00 00 00 00  |,21:54:06,......|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000100
by anonymous
grep pattern file | tr '\n\r,\"' ','

Ah good point the 'grep pattern file' is redundant ... but, anyway I was using grep to turn the output of gsmctl into a string of comma separated values (easier for my home automation software to parse). Now in fact tr is doing it.