Skip to main content

Scheduling Tasks with Cron Jobs in LAMP

While using LAMP, you may wish to schedule events or scripts to run on a regular basis - for example, analysis scripts or scripts that schedule study participants for different studies based on different criteria.

If you are using Portainer to support LAMP, you can use the following method and the swarm-cronjob project to schedule these scripts to run regularly.

1. Create a manager service

To create a manager service, add the following service to a stack (if you do not have a dedicated cron stack, you may wish to create one).It should look something like this:

image: crazymax/swarm-cronjob
- /var/run/docker.sock:/var/run/docker.sock:ro
TZ: "America/New_York"
  • Image should be crazymax/swarm-cronjob
  • Volume should be /var/run/docker.sock:/var/run/docker.sock:ro
  • The TZ env variable should be set to your local timezone - this will affect the times your scripts run if you specify an hour for them to run.

Only do this step ONCE, regardless of how many services you plan to create.

2. Create the script service.

Second, create the service that will run your script. It should look similar to this:

image: python:3.8
command: ['python','/script/']
TZ: "America/New_York"
ANALYSIS_NAME: "My Analysis"
- /usr/bin:/script
- /var/run/docker.sock:/var/run/docker.sock:ro
mode: replicated
replicas: 0
condition: none
swarm.cronjob.enable: "true"
swarm.cronjob.schedule: "0 3 * * *"
swarm.cronjob.skip-running: "false"


  • To install additional modules not included with the standard python3.8 image (for example), you may wish to run pip install commands through a shell script.
  • Use swarm.cronjob.schedule to enter your cron string

Do this step for each service you plan to create. After you update the stack, the script should begin running as scheduled. The service above, for instance, will run at daily at 3 AM.