因为从命令行调用它时我的脚本运行良好。我试图按计划使用cron运行此代码:
with open('out.txt', 'a') as f:
f.write('Hello world! \n')
我已设置chmod a+x hello_world.py
但是我想用Nitthon.io在python3.3中运行它,因为which python
和which python3.3
分别返回/home/action/.parts/bin/python
和/usr/bin/python3.3
。我试图在剧本开头添加一些shebang。
#!/usr/bin/python
#!/usr/bin/python3.3
#!/usr/bin/env python
#!/usr/bin/env python3.3
#!/home/action/.parts/bin/python (Weird, I know...)
命令python
返回一个2.7.6 python shell,python3.3
或/usr/bin/python3.3
返回一个3.3.5 python shell。并ls /usr/bin/python*
输出:
/usr/bin/python /usr/bin/python2.6-config /usr/bin/python3.2-config /usr/bin/python3.3m
/usr/bin/python2 /usr/bin/python2.7 /usr/bin/python3.2mu /usr/bin/python3.3m-config
/usr/bin/python2.5 /usr/bin/python2.7-config /usr/bin/python3.2mu-config /usr/bin/python-config
/usr/bin/python2.5-config /usr/bin/python2-config /usr/bin/python3.3
/usr/bin/python2.6 /usr/bin/python3.2 /usr/bin/python3.3-config
我还为PATH和PYTHONPATH添加了python路径:
#PATH=/usr/bin/python3.3:/home/action/.parts/bin:/home/action/.parts/sbin:/home/action/.parts/autoparts/bin:/home/action/.parts/autoparts/bin:/home/action/.parts/autoparts/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/action/.gem/ru
by/1.9.1/bin
#PYTHONPATH=/usr/bin/python3.3
pidof cron
正在返回cron的进程ID。
我尝试使用... > /path/to/cron.log 2&>1
重定向输出但没有成功。和衍生品......
我的crontab -e文件如下所示:
PYTHONPATH=/usr/bin/python3.3
* * * * * /usr/bin/python3.3 /home/action/workspace/hello_world.py
但我无法让它发挥作用...... 任何人都可以帮助这个小家伙? :)
答案 0 :(得分:1)
我的猜测 - 你的脚本工作正常。没有输出,没问题。只是不确定输出文件的位置。
对于您的代码,请尝试绝对路径。
with open('/tmp/out.txt', 'a') as f:
f.write('Hello world! \n')
顺便一提 - #!没什么关系的。
使用python解释器为python脚本添加前缀:
/usr/bin/python3.3 <any-file>
python interperter,而不是shell,执行文件并忽略#!线。
答案 1 :(得分:0)
它正在工作!似乎在Nitrous中~/actions/
充当根目录。所以如果你试着这样做:
with open('out.txt', 'a') as f:
f.write('Hello world! \n')
您正在创建一个包含~/actions/
内容的文件,而不是/home/actions/workbench/out.txt
中脚本文件的当前目录,以使其正常工作,因为我不得不重写它为:
with open('workbench/out.txt', 'a') as f:
f.write('Hello world! \n')
crontab -e文件中不需要shebang或PYTHONPATH。 cron命令它与上面相同(在问题中)。