0

I have a simple Python script on EC2 to write a text to a file:

with open("/home/ec2-user/python/test.txt", "a") as f:
    f.write("test\n\n")

That python script is here:

/home/ec2-user/python/write_file.py

When I run that script manually ('python /home/ec2-user/python/write_file.py') - new text is being written to a file ('/home/ec2-user/python/test.txt').

When schedule same script using Cronjob - no data is being added to a file. My Cronjob looks like this:

* * * * * python /home/ec2-user/python/write_file.py

I verify Cronjob is running and suspecting some ENV parameters are not the same during Cronjob execution (or something else is happening). What could be the case and how to fix it in a simple way?

Thanks.

Joe
  • 11,983
  • 31
  • 109
  • 183

3 Answers3

1

I recently began using Amazon's linux distro on ec2 instances and after trying all kinds of things for cron all I needed was:
sudo service crond start
crontab -e
This allowed me to set a cron job as "ec2-user" without specifying the user or env variables or anything. For example, this single line worked:
0 12 * * * python3 /home/ec2-user/example.py
I also posted this here.

Community
  • 1
  • 1
j3py
  • 1,119
  • 10
  • 22
1

On crontab

HOME=/home/ec2-user/
* * * * * python /home/ec2-user/python/write_file.py

alternatively (testing purpose)

* * * * * root /home/ec2-user/python/write_file.py

On your write_file.py add shebang (first line)

#!/usr/bin/env python

BONUS: (feel free to ignore) my first response debug method for cron on minute tasks:

import os
while True:
    print os.popen('pgrep -lf python').read()

executed from shell will pop out all python modules called by cron. After 60 secs you know whats going on.

Roy Holzem
  • 860
  • 13
  • 25
0

it's bette to specify the absolute path of the python interpreter because the command may not be recognized by the system. Or you can add the shebang line to your script #!pathToPythonInterpreter at the first line of your script

make sure that the user who planning the execution of script have the right to read and write to the file

make sure that you are not using the system corntab /etc/crontab to planify because then you have to specify the user who will execute the command.

I hope that this will help you

Mohamed Amine Ouali
  • 575
  • 1
  • 8
  • 23