1

I am relatively new to programming and am attempting to run a program from boot using a cron job on a Raspberry pi 2. So I went into the terminal and accessed the cron tab, I then entered @reboot python /home/pi/Project/Bluetooth.py (I know that's the right address, I have checked several times). The Python code I am trying to run in the beginning is:

import os
import subprocess

print "Hello World"
os.system('echo "hello world"')

os.system('/usr/bin./galculator')

target = open('test.txt', 'w')
target.write("Why doesn't this work")
target.close()

I am simply trying to write some type of code to see if the cron is working and I test the code below in the terminal and everything works (prints hello world twice, opens up calculator, and then creates the text file). But it doesn't do anything on boot.

I tried every thing I could think of by printing to opening up processes. I am running Raspbian that i installed my self and everything else is working ok. I can't really start working on this project until I know it will run at boot. Does anyone have any suggestions?

  • 1
    Is that the actual code, or does the real version not have the typos in lines 5, 7 and 9? There's a very good chance that X11 is not up by the time that this script is run, so galculator will have no way to run. – scruss Apr 20 '15 at 1:08
  • Sure about that dubious . in the ` /usr/bin` path? – Ghanima Apr 20 '15 at 1:21
  • Scruss? What errors? It runs in terminal, am i missing some code in there? I copied it over pretty hastily but I am pretty sure that is it. And Ghanima the ./ is to run galculator. Is that improper syntax in python? – Sean Apr 20 '15 at 1:40
  • /usr/bin./galculator means 'Run galculator in a folder called "/usr/bin." - There is no folder called bin. (with the dot at the end). – recantha Apr 20 '15 at 13:00
  • The code above runs in the terminal on PI. It throws some errors but it looks like its for the instance of galculator starting up. Is ./ not run on python? I thought that was a unix thing. It seems like cron isn't running my code right off boot. – Sean Apr 20 '15 at 16:58
1

Hey guys i figured out what my issue was! Running that code wouldnt work so in the cron tab i had to put the line "@reboot cd /home/pi/Project; sudo python Bluetooth.py &". This worked, I don't know why it wasn't working earlier. But the code would work but i did change the galculator command to what you suggested Gilles. Thanks for the help guys!

  • As often repeated: there is no need for the "&" in crontab. – Ghanima Apr 27 '15 at 7:18
  • Oh, does it automatically run everything in the back ground on start up? – Sean Apr 29 '15 at 0:28
  • 1
    Yes, cron (+) forks each scheduled job into a separate process so that it will not be attached to cron but runs "in the back ground". (+) Note that this is the common behaviour there might be cron-implentations around handling that otherwise (but that seems unlikely). – Ghanima Apr 29 '15 at 7:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.