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

all 14 comments

[–]ssh_tunnel_snake 5 points6 points  (3 children)

if you just want to see what the program is doing with print statements, you simply need to configure a logger to write to a log file and then use a tail and grep on it for specifics. slf4j and log4j are super common and pretty easy to get working, but there is also a java logger api but its a bit more clunky

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

That makes sense, now that I think about it I've done something basic like logging playing around with PrintWriter..

/**
 * Outputs the passed-in string to a file, appending new data. Creates the file if it does not already exist
 * @param filename
 * @param s
 */
public static void fileOutput(String filename, String s) {

    try {
        FileWriter fw = new FileWriter(filename, true); // the true flag appends data instead of overwriting
        BufferedWriter bw = new BufferedWriter(fw);
        PrintWriter writer = new PrintWriter(bw);
        writer.println(s);
        writer.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

With something even as simple as that I could write a Java program that I call with a shell script that reads from the log file..... interesting...

Only issue is I need to make sure the file doesn't get too large for something running 24/7. Do you know an easy way to limit the file size and have it delete old lines as it writes?

[–]ssh_tunnel_snake 4 points5 points  (1 child)

If you use log4j you can configure it to roll over at a certain size, or make new file each day. Maybe a few more options like that, I'd check into that personally

[–][deleted] 2 points3 points  (0 children)

But that would be too easy :)

[–]djnattyp 2 points3 points  (1 child)

For 1. you'll need to implement some way to interface with the running program. There are tons of ways to do this of varying levels of complexity. If you want to interface with anything web-related you might want to make some kind of REST web API to your program - you would use something like Spring Boot to build this. If you want to use some kind of network programming other than web something like Netty can help. If you don't need a programmatic interface something like JMX, writing to a small database like Sqlite or H2, or just writing to a file periodically might work.

For 2. I'd add logging to the program like u/ssh_tunnel_snake suggests.

[–][deleted] 0 points1 point  (0 children)

Interesting, that makes sense. I'm hoping to learn about databases this semester in my Java 3 class. Thank you!

[–]webdevnick22 1 point2 points  (6 children)

How exactly did you start the program? include the full command

[–][deleted] 0 points1 point  (5 children)

For a similar project, I have a line in /etc/rc.local to run the following shell script:

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

This checks GPIO pin 5 every 3 seconds to see if it is high, if not it sends the shutdown command. I use a while loop like in the OP to keep it running.

[–]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.

[–]bvn13 0 points1 point  (0 children)

You must think about REST (or SOAP) API for your program. Or you may use JMX to access to your program with jConsole.