0

I've a main program which performs some task in Java. I want to run it as a batch job & due to our policies, I'm not allowed to use crontab where I can configure it.

What's the alternate option for me? How can I schedule my Java program to run once a day??

Any sample wrapper code will be helpful!

Thanks!

6 Answers 6

3

you can use at - execute commands at a later time
and/or combine it with sleep - delay for a specified amount of time
on a loop, running in the background

for example, say you have the follow script

#!/usr/bin/env bash
while true; do
    java -jar your.jar.file
    sleep 24h # wait one day
done

run it as

bash script && disown &
Sign up to request clarification or add additional context in comments.

Comments

2

I'm not sure you want to schedule your job in Java or Bash. If you try to implement in Java, you can try to create a Thread for your job, and let the thread sleep for a period:

class repeatTask extends Thread
{
    int repeatTime;

    public repeatTask(int repeatTime)
    {
        this.repeatTime = repeatTime;
    }

    public void run()
    {
        while(true)
        {
            try {
                Thread.sleep(repeatTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            /* Some action codes here*/
        }
    }
}

And also you can use Timer class, which could also let you create a thread:

public void start()
{
    timerSchedule();            
}

public void timerSchedule()
{
    Timer timer = new Timer();

    timer.schedule(new TimerTask() {
        public void run() {
            /*Action codes here*/
        }
    }, /*Repeat Time here*/);
}

Comments

2

... due to our policies, I'm not allowed to use crontab where I can configure it.

Clearly, there must be some reason for this policy, though it is not obvious what it is. Any alternative scheme could potentially fall foul of the same reasoning that caused them to forbid cron. (Certainly, some of the alternatives proposed above are likely to be worse than cron.)

The best thing to do is to ask the people who set this policy to tell you the approved way to schedule a job. Or at least ask them to explain why the use of cron is forbidden so that you have some idea what to avoid in any alternative scheme you come up with.

(OK, it is possible that the policy is a stupid knee-jerk reaction to some problem in the past. But, you still need to understand what the problem was ...)

Comments

1

You can use java timer tasks

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/TimerTask.html

Comments

1

I'm a big fan of cron4j for things like this: see if that works for you. You can use that for the scheduling bits, and just run a standard Java program.

Comments

0

You could also look at Quartz Scheduler. It's scheduling formats are very similar to crontab.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.