'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.
Solution 1:[1]
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:
Note: There are two pitfalls to this approach:
- The script must be executable, so either install w/ unix and
chmod 755 start.shor use a git command (see SO). - The 3pp (here crontab) is installed on every startup, thus you depend on external servers/repositories when starting the webapp.
Solution 2:[2]
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
Solution 3:[3]
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.
Solution 4:[4]
You could consider using Functions on Linux: https://docs.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://docs.microsoft.com/en-us/azure/app-service/app-service-web-get-started-windows-container
Solution 5:[5]
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.
Solution 6:[6]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | HeyMan |
| Solution 2 | user1845084 |
| Solution 3 | jssDev |
| Solution 4 | AjayKumar-MSFT |
| Solution 5 | Subbu |
| Solution 6 | Jeremy Caney |

