This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hello, all: I've got a struts app that needs to schedule a task to run at a user specified time. I.e., the user can specify, via a struts JSP page, that they want a given task run at a given time. Based on this input, I want the Action bean to write a line to the crontab file. My question is, how can I write to the crontab file directly from Java? I usually edit crontab files from the commandline with crontab -e which then opens the crontab in vi. The crontab file isn't directly editable without using the crontab command, AFAIK. Are there any classes or techniques that can be recommended to do this? Thanks so much! Mark Kennedy
Hi Mark, First of all, do you know about the "java.util.Timer" and "java.util.TimerTask" classes (that are part of the JDK since version 1.3)? Are they not suitable? As a suggestion for implementing your "crontab" solution: 1. "crontab -l" dumps the contents of the "crontab" file. Save the output to some file, example: [PRE] crontab -l crontab.fil [/PRE] 2. Edit the file "crontab.fil" using the file manipulating classes from the "java.io" package, or using appropriate UNIX utilities (which you can launch from java using the "java.lang.Runtime" class). 3. Update your "crontab" using the following command: [PRE] crontab crontab.fil [/PRE] Hope this helps. Good Luck, Avi.
In the event you need to use crontab and not Timer (e.g. if the JVM may not be running later, but the OS will) you may want to use the Runtime exec() mthod to invoke the commands Avi suggests, from Java.