0

I'm developing a program that simply monitors a webpage for changes, and plays a sound file. I implemented similar to this answer using SwingUtilities.invokeLater and JOptionPane to make sure sound file is looped continuously.

Program works correctly. Now I would like to place it in crontab (on my Ubuntu Machine) and have it run every 30 minutes. That's where things stop working. I searched for why, and it looks like JOptionPane should be invoked from initial thread (which is the crontab process?). I understand that I just need to prevent main method from ending, but what would be the best approach (DataLine.Drain() ?).

rogul
  • 7
  • 2
  • .. what's a crontab? – Andrew Thompson Jul 22 '17 at 22:35
  • 1
    *"and it looks like JOptionPane should be invoked from initial thread (which is the crontab process?)"* - No, that's a misunderstanding, it should be invoked from the Event Dispatching Thread (some frameworks call it the main thread, but that would be a wrong assumption in Java/Swing) – MadProgrammer Jul 22 '17 at 23:33
  • while(running) {Thread.sleep(1000 * 60 * 30); invokeWhatsis();}? – Phil Freihofner Jul 23 '17 at 01:20
  • Can you specify how the the program fails? Is it not looping, not playing sound at all, throwing an exception, ...? What's the program's output? You mention *crontab*, so I assume this is on some sort of Linux. Which user is running the program? Does the user have access to a display server at all, so that it can open a `JOptionPane`? See https://stackoverflow.com/a/25182822/942774 for details – Hendrik Jul 24 '17 at 05:52
  • @hendrik - (Apologies for late reply from my end, but) Thanks! That solved my problem. I did not know how access to a display worked for cron. Useful stuff! – rogul Aug 12 '17 at 19:58
  • No problem. I added a corresponding answer. You might want to edit your question to include the platform (Linux, I assume). Perhaps also add crontab and linux as tags, as that's what this is really about. – Hendrik Aug 12 '17 at 21:14

1 Answers1

0

You are trying to display something without making sure a display server is present and accessible (which is not a given for a process started by cron).

You can specify a display server by setting the DISPLAY variable to :0. For example:

* * * * * export DISPLAY=:0; YOUR_PROGRAM

See also https://stackoverflow.com/a/25182822/942774

Hendrik
  • 5,085
  • 24
  • 56