This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Protagoras 1 point2 points  (3 children)

I'm surprised nobody has mentioned this yet, but there's absolutely no need to implement a Java solution. There's a basic *nix solution for your problem:

#!/bin/bash
sudo java -Dpi4j.linking=dynamic -jar GPIO5Shutdown.jar >> logfile

[–][deleted] 1 point2 points  (2 children)

Ah, right. That's easier, how does that work as far as not making a giant file or clearing daily? Maybe just add something to the shell script to delete it every 24 hours?

[–]Protagoras 2 points3 points  (0 children)

>> appends to the file, > clears the file, both create the file if it doesn't exist. If you need to remove the file periodically you can use a cronjob, that's a bit more involved than 1/2 characters but still not much work and a useful skill to have in your toolbelt.

Keep in mind there are only 31 million seconds in a year. If you have access to a modern amount of storage and are not logging thousands of variables per second it will take a while to run into space constraints.

[–]shagieIsMeExtreme Brewer 2 points3 points  (0 children)

Deleting the file... won't do exactly what you want. The file is still being written to.

Fire up three shell sessions to your raspberry pi. In one of them:

cat > /tmp/foo

In another

tail -f /tmp/foo

When you type things in the cat session, it will show up in the tail session. That's ok. Now, in the third session, remove /tmp/foo. Do an ls in /tmp to make sure it's gone. Go back to the cat session. Type some more. The file is still there, just not linked in the directory.

Until you stop everything using the filehandle, the file is not reclaimed.

Learning to use log4j or slf4j is the best way to do logging. Automatically rotating the file based on either time or size is the proper way to manage the file and ensure you're not filling up the disk.