0

I have a Python program which is working properly when I run it normally from LXTerminal:

$ sudo python testcon.py

but when I run it with cron to start after reboot:

@reboot python /home/pi/testcon.py &

it stops at the line:

f = open('info.txt')

and doesn't do anything more. It's supposed to open the file /home/pi/info.txt.

Why does this happen? How can I fix this?

Here's a simpler version of my program that shows the problem:

import smbus
import time

bus = smbus.SMBus(1) # Rev 2 Pi uses 1

DEVICE = 0x23 # Device address (A0-A2)
IODIRA = 0x00 # Pin direction register
OLATA  = 0x14 # Register for outputs
GPIOA  = 0x12 # Register for inputs

bus.write_byte_data(DEVICE,IODIRA,0x00)

bus.write_byte_data(DEVICE,OLATA,0xFF)  #set all of the outputs
time.sleep(3)                           #wait for 3 sec

f = open('info.txt')                    #should open the txt file
bus.write_byte_data(DEVICE,OLATA,0)     #clear all of the outputs
f.close()
Dan Getz
  • 8,774
  • 6
  • 30
  • 64
Mora
  • 1
  • My guess is that python is not in the path of the cron process. Can you track down the error messages in the log? – alexis May 02 '15 at 18:04
  • Not exactly understand what you mean, but when reboot the raspberry i can start the program (use button), but when the program reach this part f=open('info.txt') the program stop. If i start with sudo python... there is no problem. Should i somehow write in the cron to get it able to open txt file? – Mora May 02 '15 at 18:09
  • (a) raspberry, you say? (b) If adding sudo fixes it, it's clearly a permission problem. If your regular account has access to the file, it seems that cron runs as its own user, is this the case? – alexis May 02 '15 at 19:32
  • sorry, i mean "sudo added" when i start the program in LX terminal, only this case the python program can run properly and open txt file. if i set to start by cron after reboot, the python program stops at the line when is should open the txt file. so what should i do, maybe allow to cron to open text file, or i don't know? – Mora May 02 '15 at 20:23
  • I think you need to put some work into explaining your situation clearly. I still have no confidence that I understand what you're describing. There's no way anyone could reproduce your problem, even if they have exactly your computer and operating system. Focus on that and I'm sure you'll be sorted out. – alexis May 02 '15 at 21:17
  • Ok, i try to explain better. I have a phyton program on raspberry pi 2. This program a too long to copy here, but it do led blinking via i2c port expander. The program use a txt file to get some information and open with this f=open('info.txt'). When i start the program running in LXterminal (write in: sudo phyton test.py)there is no problem, but when i want that to start automatically after reboot (i use cron: @reboot python /home/pi/test.py & ) the led blinking (py program) starts well, but when the program reach the txt open part, it stops. The txt contain only this: 00600 – Mora May 03 '15 at 07:54
  • Mora, you're right not to be posting your whole program-- that wouldn't be useful either. But if a program containing **only** the line you mention demonstrates the problem, you can **edit your question** to make that clearer. (I edited the question for you a bit.) – alexis May 03 '15 at 09:45
  • In view of @William's answer: If the problem is persisting, mention which crontab you put this command in. – alexis May 03 '15 at 09:54
  • It's also possible that the file is being read, but your program lacks permissions to blink the leds? – alexis May 03 '15 at 09:56
  • I made a shorter program:import smbus import time bus = smbus.SMBus(1) # Rev 2 Pi uses 1 DEVICE = 0x23 # Device address (A0-A2) IODIRA = 0x00 # Pin direction register OLATA = 0x14 # Register for outputs GPIOA = 0x12 # Register for inputs bus.write_byte_data(DEVICE,IODIRA,0x00) bus.write_byte_data(DEVICE,OLATA,0xFF) time.sleep(3) f = open('info.txt') bus.write_byte_data(DEVICE,OLATA,0) f.close() – Mora May 03 '15 at 11:38
  • **Put it in your question** and format it properly. – alexis May 03 '15 at 11:52
  • Mora, thanks for the clarification. But please, please learn to use this site properly. **Edit your question** and put the new information there. Answers are not the right place to expand on your question. If you don't update your question, it will not be useful to future users of the site. – alexis May 03 '15 at 13:04

2 Answers2

0

The @reboot option can only be used in root. It cannot be invoked in your user's cron. However, sometimes it depends upon your operating system. See here.

Community
  • 1
  • 1
William Lee
  • 93
  • 1
  • 10
0

Looking at this rather similar question, and especially at this answer, I have two suggestions:

a) Does the raspberry support the @reboot syntax at all? Check by reading the manual page for cron and crontab.

b) From the linked answer it seems that you need to add the name of the user that should be executing the script:

@reboot username /usr/bin/python /home/pi/test.py &

If the program can only succeed when running as root, use root as the username; but otherwise it's always a good idea to avoid running things as root.

Community
  • 1
  • 1
alexis
  • 48,685
  • 16
  • 101
  • 161