As mentioned by Drimity you can put the java program run command in a script and schedule the script.
echo "start"
java YourClass
echo "end"
Its a good practice to redirect all output of your java program to a log file instead of System.out /System.err
However, there is a much better way if you're using Java EE and on a Java EE server. Server will schedule the job for you. All you need is an annotation.
import javax.ejb.Schedule;
import javax.ejb.Singleton;
@Singleton
public class ShedulerEjb {
/**
* This scheduler method runs at 0200 hours (system time)
*/
@SuppressWarnings("unused")
@Schedule(hour="2", minute="0", second="0")
private void doStuff(){
//Do your stuff
}
}
Underneath it uses operating system's timer services.
PS: If you don't make your bean singleton
and at the scheduled time, there are more than one instances (say n
) of your EJB bean, the scheduled method will be executed n
number of times.