If I am using a load balancer with two Linux VMs, how can you set up a cronjob?

Coooper 0 Reputation points
2023-05-09T17:35:12.5033333+00:00

Typically, I use Linux cron jobs to automate tasks. However, when using two VMs with an Azure load balancer, I would need to run a cron job on each VM, resulting in two separate jobs. This could lead to incorrect matches. Additionally, I use process services like Supervisor on each VM. How should I structure the backend process if I decide to use an additional VM with a load balancer?

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,873 questions
Azure Load Balancer
Azure Load Balancer
An Azure service that delivers high availability and network performance to applications.
439 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jackson Martins 10,551 Reputation points MVP
    2023-05-09T17:40:30.1566667+00:00

    Hi Cooper

    In this scenario, it is essential to include the cron job within the Linux VM image. This ensures that the cron job is present upon provisioning the VM. However, if you require a unique and customized cron job for each machine, you should store the cron job files in an external directory. Additionally, create a script that downloads the cron job file and adds the necessary information for each machine.

    Get in touch if you need more help with this issue.

    --please don't forget to "[Accept the answer]" if the reply is helpful--

    0 comments No comments

  2. Prrudram-MSFT 24,546 Reputation points
    2023-05-10T05:55:15.04+00:00

    Hello @Coooper

    Thank you for reaching out to the Microsoft Q&A platform.

    If you are using two Azure VMs with an Azure load balancer, you can use a distributed task scheduler to ensure that the cron job runs only once and on the appropriate VM. This will help you avoid any incorrect matches that may occur due to running the cron job on both VMs.

    One way to achieve this is by using a distributed task scheduler like Apache Mesos or Kubernetes. These schedulers can help you manage and distribute tasks across multiple VMs, ensuring that the cron job runs only once and on the appropriate VM.

    In addition to using a distributed task scheduler, you can also use a process manager like Supervisor to manage the cron job process on each VM. This will help you ensure that the process is running correctly and can be easily restarted if necessary.

    Overall, the key to structuring the backend process when using an additional VM with a load balancer is to use a distributed task scheduler and a process manager to manage and distribute tasks across multiple VMs. This will help you ensure that the cron job runs only once and on the appropriate VM, while also providing you with the necessary tools to manage and monitor the process.

    Please accept answer and upvote if the above information is helpful for the benefit of the community.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.