Python脚本在被cron调用时不运行后台进程

2024-04-26 06:59:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个由cron运行的python脚本:

"*/5 * * * * python /home/alex/scripts/checker > /dev/null &";

它有几个目的,其中之一是检查ps列表中的某些程序,如果它们不在那里就运行它们。问题是,cron运行的脚本在后台没有正确执行程序,所有程序都在ps列表中,如下所示:

/usr/bin/python /home/alex/exec/runnable

所以它们看起来像python脚本。当我手动启动python脚本时,它似乎在后台正确地执行runnable,但是对于cron,什么都不起作用。你知道吗

下面是代码示例:

def exec(file):
        file = os.path.abspath(file)
        os.system("chmod +x " + file)
        cmd = file
        #os.system(cmd)
        #subprocess.Popen([cmd])
        subprocess.call([cmd])

我尝试了不同的方法,但似乎没有什么是正确的。你知道吗

Some code update:
pids = get_pids(program)
if pids == None:
    exec(program)
    print 'Restarted'

Tags: 程序脚本cmdhome列表ossystemcron