Running a Scheduled Task Written in Java on a Linux Server

Running a scheduled task written in java on a linux server

You can write a shell script for executing your Java utility (you might need to add classpath and other environmental vairables) and put it in your crontab.

Here is quick crontab reference.

Developing Scheduled tasks in Java and running on Linux server

You can build the app using Spring Boot and run it as a daemon:

https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html

And then use quartz to schedule tasks

schedule management

Instead of using java.util.Timer alone, try using it with TimerTask. There is a good article from IBM on this.

Have a look at this link: http://www.ibm.com/developerworks/java/library/j-schedule.html

The code is also shared and seems to work for trivial routine job.

How to keep running my scheduler regardless of my server

I am not quite sure what type of server environment in question. But I assume yours might be some application server or web server. In that case Scheduled jobs can be creating batch job and schedule it through cron Tab on Linux/Unix (assuming your application is running on Unix/Linux box).

Backend process VS scheduled task

By scheduled task, do you mean triggered by the system scheduler, or as a scheduled thread in the existing backend processes?

To capture unexpected termination or unresponsive states you would be best running a separate process rather than a thread. However, a scheduled thread would give you closer interaction with the owning process with less IPC overhead.

I would implement both. Maintain a record of the local state in each backend process, with a scheduled task in each process triggering a thread to update the current state of that node. This update could be fairly frequent, since it will be less expensive than communicating with a separate process.

Use your separate "monitoring app" process to routinely gather the information about all the backend processes. This should occur less frequently - whether the process is running all the time, or scheduled by a cron job is immaterial since the state is held in each backend process. If one of the backends become unresponsive, this monitoring app will be able to determine the lack of response and perform some meaningful probes to determine what the problem is. It will be this component that will then notify your SMS/Email utility to send a report.

Intermittently update database using java program every 5 minutes on Linux machine

You mention two different "technologies" that have different approach to your disposal, one is Java, the second is linux.

On linux you have

  1. CRON - this is a 'linux' way to execute scheduled tasks (scripts) so you can run your java program/whatever you want each 5 minutes. Defined correctly you shouldn't be bothered by the service reboot, cron will run automatically upon the service reboot and will handle your java program. You might want to use anacron instead, but you should talk to your system administator for the details here.

  2. If you don't want to run scheduled job in linux and wan't to manage scheduling in Java, you indeed can write a program that will run in the endless loop by means of that the process will run forever, but in this case I would suggest you to use java.util.Timer instead of sleeps, or if you don't mind to use thirdparties - than go for Quartz, its a full fledged Java scheduler. But then in order to make this program running upon reboot you should define it as a service in linux. Basically, you put some script (defined properly) in a predefined folder, register the service and linux upon reboot will call this script, so that you'll be able to run if you want.

    service myscheduledtask start/stop/status

The technical details slightly change depending on Linux distribution but the infrastructure for running scheduled task exists in any distribution for sure
For example on Redhat you should look at chkconfig command as an entry point.

Now what approach is better? There is no silver-bullet solution here. I would probably go with cron based approach, because its easier. But being myself a java programmer I realize that java will provide a crossplatform solution (if one day you'll move to another operating system, than you'll have to define the scheduling mechanism again).
On the other hand its always better to have less processes, so an always-running process is a drawback here :) So, the choice is yours

Hope this helps



Related Topics



Leave a reply



Submit