Skip to content

Instantly share code, notes, and snippets.

@jericbas
Created January 17, 2023 10:48
Show Gist options
  • Save jericbas/faca7cd4fe872e2f8f5b40a7d0547adb to your computer and use it in GitHub Desktop.
Save jericbas/faca7cd4fe872e2f8f5b40a7d0547adb to your computer and use it in GitHub Desktop.
Here is an example of how to run a Python script inside a virtual environment using cron:
  1. First, activate your virtual environment using the command:
source /path/to/virtualenv/bin/activate
  1. Now, you can run your python script inside the virtual environment
python /path/to/script.py
  1. To run this script at a specific time or interval, you can add a cron job.
crontab -e

This command will open the cron configuration file, where you can add new jobs.

  1. Add a new line with the following format:
* * * * * /bin/bash -l -c '/path/to/virtualenv/bin/activate && python /path/to/script.py'

This line tells cron to run the command every minute (* * * * *), activate the virtual environment, and then run the Python script. You can change the values in the first 5 fields of the line to specify the schedule of the cron job.

  1. Save the file and exit.

  2. Verify that the cron job was added by running

crontab -l

This will list all the cron jobs that are currently running.

  1. To remove a cron job you can use `
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment