管理。crontab中的py命令不起作用

时间:2022-06-12 23:18:55

I have created a executeable script .sh which contains code to run a django managemenet command.

我创建了一个可执行的脚本.sh,其中包含运行django managemenet命令的代码。

cron.sh

cron.sh

#!/bin/sh
. /path/to/env/activate
cd /path/to/project
/path/to/env/bin/python manage.py some_command

I can confirm this script and manage.py command is working by executing it directly on terminal

我可以确认这个脚本并进行管理。py命令通过直接在终端上执行来工作

$ /path/to/cron.sh

美元/路径/ / cron.sh

When i do it same via crontab its not working as expected.

当我用crontab的方式做同样的事情时,它不像预期的那样工作。

** What am i doing wrong ?? I can confirm there is nothing wrong with crontab, it executing the cron.sh file but path/to/env/bin/python manage.py some_command is not working as expected.

我做错了什么?我可以确认crontab没有问题,它执行cron。sh文件但是路径/to/env/bin/python管理。py some_command没有正常工作。

cron log also showing

cron日志也显示

CRON[14768]: (root) CMD /path/to/cron.sh > /dev/null 2>&1

I am using bitnami django ami (ubuntu 14.04.5 LTS)

我正在使用bitnami django ami (ubuntu 14.04.5 LTS)

Update

更新

After removing /dev/null i am getting this error now

在删除/dev/null之后,我现在得到了这个错误

"Cannot locate wrapped file"

2 个解决方案

#1


1  

It seems that it is a PATH problem. I do not know if django uses specific paths that must be set but AFAIK the crontab PATH is really limited due to security reasons. Just to check if that is the problem you could do in a shell terminal the following:

这似乎是一个路径问题。我不知道django是否使用了必须设置的特定路径,但是由于安全原因,crontab路径确实受到了限制。只是为了检查这是否是你在shell终端可以做的问题。

echo $PATH

回声路径美元

You will get a complete PATH for instance:

你会得到一个完整的路径,例如:

/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl

/usr/local/sbin:/ usr /地方/ bin:/ usr / bin:/ usr / lib / jvm /违约/ bin:/ usr / bin / site_perl:/ usr / bin / vendor_perl:/ usr / bin / core_perl

In your crontab, put it above your code:

在crontab中,将其置于代码之上:

PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl

路径= / usr /地方/ sbin:/ usr /地方/ bin:/ usr / bin:/ usr / lib / jvm /违约/ bin:/ usr / bin / site_perl:/ usr / bin / vendor_perl:/ usr / bin / core_perl

Tell me if this works. If does, try to purge the provided PATH or even better provide absolute locations in your code.

告诉我这是否有效。如果是,尝试清除所提供的路径,或者更好地在代码中提供绝对位置。

I have to say that I don't know if you can perform a cd in the cron like this. I always used absolute paths or cd /some/dir && /path/to/script args.

我不得不说,我不知道你是否能像这样在cron上表演cd。我总是使用绝对路径或cd /some/dir & /path/to/script args。

P.S: I cannot make comments yet, for this reason I put it in an answer.

P。S:我现在还不能评论,所以我把它写了出来。

#2


0  

The problem is that your not using the script that Bitnami uses to load all the environment variables (/opt/bitnami/scritps/setenv.sh).

问题是您没有使用Bitnami用来加载所有环境变量的脚本(/opt/ Bitnami /scritps/setenv.sh)。

I would try using this script:

我会尝试使用这个脚本:

#!/bin/sh
. /opt/bitnami/scritps/setenv.sh
. /path/to/env/activate
cd /path/to/project
/path/to/env/bin/python manage.py some_command

#1


1  

It seems that it is a PATH problem. I do not know if django uses specific paths that must be set but AFAIK the crontab PATH is really limited due to security reasons. Just to check if that is the problem you could do in a shell terminal the following:

这似乎是一个路径问题。我不知道django是否使用了必须设置的特定路径,但是由于安全原因,crontab路径确实受到了限制。只是为了检查这是否是你在shell终端可以做的问题。

echo $PATH

回声路径美元

You will get a complete PATH for instance:

你会得到一个完整的路径,例如:

/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl

/usr/local/sbin:/ usr /地方/ bin:/ usr / bin:/ usr / lib / jvm /违约/ bin:/ usr / bin / site_perl:/ usr / bin / vendor_perl:/ usr / bin / core_perl

In your crontab, put it above your code:

在crontab中,将其置于代码之上:

PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl

路径= / usr /地方/ sbin:/ usr /地方/ bin:/ usr / bin:/ usr / lib / jvm /违约/ bin:/ usr / bin / site_perl:/ usr / bin / vendor_perl:/ usr / bin / core_perl

Tell me if this works. If does, try to purge the provided PATH or even better provide absolute locations in your code.

告诉我这是否有效。如果是,尝试清除所提供的路径,或者更好地在代码中提供绝对位置。

I have to say that I don't know if you can perform a cd in the cron like this. I always used absolute paths or cd /some/dir && /path/to/script args.

我不得不说,我不知道你是否能像这样在cron上表演cd。我总是使用绝对路径或cd /some/dir & /path/to/script args。

P.S: I cannot make comments yet, for this reason I put it in an answer.

P。S:我现在还不能评论,所以我把它写了出来。

#2


0  

The problem is that your not using the script that Bitnami uses to load all the environment variables (/opt/bitnami/scritps/setenv.sh).

问题是您没有使用Bitnami用来加载所有环境变量的脚本(/opt/ Bitnami /scritps/setenv.sh)。

I would try using this script:

我会尝试使用这个脚本:

#!/bin/sh
. /opt/bitnami/scritps/setenv.sh
. /path/to/env/activate
cd /path/to/project
/path/to/env/bin/python manage.py some_command