os- ubuntu 14.04
这是我想使用cron作业安排的外壳脚本。
parser.sh
#!/bin/bash
source .profile
workon venv
cd /home/ashish/deployments/myproject
export DJANGO_SETTINGS_MODULE=myproject.settings_production
python /home/ashish/deployments/myproject/myproject/manage.py parse_data
Cron Job-
*/15 * * * * . /home/ashish/parser.sh
每当此Cron作业运行时,我都会收到一个带错误的邮件:
/bin/sh: 6: /home/ashish/parser.sh: source: not found
/bin/sh: 7: /home/ashish/parser.sh: workon: not found
Traceback (most recent call last):
File "/home/ashish/deployments/myproject/myproject/manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
当我从终端运行此脚本时 -
. parser.sh #this gives no error
sh parser.sh #this gives errors
/bin/sh: 6: /home/ashish/parser.sh: source: not found
/bin/sh: 7: /home/ashish/parser.sh: workon: not found
此脚本中可能有什么错误?
您需要使用:
运行它bash parser.sh
不是sh parser.sh
。
如果您的脚本是为bash
编写的,则不应尝试使用sh
执行它。脚本的每个语法可能不符合sh
Bourne Shell的标准。
将Cron Job命令更改为:
*/15 * * * * bash /home/ashish/parser.sh
专门指定bash
。或当您的操作系统是Ubuntu 14.04时,您通常不需要指定bash,即
*/15 * * * * /home/ashish/parser.sh
应该正常工作。