Jayjay
Jayjay

Reputation: 148

How to run cronjobs on an azure linux hosted webapp?

Crontab command not found on Linux web app on azure,I'm trying to schedule a cronjob for a laravel application.

I'm trying to run a cronjob for my laravel web app.The solutions online suggest that I use webjobs for this, in my case it is blurred out, the reason for this I was told by the technician that it is because it's a Linux web app, I then have to run cronjobs using crontab. which I did, but once in a while I get "-bash crontab: command not found" which suggest that the whole configuration gets lost somehow.

Upvotes: 10

Views: 16730

Answers (7)

Scott McKenzie
Scott McKenzie

Reputation: 1

This solution is not as flexible as installing cron, but does not require the installation of additional software.

Create /home/startup.sh then set it as the startup command of your web app.

#!/bin/sh
(
  flock -n 9 | exit 0
  while true; do
    echo "$(date -Iseconds) Starting cron.php" >> /home/startup.log
    php /home/site/wwwroot/cron.php
    sleep 10m
  done
) 9>/home/site/locks/cron.php.lock &
  • Remove the "echo" line if you don't want logging.
  • Adjust the "php" line so that it executes your task
  • Change how often the task runs by adjusting the sleep delay

Upvotes: 0

jssDev
jssDev

Reputation: 993

We are warned when we open the console of our application by ssh:

Note: Any data outside '/ home' is not persisted

Therefore, a possible solution in this case is to launch a script at the start of the application, so that after each restart the cron service is installed and the work necessary to execute the Laravel scheduller tasks is created. I'll explain what has worked for me for my PHP + Laravel application hosted on Azure Linux Webapp:

1. Create the start script /home/startup.sh:

apt-get update -qq && apt-get install cron -yqq
(crontab -l 2>/dev/null; echo "* * * * * /usr/local/bin/php /home/site/wwwroot/MyAppFolder/artisan schedule:run >> /home/cronresult.txt 2>&1")|crontab
service cron start

Note: note that we are indicating that our PHP PATH is in the /usr/local/bin/php directory. We will have the output of the command in a file /home/cronresult.txt, which will help us in debugging any problem from its execution.

2. Set ‘/home/startup.sh’ as ‘startup command’ in our azure panel, as indicated by @HeyMan in his answer.

3. We will have to restart our application for the startup script to load.

Upvotes: 6

HeyMan
HeyMan

Reputation: 1845

Create a startup script, e.g. start.sh and add the following:

# install & start crontab
apt-get update -y
apt-get install -y cron
echo "* 04-17 * * * your_job.sh" | crontab -
service cron start

# don't forget to start your webapp service at the end of this script, e.g.:
python -m gunicorn your_application:app

Then add start.sh as startup script to your WebApp:

enter image description here

Note: There are two pitfalls to this approach:

  1. The script must be executable, so either install w/ unix and chmod 755 start.sh or use a git command (see SO).
  2. The 3pp (here crontab) is installed on every startup, thus you depend on external servers/repositories when starting the webapp.

Upvotes: 5

user1845084
user1845084

Reputation: 21

you need to create a bash script includes all the commands you want to run. then you refer to this in the configuration as a startup script.

It should work

Upvotes: 2

Dominique Broeglin
Dominique Broeglin

Reputation: 11

Alternatively, you might use the multi-container feature of Azure App Services and deploy the "CRON" part in a second container. Make sure you include all necessary components (cron & app) in the second container.

See Microsoft's Tutorial: Create a multi-container (preview) app in Web App for Containers for an example of multi-container application.

Upvotes: 1

Subbu
Subbu

Reputation: 663

As per the @Ajaykumar-MSFT suggestion, if your target is to run a task on a schedule you can use timer trigger for Azure function feature.

Upvotes: 0

AjayKumar
AjayKumar

Reputation: 3163

You could consider using Functions on Linux: https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-first-azure-function-azure-cli-linux

There is a feedback post on this - https://feedback.azure.com/forums/169385-web-apps/suggestions/32437156--linux-to-support-webjob-feature-on-webapp-for-co

You may use the Web App for containers with docker custom image with your Cron Jobs. And in addition, if you enable AlwaysOn for your web app for container, the alwaysOn pinger will keep the container running.

Also, based on your requirement, you may consider using Azure App Service Windows Containers (Preview) and inside the Windows Container you can run custom software such as the WebJobs runtime. Here you can follow the QuickStart to run a Windows Container in App Service: https://learn.microsoft.com/en-us/azure/app-service/app-service-web-get-started-windows-container

Upvotes: 0

Related Questions