• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Writing to a crontab file

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1143
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic