Ubuntu 16,使用Crontab运行Python脚本

Ubuntu 16,使用Crontab运行Python脚本,python,linux,ubuntu,cron,Python,Linux,Ubuntu,Cron,出于测试目的,我有一个非常简单的python脚本,它创建了一个以当前日期时间命名的文本文件,这样我就可以知道它是何时运行的: #!/usr/bin/python from time import gmtime, strftime try: filename = strftime("%Y-%m-%d %H:%M:%S", gmtime()) f = open(filename+'.txt', 'w') f.write('HelloWorld') f.close

出于测试目的,我有一个非常简单的python脚本,它创建了一个以当前日期时间命名的文本文件,这样我就可以知道它是何时运行的:

#!/usr/bin/python

from time import gmtime, strftime

try:

    filename = strftime("%Y-%m-%d %H:%M:%S", gmtime())
    f = open(filename+'.txt', 'w')
    f.write('HelloWorld')
    f.close()
except StopIteration:
    print "An error has occurred.."
我已将文件放置在此处,并且它具有完全权限:

usr/share/pyshared/scripts/test_script.py
当我使用以下命令手动运行脚本时,脚本将运行并创建测试文本文件:

python ~/../../usr/share/pyshared/scripts/test_script.py
在我的crontab文件中,我有以下内容没有运行(为了测试的目的,应该每分钟运行一次)

***python~/../usr/share/pyshared/scripts/test\u script.py


第一次使用crontab进行实验,对linux来说也是相当新的,所以如果我错过了一些非常明显的东西,请原谅,提前谢谢。

在crontab中使用绝对路径:

* * * * * /usr/bin/python /usr/share/pyshared/scripts/test_script.py
另外,在脚本中指定绝对路径:

filename = "/home/MYUSER/"+strftime("%Y-%m-%d %H:%M:%S", gmtime())
f = open(filename+'.txt', 'w')

一些解释:cron使用不同的shell并在空环境中执行。如果没有绝对路径,它甚至找不到Python解释器,因为没有定义路径变量。