Sample Header Ad - 728x90

Cron issues when running python script in TMUX for a different conda environment

0 votes
1 answer
79 views
I have been trying to troubleshoot this issue for a while now and would like to ask for your help. Let's say that I need to run model.py inside a tmux session. Here are my steps for this to work: 1. Create tmux session 2. Activate conda environment model_env 3. Run model.py This will run using the packages that I need (mostly for data science work, ex: numpy and pandas) and will eventually run the script without any errors. However, I've been trying to schedule this run using cron and encountered the following: 1. I print debugged the script and saw that the packages used are still using my "base" env and not my conda model_env. 2. This will in turn, generate errors since the numpy package is incorrect. This is how my .sh file looks like:
#!/bin/bash

# Activate conda environment
conda activate model_env

# Name of the tmux session
SESSION_NAME="tmux_session"

# Path to your Python interpreter
PYTHON_PATH="/home/ubuntu/anaconda3/bin/python3"

# Path to your Python script
SCRIPT_PATH="/home/ubuntu/model/model.py"

# Log file
LOG_FILE="/home/ubuntu/cron_scheduler/logs/model/model_$(date +'%Y-%m-%d_%H-%M-%S').log"

# Start a new tmux session if it doesn't exist
tmux has-session -t $SESSION_NAME 2>/dev/null

if [ $? != 0 ]; then
    tmux new-session -d -s $SESSION_NAME
fi

# Run the script in the tmux session
tmux send-keys -t $SESSION_NAME "$PYTHON_PATH $SCRIPT_PATH >> $LOG_FILE 2>&1" C-m
Any help will be greatly appreciated. Thanks.
Asked by easymoneysniper (1 rep)
Sep 22, 2024, 11:54 AM
Last activity: Sep 22, 2024, 12:07 PM