Issue
I am trying to run another Python script(service_scheduler.py) along with the one which runs the app server( main.py) which is actually a scheduler. It will schedule a job and it will be called at a regular time interval. The problem is when I add this to my bash script neither job gets scheduled nor the app server runs.
Here is my bash script(svc_invoke.sh):
#!/bin/bash
cd /opt
/opt/env/bin/python3.11 main.py &
/opt/env/bin/python3.11 /api_topics/services/service_scheduler.py &
status=$?
if [ $status -ne 0 ]; then
echo "Failed to start python process: $status"
exit $status
else
echo "Started python $status"
fi
This one works, app server goes up and running:
#!/bin/bash
cd /opt
/opt/env/bin/python3.11 main.py
status=$?
if [ $status -ne 0 ]; then
echo "Failed to start python process: $status"
exit $status
else
echo "Started python $status"
fi
And here is my Dockerfile:
FROM python:3.11-slim
RUN apt-get update && \
apt-get install -y gcc && \
apt-get clean;
# Add files
COPY topic_modelling/ /opt
COPY svc_invoke.sh /svc_invoke.sh
RUN chmod -R 755 /svc_invoke.sh
RUN cd /opt
RUN python3.11 -m venv /opt/env
RUN /opt/env/bin/python3.11 -m pip install --upgrade pip
RUN /opt/env/bin/python3.11 -m pip install wheel
RUN /opt/env/bin/python3.11 -m pip install -r /opt/requirements.txt
ENV PYTHONUNBUFFERED=1
# Expose Port for the continuous learning
EXPOSE 8000
# Run the service
CMD ./svc_invoke.sh
I am new to Docker and shell scripting and I am stuck here. Any help will be highly appreciated.
Thanks
Solution
Try this:
#!/bin/bash
cd /opt
/opt/env/bin/python3.11 main.py & mainpid=$!
/opt/env/bin/python3.11 /api_topics/services/service_scheduler.py & schedpid=$!
status=0
wait $mainpid ; (( status |= $? ))
wait $schedpid ; (( status |= $? ))
if [ $status -ne 0 ]; then
echo "Failed to start python process: $status"
exit $status
else
echo "Started python $status"
fi
When you move the python scripts into the background with &
the main script is allowed to exit before they finish, which in turn would allow Docker to exit. If you want them both to have an opportunity to finish running then you need to wait
for them.
Answered By - tjm3772 Answer Checked By - Senaida (WPSolving Volunteer)