question

JasonZwanepoel-5675 avatar image
0 Votes"
JasonZwanepoel-5675 asked ·

Crontab on Linux Webapp Container

Hi.

I'm running a Laravel Application in my container and require the scheduler to run a command every minute as seen here.
https://laravel.com/docs/7.x/scheduling#introduction I'm quite new with container and docker as well as Azure Services.

I'm building my application trough a deployment slot on my container using Azure Dev-ops, this uses the docker file to build my application container and run it then deploy to the slot. currently stuck with being able to run my scheduler trough a cron-job or command.

Hoping you might be able to assist me.

Kind Regards


azure-webappsazure-container-registry
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

SnehaAgrawal-MSFT avatar image
1 Vote"
SnehaAgrawal-MSFT answered ·

Thanks for asking question!

For Custom Containers you may follow below steps.

  1. Enable CRON job for custom containers:

  2. To Enable cron, add the following line to Dockerfile: RUN apt-get install -yqq cron

  3. And following line in the container init script (init_container.sh)
    (crontab -l && echo " * echo 'hello from cron' >> /home/site/wwwroot/cron1.txt")|crontab

  4. In this case we will write “hello from cron” to file /home/site/wwwroot/cron1.txt every minute. You can replace this with anything else you wish to run periodically.

  5. echo 'hello from cron' >> /home/site/wwwroot/cron1.txt

  6. for crontab syntax please refer to:

  7. List item

https://www.adminschoice.com/crontab-quick-reference

https://crontab-generator.org/

•For Blessed Images:

  1. Create a script file in /home directory of the App Service and trigger it via the startup command under Configuration blade.

  2. A test script which installs cron as well as create a CRON job:

!/bin/sh


apt-get update -qq && apt-get install cron -yqq

service cron start

mkdir /home/BackupLogs

(crontab -l 2>/dev/null; echo "/5 cp /home/LogFiles/.log /home/BackupLogs")|crontab

Hope this helps!

· 2 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @SnehaAgrawal-MSFT

Thank you for your comment, however like I said I'm pretty new to this whole Azure | Docker | Dev-ops environment.

I'm using Azure Pipelines to build my Laravel application with docker and release it to my staging slot.

In your comment I could not find where to add the line 3

Blockquote
3. And following line in the container init script (init_container.sh) (crontab -l && echo " * echo 'hello from cron' >> /home/site/wwwroot/cron1.txt")|crontab


If you could kindly help me with this step and how to do it in Azure pipelines, I would appreciate it.


8750-docker-example.txt


0 Votes 0 · ·

Thanks for reply! From your dockerfile it looks like you are running the application using the CMD. You can add an entrypoint script to your source and call it in the dockerfile. The entrypoint script would contain the script for the cron job.

You may refer to this on how to use entrypoint

Let me know if issue persists.


1 Vote 1 · ·