Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing to a crontab file

 
Mark Kennedy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic