pythonlinuxcronnitrousio

Nitrous.io, Python and Cron not working


Since my script works well when calling it from command line. I'm trying to run this code on schedule with cron:

with open('out.txt', 'a') as f:
    f.write('Hello world! \n')

And I've set chmod a+x hello_world.py

But I want to run it in Nitrous.io with python3.3, since which python and which python3.3 returns /home/action/.parts/bin/python and /usr/bin/python3.3 respectively. I have tried to add some shebangs at the start of the script.

#!/usr/bin/python
#!/usr/bin/python3.3
#!/usr/bin/env python
#!/usr/bin/env python3.3
#!/home/action/.parts/bin/python (Weird, I know...)

The command python returns a 2.7.6 python shell and python3.3 or /usr/bin/python3.3 returns a 3.3.5 python shell. And ls /usr/bin/python* outputs:

/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

I also added python paths to PATH and PYTHONPATH:

#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 is returning the Process ID of cron.

I tried to redirect the output with ... > /path/to/cron.log 2&>1 without success. And derivatives...

My crontab -e file looks like:

PYTHONPATH=/usr/bin/python3.3
* * * * * /usr/bin/python3.3 /home/action/workspace/hello_world.py

But I can't get it to work... Can anyone help this litle guy ? :)


Solution

  • My guess - your script is working fine. No output, no problem. Just not sure where the output file is.

    For your code, try an absolute path.

    with open('/tmp/out.txt', 'a') as f:
        f.write('Hello world! \n')
    

    As an aside - the #! doens't matter.

    When you prefix the python script with the python interpreter:

    /usr/bin/python3.3 <any-file>
    

    the python interperter, not the shell, executes the file and will ignore the #! line.