cron作业忽略了python脚本中执行的docker命令
我有一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论