Centos 7 Monitor Directory With Python3

1 minute read

Description:

So the other day, I wrote a post about using the open source version of pwsh to monitor a path, but today I wanted to convert that over to python3. Here is how I did it:

To Resolve:

  1. Following this guide I installed python 3 on my RHEL 7 box by running:

    1
    2
    3
    4
    
    subscription-manager repos --enable rhel-7-server-optional-rpms \
    --enable rhel-server-rhscl-7-rpms
    yum -y install @development
    yum -y install rh-python36
    
  2. Next, I copied over my monitorcsv.py that I have been building in vscode:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    cd /myuser/scripts
    mkdir csv
    cd csv
    scl enable rh-python36 bash
    python -m venv /myuser/scripts/csv/venv
    source /myuser/scripts/csv/venv/bin/activate
    pip install --upgrade pip
    python3 -m pip install requests
    python3 -m pip install python-dotenv
    vi monitorcsv.py
    #paste in script
    chmod 700 monitorcsv.py
    exit
    
  3. Next, I created a caller bash script:

    1
    2
    3
    4
    5
    6
    7
    
    vi /myuser/scripts/monitorcsv.sh
    # paste in:
    #!/bin/bash
    scl enable rh-python36 bash
    source /myuser/scripts/csv/venv/bin/activate
    python /myuser/scripts/csv/monitorcsv.py
    chmod 700 csv.sh
    
  4. Create an .env file for python to load environmental variables within the env:

    1
    2
    
    EMAIL="somePass"
    KEY="someKey"
    
    • Make sure to add at top of your python script:
    1
    2
    3
    
    from dotenv import load_dotenv
    
    load_dotenv()
    
  5. Now just update your service file to point to /myuser/scripts/monitorcsv.sh instead. Ensure you do a systemctl daemon-reload followed by systemctl restart yourservice.path and systemctl restart yourservice for changes to take effect.

Tags:

Updated:

Comments