无法在crontab docker中执行.sh文件 [英] Unable to execute .sh file in crontab docker
问题描述
我正在研究Docker,Crontab,Python& CentOS的。我需要读取Python脚本中的docker compose环境变量。
I am working on a project which is on Docker, Crontab, Python & CentOS. I have a requirement to read docker compose environment variables in Python script.
我有以下Docker File,Crontab和Shell Wrapper文件
I have the below Docker File, Crontab and Shell Wrapper files
Docker文件
FROM centos:latest
RUN yum -y install crontabs
RUN yum update -y \
&& yum install -y https://centos7.iuscommunity.org/ius-release.rpm \
&& yum install -y python36u python36u-libs python36u-devel python36u-pip \
&& yum install -y which gcc \
&& yum install -y openldap-devel
# pipenv installation
RUN pip3.6 install pipenv
RUN ln -s /usr/bin/pip3.6 /bin/pip
RUN rm /usr/bin/python
# python must be pointing to python3.6
RUN ln -s /usr/bin/python3.6 /usr/bin/python
RUN pip install --upgrade pip
RUN pip install pymysql
RUN pip install pymongo
RUN pip install lxml
RUN pip install pyyaml
RUN pip install envs
# comment out PAM
RUN sed -i -e '/pam_loginuid.so/s/^/#/' /etc/pam.d/crond
RUN printenv | grep -v "MAINDB_CONNECTIONSTRING" > /root/project_env.sh
ENV SCRIPT_DIR="/opt/Importer"
WORKDIR ${SCRIPT_DIR}
COPY Importer/* ./
#Add your cron file
ADD Importer/crontab /etc/cron.d/crontab
RUN chmod 0644 /etc/cron.d/crontab
RUN chmod +x /opt/Importer/ShellWrapper.sh
#This will add it to the cron table (crontab -e)
RUN crontab /etc/cron.d/crontab
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
CMD crond && tail -f /var/log/cron.log
Crontab文件
#!/bin/bash
* * * * * /usr/bin/sh /opt/Importer/ShellWrapper.sh
# Mandatory blank line
ShellWrapper.sh
ShellWrapper.sh
#/bin/bash
/usr/bin/python Importer.py
实际结果-当我直接在docker / bin / bash中执行ShellWrapper.sh时,正在执行python脚本,但未使用Crontab执行相同的ShellWrapper.sh。
Actual result - When I execute the ShellWrapper.sh in docker /bin/bash directly, the python script is being executed but the same ShellWrapper.sh is not being executed with Crontab.
预期结果-Crontab应该执行ShellWrapper.sh,而ShellWrapper.sh文件将执行Python脚本
Expected result - Crontab should execute ShellWrapper.sh and ShellWrapper.sh file will execute Python script
您能帮上忙吗?
推荐答案
在cron.d中,您需要指定用户:
In cron.d, you need to specify the user:
* * * * * root /usr/bin/sh /opt/Importer/ShellWrapper.sh
另请参见:有关类似问题使用cron.d文件夹
这篇关于无法在crontab docker中执行.sh文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!