michaelmuckkremtz
Posts: 5
Joined: Fri Aug 14, 2020 8:00 am

nano creates empty documents on all raspberries I tried

Fri Aug 14, 2020 8:24 am

Hi everybody!

That's my first attempt here in the forum but I don't now how to proceed with my problem.
I made myself a little digital diary where I can power up a Pi Zero just with an attached keyboard and write some stuff that I have on my mind.

Unfortunately most of the time the files I create are just empty. I tried to find a scheme about when this occurs but I don't see one.
I think when I'm in reach of my home Wifi the chances of having the text beeing saved is higher.
But the idea is to have this thing with me an write from time to time.

Here is the code:

Code: Select all

cd /home/pi/Documents/timestamped_texts/bin/
./blink.py &     #that triggers a flashing LED attached to the nano to show me the text document is open

cd /home/pi/Documents/timestamped_texts/
sudo nano $(date +%Y%m%d_%H%M%S)_$(($RANDOM % 10))$(($RANDOM % 10))_     #that creates the document to write in
cd ~/

kill  $(pgrep blink.py)     # after finishing the file the blinking stopps
To finish my files I type in
ctrl + x
ctrl + y
(optionally adding a title)
enter
(optionally hitting y when I entered a title)

according to my script the blinking stops and I know everything should be fine.

The file names come out very nicely just the emptiness of the documents is not was I was looking for...
Screenshot 2020-08-14 at 10.02.48.png
Screenshot 2020-08-14 at 10.02.48.png (180.5 KiB) Viewed 354 times
Interesting is that the modified date reflects correctly how long I wrote in the file.
But nevertheless it's just empty.

For having the correct time in the timestamp I just added a RTC module that syncs the correct system time on startup.
I thought that might fix my problem. But unfortunately it didn't.

Any ideas would be highly appreciated!
Thanks in advance!

PS: On my Linux Mint notebook I run the same command and everything works as expected...

Code: Select all

 sudo nano $(date +%Y%m%d_%H%M%S)_$(($RANDOM % 10))$(($RANDOM % 10))_

pcmanbob
Posts: 9612
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: nano creates empty documents on all raspberries I tried

Fri Aug 14, 2020 10:24 am

Hi.

So my first question is why are you running nano with root privileges, I seen no need to do that.

and to save and exit nano, you need to do , Ctrl & x , then just y and then press enter/carriage return

this way your file will be save with what ever you typed in to it.

Edit ...

I created a script similar to yours

Code: Select all

echo " starting nano for text entry "
sleep 2

cd /home/pi/Documents/
nano $(date +%Y%m%d_%H%M%S)_$(($RANDOM % 10))$(($RANDOM % 10))    #that creates the document to write in
cd ~/

echo " finished exit script   "
and here is a video of it in operation showing the saved text in the created file.

https://youtu.be/UNwNjfBJ0Pc
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

michaelmuckkremtz
Posts: 5
Joined: Fri Aug 14, 2020 8:00 am

Re: nano creates empty documents on all raspberries I tried

Fri Aug 14, 2020 11:56 am

Hi!

Thanks for helping me out here!!

There is no particular reason why I used sudo. I guess I'm just used to type sudo nano cause I have to for all kinds of other documents outside of my pi user folder...
I removed sudo and it doesn't change the situation.

For saving: of course it's just 'y' and not ctrl + 'y'.
I wrote it down incorrectly but use it in a correct way.

Sometimes when I want to name the file I add some text and then press enter any y again.

Since I don't have a monitor attached to my pi zero I just rely on the blinking of the LED which stops when I get out of nano.
From the command line I can even restart the script and the blinking starts over.
That works for me quite well if I want to split documents.

But still: some files are just empty and I don't have a clue why.
Screenshot from 2020-08-14 13-39-28.png
Screenshot from 2020-08-14 13-39-28.png (15.54 KiB) Viewed 315 times
I think I'll put a little screen on it for now and keep on trying stuff again in order to find the cause and hopefully the solution...

pcmanbob
Posts: 9612
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: nano creates empty documents on all raspberries I tried

Fri Aug 14, 2020 12:07 pm

Well if you are typing blindly then its no wonder you are failing in your task, you need some way of knowing that keyed in commands are actually being executed correctly.

And you have no way of knowing if you are even typing on the correct part of the screen to enter text in your file.

if you had included that vital bit of information that you were trying to do it without a screen then the first suggestion would have been to connect a screen.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

hippy
Posts: 7908
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: nano creates empty documents on all raspberries I tried

Fri Aug 14, 2020 1:15 pm

If you want something which you can blindly type into it would be better to create a Python program which can accept typed input and store that to a file rather than invoking 'nano'.

michaelmuckkremtz
Posts: 5
Joined: Fri Aug 14, 2020 8:00 am

Re: nano creates empty documents on all raspberries I tried

Fri Aug 14, 2020 1:18 pm

Sorry for not explicitly mentioning the abundance of a screen.
Writing "a Pi Zero just with an attached keyboard" was not clear enough...

But that's not really the cause for the problem.
I just start typing when the LED flashes.
And the end of the flashing means, nano came to an end which means I hit the right keys... ;-)

The whole idea of this setup is to not have a screen and just write.
But of course I want to be able to read it later..

I think I found the solution though:

For debugging I connected a screen and added ls -l after my nano command to the script to check the content of the folder I'm writing to.
Everything works fine here and the files have a size.

After turning off the power and rebooting some files just loose their size completely.

Stupidly easy thing: I usually just pull the USB-cable to turn off the pi.
During the last tests I typed 'shutdown now' at the end and now files are fine after rebooting.
It seems to be a good idea to let Linux go to sleep properly.

I will monitor it a bit more but it makes kind of sense that the files system might be in danger when not really shutting down the system.
Now I can finally design and print my casing now that I don't need to change the hardware!

Thanks for your thoughts!

pcmanbob
Posts: 9612
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: nano creates empty documents on all raspberries I tried

Fri Aug 14, 2020 1:33 pm

michaelmuckkremtz wrote:
Fri Aug 14, 2020 1:18 pm
Sorry for not explicitly mentioning the abundance of a screen.
Writing "a Pi Zero just with an attached keyboard" was not clear enough...
I think most people would have assumed like i did that if you are typing then you would have a screen attached, "the keyboard only" , only suggested no mouse to me.

After turning off the power and rebooting some files just loose their size completely.

Stupidly easy thing: I usually just pull the USB-cable to turn off the pi.
During the last tests I typed 'shutdown now' at the end and now files are fine after rebooting.
It seems to be a good idea to let Linux go to sleep properly.
If you just pull the power several things could happen ,

1. Any recently closed files may not have been flushed from the cache to the SD card or even from the SD card cache to the actual storage memory.

2. If the OS is updating or writing to the system files you could end up with a corrupted non bootable OS

you should always shut the pi down with the correct shutdown command, you could implement this using a simple button/switch connected to a gpio pin.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

hippy
Posts: 7908
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: nano creates empty documents on all raspberries I tried

Fri Aug 14, 2020 1:37 pm

michaelmuckkremtz wrote:
Fri Aug 14, 2020 1:18 pm
I usually just pull the USB-cable to turn off the pi.
Typing blindly into a Pi without a screen, into an editing program whose operational mode you cannot know or determine, then simply pulling the plug, sounds a sure fire way to have things go wrong in the way you are experiencing.

There's nothing wrong per se in what you are wanting to do; you just need to do it in a more refined way.

Prototype using Raspberry Pi OS by all means but it would be worth looking at Bare Metal, BuildRoot, TinyCore, DietPi, Ultibo or NARD, creating 'a typing appliance' rather than using a general purpose OS.

DarkElvenAngel
Posts: 851
Joined: Tue Mar 20, 2018 9:53 pm

Re: nano creates empty documents on all raspberries I tried

Fri Aug 14, 2020 1:51 pm

If you are using the onboard LED to see when you should be typing why not set the trigger to turn it on or heartbeat when nano is started and back to mmc0 afterwards?

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 26716
Joined: Sat Jul 30, 2011 7:41 pm

Re: nano creates empty documents on all raspberries I tried

Fri Aug 14, 2020 1:52 pm

Sounds like the files are not yet flushed. So you need to "sync" on the command line after exiting Nano, and once that command has completed then you can turn off.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

DarkElvenAngel
Posts: 851
Joined: Tue Mar 20, 2018 9:53 pm

Re: nano creates empty documents on all raspberries I tried

Fri Aug 14, 2020 1:59 pm

Syncing is a good point you add sync to your script and then set LED normal after the sync is complete. Or something like that.

michaelmuckkremtz
Posts: 5
Joined: Fri Aug 14, 2020 8:00 am

Re: nano creates empty documents on all raspberries I tried

Fri Aug 14, 2020 3:17 pm

Great!

Sync is added to the script.
Additionally I shutdown the system correctly for not messing up anything else.

Problem solved, I'm happy.

Thanks everyone!

Return to “General programming discussion”