cron


Executing a Cron Job Within an Anaconda Environment

For those seeking to automate their Python scripts using cron jobs within an Anaconda environment, there’s a straightforward method to ensure your scripts run under the correct environment. This approach involves leveraging the capabilities of bash and the configuration of your cron environment to recognize and activate the necessary Anaconda environment. Here’s how to set it up:

  1. Export Anaconda Initialization to a Dedicated Configuration File First, isolate the Anaconda initialization snippet from your ~/.bashrc file by copying it to a new file, say ~/.bashrc_for_cron. This step ensures the cron environment can source the necessary configurations to activate Anaconda environments. Make sure that:
  • The file is readable by the user scheduling the cron job.
  • The file is secured against write access from other users to mitigate security risks.
  1. Configure Cron to Use Bash and Source the Anaconda Configuration Edit your crontab configuration by running crontab -e and prepend your cron jobs with two crucial lines:
   SHELL=/bin/bash
   BASH_ENV=~/.bashrc_for_cron

These lines configure cron to execute jobs using bash instead of the default sh shell and to source the Anaconda configurations from ~/.bashrc_for_cron. When invoked by cron, this setup ensures that bash is aware of the necessary environment to activate Anaconda environments.

  1. Activate Anaconda Environment Before Executing Your Script When scheduling your Python script in crontab -e, prefix the command with the conda activate instruction to switch to your desired Anaconda environment. For instance, to run a script at 12:30 AM every day within a specific Anaconda environment, your cron job entry would look like:
   30 0 * * * conda activate opencv_env; python /path/to/script.py

This ensures that the script executes within the context of the specified Anaconda environment, opencv_env, leveraging any dependencies or configurations defined within that environment.

By following these steps, you can seamlessly schedule and run Python scripts under specific Anaconda environments, leveraging cron’s scheduling capabilities while ensuring the correct environment and dependencies for your scripts.


Find all git repositories and perform a pull operation on them.

The following command will find all git projects in your home folder and perform a pull operation on them.

find ~ -name ".git" -type d -exec bash -c "echo '{}' && cd '{}'/.. && git pull" \;

The above command is based on finding the .git folders that exist in any clone of a git repository. Once a .git folder is found, it will navigate to its parent folder where it will perform the pull request.

Bonus – Automate the procedure using a cron job

The following entry in crontab allows us to periodically perform a pull request on all the cloned repositories that we have in a specific folder. Specifically, it will perform this operation once every five minutes.

*/5    *    *    *    *    cd /home/bytefreaks/Projects; find . -name ".git" -type d -exec bash -c "echo '{}' && cd '{}'/.. && git pull" \; &> /tmp/bf.git.log

Please note that it would be easier to use an ssh key that does not have a password for this automation.
If you do not, the you will need to either pass the password via this configuration line (not recommended) or have a key agent running to provide the password for the key.