cron作业忽略了python脚本中执行的docker命令

发布于 2025-02-03 02:32:06 字数 1454 浏览 2 评论 0原文

我有一个Python脚本,它可以从Gitlab Repo中进行最新变化,如果仓库发生了变化,则可以重建容器。为了自动化这一点,我使用cron作业,触发了.sh脚本和此.SH脚本,我尝试执行Python脚本。 cron作业看起来像这样:* * * * * /home/user_name/job.sh。 SH脚本:

#!/bin/sh
export DISPLAY=:0
cd path_to_script
python3 gitpull.py

Python脚本:

#!/usr/bin/python3

import git
import pymongo
import subprocess

url = 'mongodb://host:27017/'
client = pymongo.MongoClient(url)
db = client['branch']

def rebuild_command(service_name):
    f = open("/var/log/test_py_job.log","w")
    subprocess.call(f"docker-compose up -d --no-deps --build {service_name}", shell=True,stdout=f)

def git_pull(path,collection_name):
    collection = db[collection_name]
    repo = git.Repo(path)
    pullc = repo.remotes.origin
    pullc.pull('branch')
    current_head = str(repo.head.commit)
    last_saved_head = ''
    for sha in collection.find({},{"last_head":1, "_id":0}):
            last_saved_head = sha['last_head']
    if current_head == last_saved_head:
        return False
    else:
        collection.delete_one({'last_head': last_saved_head})
        collection.insert_one({'last_head': current_head})
        if path == './path':
            rebuild_command('frontend')


git_pull('./path','collection_name')

问题在以下内容:如果我禁用Cron Job并执行Python脚本手动工作正常,但是如果我启用Cron Job仅执行GitPull()函数,则Docker命令在Rebuild(Rebuild()函数中执行不执行。我用chmod命令执行两个文件(job.sh,python脚本),docker在没有用户特权的情况下运行。

I have a python script which pulls recent changes from gitlab repo and rebuild containers if something changed in repo. To automate this I use cron job, which triggers .sh script and inside this .sh script I try to execute python script. Cron job looks like this: * * * * * /home/user_name/job.sh . Sh script:

#!/bin/sh
export DISPLAY=:0
cd path_to_script
python3 gitpull.py

Python script:

#!/usr/bin/python3

import git
import pymongo
import subprocess

url = 'mongodb://host:27017/'
client = pymongo.MongoClient(url)
db = client['branch']

def rebuild_command(service_name):
    f = open("/var/log/test_py_job.log","w")
    subprocess.call(f"docker-compose up -d --no-deps --build {service_name}", shell=True,stdout=f)

def git_pull(path,collection_name):
    collection = db[collection_name]
    repo = git.Repo(path)
    pullc = repo.remotes.origin
    pullc.pull('branch')
    current_head = str(repo.head.commit)
    last_saved_head = ''
    for sha in collection.find({},{"last_head":1, "_id":0}):
            last_saved_head = sha['last_head']
    if current_head == last_saved_head:
        return False
    else:
        collection.delete_one({'last_head': last_saved_head})
        collection.insert_one({'last_head': current_head})
        if path == './path':
            rebuild_command('frontend')


git_pull('./path','collection_name')

The problem is in following: If I disable cron job and execute python script manually everything works fine, but if I enable cron job executes only gitpull() function, docker command in rebuild() function doesn't execute. I made both files (job.sh, python script) executable with chmod command, docker runs without user privileges.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文