-1

I am trying to run a simple python script on a VM running Ubuntu 18 hosted on my Synology. The python script simply prints date and time in a file so I can check if it ran or not. It looks like this:

from datetime import datetime
with open('file.txt','a') as file:
    file.write('Recorded at: %s\n' % datetime.now())

I made a cronjob that looks like this:

* * * * * /home/anaconda3/bin/python3.7 /home/Documents/crontest.py

I have tried many variations. For instance not writing 3.7 and just simply write 'python'. I tried the default python path /usr/bin/python3.7.

Furthermore I tried to ad the shebang #!/home/anaconda/bin/python3.7 to the script and leaving out the path in the cronjob.

It feels like I am missing something elementary here. I tried many options as posted on Stack and other forums, but none of the options seem to fix my problem.

edgar piet
  • 67
  • 3
  • 11
  • 2
    1) The indetion of your Python code is broken. 2) It is `#!`, not `!#`. 3) Where is Python *really* installed? What does `which python3` tell you? –  Feb 12 '19 at 11:23
  • 1
    Possible duplicate of [CronJob not running](https://stackoverflow.com/questions/22743548/cronjob-not-running) – tripleee Feb 12 '19 at 11:24
  • Could also try commands: _py_, _py3_, _python3_ etc. – Lordfirespeed Feb 12 '19 at 11:25
  • @LutzHorn 1,2) My bad, poor copying skills. fixed it in the question. 3) /home/anaconda3/bin/python3. I changed the code in the cronjob accordingly, but it don't make a difference. – edgar piet Feb 12 '19 at 11:35
  • It may be permission issue what happens if you run it manually? or if you change cron to ```/home/anaconda3/bin/python3.7 /home/Documents/crontest.py 2>&1 | tee crontest.log``` – Ali Hallaji Feb 12 '19 at 11:38
  • Ok, I fixed it thanks to @LutzHorn (I used the wrong path) combined with a solution in the potential duplicate where I found that you can not use relative path within python scripts when running cronjobs. – edgar piet Feb 12 '19 at 11:41
  • Running manually worked just fine. – edgar piet Feb 12 '19 at 11:44

1 Answers1

1

Using relative links in python scripts is not allowed when running cronjobs. So it works when I write it like this:

from datetime import datetime
with open('/home/Documents/file.txt','a') as file:
    file.write('Recorded at: %s\n' % datetime.now())

Furthermore, I used the wrong path to python. I wrote /python3.6 instead of /python3 which I found through typing

which python3

in terminal.

edgar piet
  • 67
  • 3
  • 11