Jump to content

Wikipedia:Reference desk/Archives/Computing/2020 October 1

From Wikipedia, the free encyclopedia
Computing desk
< September 30 << Sep | October | Nov >> October 2 >
Welcome to the Wikipedia Computing Reference Desk Archives
The page you are currently viewing is a transcluded archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.


October 1

[edit]

Java program that runs indefinitely on a computer

[edit]

Suppose I wanted to make a Java program that updated a database every day at midnight. Whenever I make a program, I click run code, it runs, and then it stops. What's the process for making a program that always runs in the background and is able to execute a task at a specified time? Whatever the process is, would it still work if my computer is off? --PuzzledvegetableIs it teatime already? 20:46, 1 October 2020 (UTC)[reply]

Puzzledvegetable, the typical way to do this is using Task Scheduler on Windows, or cron(8) or anacron(8) on Linux. You can set them up to fire off the program at a given time on a schedule. It would be feasible to have a long-running service/daemon, written in Java, that did the task at a preset time every day, but this would not normally be necessary unless you have to retain some state or data structures on a long-running basis and it would be computationally expensive to start the process afresh every day.
Naturally, none of this applies when your computer is off, it won't be running any programs at that time. Elizium23 (talk) 21:02, 1 October 2020 (UTC)[reply]
I don't know if what I'm about to say makes any sense at all, but is there any way to host a Java program on a cloud server so that it's not dependent on a particular computer being on at a particular time? --PuzzledvegetableIs it teatime already? 21:48, 1 October 2020 (UTC)[reply]
Puzzledvegetable, yes, there are all kinds of ways to do that. Via Amazon EC2, or Microsoft Azure, or Digital Ocean, any VPS server could run such a platform for you. You could probably manage it for no charge at all. You would probably be on the hook as the sysadmin for the machine, managing its OS install, patches, and general upkeep. Elizium23 (talk) 21:54, 1 October 2020 (UTC)[reply]
Normally you'd leave the server running all the time. There are so-called serverless platforms that activate when a query arrives, but they aren't what you want for this. For personal use, among the hosts mentioned, I'd go with Digital Ocean. There's probably a DO user or two here who can give you a referral code, which gives you $100 of DO credit that is good for two months. That is enough for a pretty big instance. After that, it's $5/month for a 1GB server, which is ok though not the cheapest for this type of product. More savings are possible if your program is memory hungry and you need a bigger server. If you don't mind being hosted in Europe, your best bet is hetzner.com which is around $3/month for a 2GB instance in Germany. 2601:648:8202:96B0:0:0:0:DDAF (talk) 00:11, 2 October 2020 (UTC)[reply]
There is an elephant in the room here, and I'm going to point it out: network locality. If you have a Java daemon or crontab entry that updates a database on the local machine every day at midnight, then that's reliable and secure. On the other hand, if you have a server way out there in the cloud on a VPS running a long-lived daemon process, then you have to consider network access and network security. The cloud machine will need to reach into your LAN or Intranet and access your database server. This is fraught with difficulty, because you will need to ensure that the bad guys cannot also reach into your database. In addition, you will need to ensure uptime and availability of your service and the whole network in between. That's going to run you way more than $5/month. Elizium23 (talk) 00:15, 2 October 2020 (UTC)[reply]