Mastering Automation: Adding Cron Jobs to Azure App Service for Linux

Hasitha Charaka šŸ”µ
4 min readAug 18, 2023

azure tux

Automating routine tasks is a key aspect of efficient application management, and cron jobs play a pivotal role in achieving this automation. When combined with the flexibility of Azure App Service for Linux, developers can streamline processes and enhance the performance of their applications. In this guide, we will explore the steps to add and manage cron jobs in Azure App Service for Linux, ensuring seamless automation and improved application management.

Accessing SSH in Azure App Service

Before we delve into adding cron jobs, itā€™s important to be familiar with how to access the SSH (Secure Shell) terminal within Azure App Service. SSH provides a secure way to connect to your applicationā€™s virtual machine, granting you direct command-line access. Hereā€™s how you can access SSH for your Azure App Service:

  1. Azure Portal Access:
  • Log in to the Azure portal (https://portal.azure.com).
  • Navigate to your Azure App Service instance.
  • In the left-hand menu, under the ā€œDevelopment Toolsā€ section, click on ā€œSSH.ā€

2.Using Azure CLI:

  • Open your command-line interface.
  • Log in to your Azure account using az login if you haven't already.
  • Use the following command to open an SSH session:
az webapp ssh --resource-group <resource-group-name> --name <app-name>

Once youā€™re connected to the SSH terminal, youā€™ll have direct access to your applicationā€™s file system and command line, allowing you to perform various tasks, including adding and managing cron jobs.

Installing and Configuring Cron

Before diving into adding cron jobs, ensure that the cron package is installed on your Linux system. Depending on your distribution, use the relevant package manager to install the cron package. For instance, on Debian/Ubuntu-based systems, execute the following command:

apt-get install cron

(Note: In an Azure environment, you might not need to use ā€œsudoā€ before the command.)

For Red Hat/CentOS-based systems, the command would be:

yum install cronie 

Next, verify the path and permissions of the cron binary:

which cron

Restart the cron service after installing or making changes:

  • On systemd-based systems:
sudo systemctl restart cron
  • On SysVinit-based systems:
sudo service cron restart

Adding Cron Jobs

  1. Open Crontab for Editing: Use the following command to edit the crontab:
crontab -e

2. Add a Cron Job:

minute hour day month day_of_week command_to_run

For example, to run a script named myscript.sh located in /home/site/wwwroot every day at 2:30 PM:

30 14 * * * /home/site/wwwroot/myscript.sh

3. Verify the Cron Job:

Confirm that the cron job is added by listing the crontab entries:

crontab -l

Adding API Calls to Cron Jobs:

Extend cron jobs by incorporating API calls. Open the crontab for editing:

crontab -e

Include the API call within the crontab to execute desired actions, such as making HTTP requests:

* * * * * curl -s http://your-api-url.com/endpoint >> /home/site/wwwroot/cron-api.log 2>&1

Viewing Current Running Cron Jobs

crontab -l

you will get following output

# Example Crontab - List of Scheduled Tasks

# Minute Hour Day Month Day of Week Command
# ---------------------------------------------------------
30 14 * * * /home/site/wwwroot/myscript.sh
0 2 * * 5 /home/site/wwwroot/backup.sh
*/15 * * * * /home/site/wwwroot/data_cleanup.sh

*Note*

Any data outsdie /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:
  GNU nano 5.4                                                                                        startup.sh                                                                                                  
1 #!/bin/bash
2
3 # Install cron
4 apt-get update -qq
5 apt-get install cron -yqq
6
7 # Add a cron job to call a URL every hour
8 (crontab -l ; echo "* * * * * curl -s https://pmc-api-dev-app.azurewebsites.net/api/test-cron >> /home/site/wwwroot/cron-api.log 2>&1") | crontab -
9
10 # Start the cron service
11 service cron start
12

2. Set ā€˜/home/startup.shā€™ as ā€˜startup commandā€™ in our azure panel

your app-service -> configuration -> general settings

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

Conclusion

Integrating cron jobs into Azure App Service for Linux empowers developers to automate tasks effectively, enhancing application management and performance. By following these steps, you can seamlessly add, configure, and manage cron jobs within your Azure environment, ensuring consistent and reliable task execution. Embrace the power of automation and take control of your applicationā€™s routine processes with the combination of cron jobs and Azure App Service for Linux.

May your journey of mastering cron jobs and Azure App Service lead to more streamlined operations, improved efficiency, and greater success in your development endeavors. Happy automating!

Happy Coding ā¤

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Hasitha Charaka šŸ”µ
Hasitha Charaka šŸ”µ

Written by Hasitha Charaka šŸ”µ

ā˜• Support me by buying me a Coffee šŸ’– ā€” https://buymeacoffee.com/hasithacharaka Web developer ā€” hasi94.github.io

No responses yet

Write a response