1

I am trying to get a simple Python script running on startup with Cron on Ubuntu 16.04. What I did is opened cron with:

sudo crontab -e

and added the last line as shown in the screenshot: enter image description here

I even tried

@reboot without sudo later.

Then I do:

sudo reboot

But when Ubuntu starts the Python file seems to have not run. I also modified crontab by opening it as a simple user:

crontab -e

And added:

@reboot sudo python /my_script_path

Nothing worked.

The Python script is supposed to create a text file when executed. I have tested the Python script using the terminal and it successfully generates that text file.

Anyone know what I am missing here?

Edits: The Python script I am trying is a very simple one:

with open("example.txt", 'w') as file:
    file.write("Some text")
print("Done")
  • what is the source of the text .. in other words is it getting it from a service or program ... it may be a timing issue .. it may be trying to get the information before the place its trying to get the information is available. The script runs when cron starts which may be before the information is accessable – John Orion May 9 '16 at 6:18
  • @JohnOrion it's a simple Python script (I edited my question adding the Python code). Would I need to add some delay in crontab somehow? – multigoodverse May 9 '16 at 6:23
  • ok so you aren't grabbing text from anywhere .. everything is static... well that rules out that theory. sorry... no my theory was wrong .. if you arent actually getting the text from somewhere and its just static in your script then the issue i was talking about cant happen. It would have been something like if you were trying to read from a process to grab text from it but the script ran before the process was launched so it didnt have any text to grab .. this doesn't fit for your situation :( – John Orion May 9 '16 at 6:25
  • 1
    @JohnOrion I am now suspecting cron is creating "example.txt" in the cron directory because I am passing a relative path for the text file. I should maybe pass an absolute text file path. I don't have my Ubuntu here at the moment to try that though. – multigoodverse May 9 '16 at 6:30
  • you may be onto something there ... or maybe the root directory. Give me a few moments .. i will try it out with an absolute path and see if it works .. Ill slap it into a VM so I dont have to restart my server :D – John Orion May 9 '16 at 6:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.