如何让 cronjob 每分钟运行一次

如何让 cronjob 每分钟运行一次

我按照网上关于 crontab 使用的建议,然后运行sudo crontab -e,我的 cron 文件是:

[email protected]

* * * * * /home/cchilders/scripts/python/scripts-in-progress/update_projects.py

它没有给我发送任何邮件,更糟糕的是,我正在检查我更改的项目的 git 日志,但 update_projects 根本没有运行。当我手动运行它时,它按预期工作。从我读到的所有内容来看,我真的认为 cronjobs 很简单,只需要一个时间(5 个符号)和一个脚本路径。

我尝试测试脚本调用以确保 python 路径和脚本路径都正确。为此,我制作了 crontest.sh:

#!/bin/bash

/usr/bin/python /home/cchilders/scripts/python/scripts-in-progress/update_projects.py

从终端调用时,Crontest.sh 可以工作。我还没有将其上传到用户 crontab、sudo crontab,但它仍然没有运行。

用户 crontab:

* * * * * /home/cchilders/scripts/bash/crontest.sh

* * * * * /home/cchilders/scripts/python/scripts-in-progress/update_projects.py

命令crontab:

[email protected]

* * * * * /home/cchilders/scripts/bash/crontest.sh

* * * * * /home/cchilders/scripts/python/scripts-in-progress/update_projects.py

如果路径正确,那这有什么问题?谢谢

update_projects.py 如下所示,并且可以从终端运行:

#!/usr/bin/env python

import os, sys, time, subprocess
from os.path import expanduser

HOME = expanduser('~')

print 'running?


def call_sp(command, **arg_list):
    #run that
    p = subprocess.Popen(command, shell=True, **arg_list)
    p.communicate()


def get_project_path():
    i = 0
    for root, dirs, files in os.walk(HOME):
        if i >= 2:
            return os.path.join(HOME, "projects")
        i += 1
        for this_dir in dirs:
            if this_dir == "django_practice":
                return os.path.join(HOME, "django_practice")

def update_projects(home_path):
    i = 0
    for root, dirs, files in os.walk(home_path):
        for this_dir in dirs:
            if this_dir.startswith("."):
                continue
            full_path = os.path.join(root, this_dir)
            print full_path
            time.sleep(2)

            is_git_project = False
            j = 0
            for subroot, subdirs, subfiles in os.walk(full_path):
                if j >= 1:
                    break
                j += 1
                if not ".git" in subdirs:
                    break
                else:
                    is_git_project = True

            if not is_git_project:
                continue

            d = {'cwd': full_path}
            print 'git pull from {}'.format(full_path)
            call_sp('git pull', **d)
            time.sleep(2)
            call_sp('git add -A', **d)
            call_sp('git commit -m "automatic update"', **d)
            call_sp('git push', **d)
        dirs[:] = []

ppath = get_project_path()
update_projects(ppath)

答案1

将 /usr/bin/python 放入 crontab 条目中。

* * * * * /usr/bin/python /home/cchilders/scripts/python/scripts-in-progress/update_projects.py

相关内容