jixin
Posts: 14
Joined: Thu Jul 02, 2015 2:35 pm

subprocess.pOpen

Fri Nov 13, 2015 9:02 am

I call a child program by using subprocess.Popen("./excutable_file data.csv 1 5000", shell=True) in my script cron.py. It works very well, and a window with video is shown on screen after run cron.py. (the excutable_file is used to show a video on screen)
Now I want to run cron.py automatically when reboot by using "sudo crontab -e" commend, and add line "@reboot python /home/pi/cron.py &" in the file. After setting, cron.py can run automatically after reboot, but the child program is not run. Which means there is no video displayed in a window on screen.
Can anyone help me with this problem. Is that because there is no shell(terminal) shown on screen after reboot? and how to solve this problem.

Thank you very much

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: subprocess.pOpen

Fri Nov 13, 2015 9:29 am

I suspect the problem is that when your code is run by the root users crontab entry the working directory is not /home/pi. Which makes sense when you think about it, as root should start in its own home directory (/root).

change:

Code: Select all

subprocess.Popen("./excutable_file data.csv 1 5000", shell=True)
to:

Code: Select all

subprocess.Popen("/home/pi/excutable_file /home/pi/data.csv 1 5000", shell=True)

jixin
Posts: 14
Joined: Thu Jul 02, 2015 2:35 pm

Re: subprocess.pOpen

Fri Nov 13, 2015 2:05 pm

AndyD wrote:I suspect the problem is that when your code is run by the root users crontab entry the working directory is not /home/pi. Which makes sense when you think about it, as root should start in its own home directory (/root).

change:

Code: Select all

subprocess.Popen("./excutable_file data.csv 1 5000", shell=True)
to:

Code: Select all

subprocess.Popen("/home/pi/excutable_file /home/pi/data.csv 1 5000", shell=True)

Thank you for your reply. I tried, but still not working.

JimmyN
Posts: 1109
Joined: Wed Mar 18, 2015 7:05 pm
Location: Virginia, USA

Re: subprocess.pOpen

Fri Nov 13, 2015 4:02 pm

It could be because X is not up and running yet when crontab executes the script, so no video.

Instead of crontab try adding it to "/home/pi/.config/lxsession/LXDE-pi/autostart", so the script is run when the desktop comes up.

jixin
Posts: 14
Joined: Thu Jul 02, 2015 2:35 pm

Re: subprocess.pOpen

Fri Nov 13, 2015 5:27 pm

JimmyN wrote:It could be because X is not up and running yet when crontab executes the script, so no video.

Instead of crontab try adding it to "/home/pi/.config/lxsession/LXDE-pi/autostart", so the script is run when the desktop comes up.
I tried again, add "@sudo python /home/pi/cron.py" in /home/pi/.config/lxsession/LXDE-pi/autostart. it works the same as crontab.
In my cron.py, I have a button to control the excutable_file to run, which is, after reboot, script cron.py start run, then I need to press the button to start run excutable_file. but now the excutable is not run, no video window show on screen.

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: subprocess.pOpen

Fri Nov 13, 2015 5:42 pm

Maybe try to direct the script output to a file so any errors are recorded?

Something along:

"@sudo python /home/pi/cron.py >/home/pi/cron_py.out"

subprocess.Popen("/home/pi/excutable_file /home/pi/data.csv 1 5000 >/home/pi/excutable_file.out", shell=True)
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

gordon77
Posts: 5036
Joined: Sun Aug 05, 2012 3:12 pm

Re: subprocess.pOpen

Fri Nov 13, 2015 5:55 pm

jixin wrote:
JimmyN wrote:It could be because X is not up and running yet when crontab executes the script, so no video.

Instead of crontab try adding it to "/home/pi/.config/lxsession/LXDE-pi/autostart", so the script is run when the desktop comes up.
I tried again, add "@sudo python /home/pi/cron.py" in /home/pi/.config/lxsession/LXDE-pi/autostart. it works the same as crontab.
In my cron.py, I have a button to control the excutable_file to run, which is, after reboot, script cron.py start run, then I need to press the button to start run excutable_file. but now the excutable is not run, no video window show on screen.
What's in your Cron.py?

Here's a py program l did which will run at boot in the same way, it uses pygame to give a window...

viewtopic.php?f=67&t=124395&p=835753#p835753

jixin
Posts: 14
Joined: Thu Jul 02, 2015 2:35 pm

Re: subprocess.pOpen

Sun Nov 22, 2015 8:43 pm

Thank you for all your help, my problem is not fixed yet.
When I open a terminal, and input the command line "sudo python cron.py", it video window show up when button is pressed.
While if I set a reboot and want it start automatically, after press the button, LED is on, which means the script is running. but there is no video show up. If I press button again, the Raspberry Pi is shut down.
Here is my cron.py script. Please help me figure out what the problem is. Thanks

Code: Select all

#!/usr/bin/python
import os
import subprocess


# Import required libraries
import RPi.GPIO as GPIO
import time

# Tell GPIO library to use GPIO references
GPIO.setmode(GPIO.BCM)

# List of LED GPIO numbers
LedSeq = [4,17,22,10,9,11]

print "Setup GPIO pins as inputs and outputs"

# Set Switch GPIO as input
#GPIO.setup(7 , GPIO.IN)
GPIO.setup(7 , GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

# Set up the GPIO pins as outputs and set False
print "Setup LED pins as outputs"
for x in range(6):
    GPIO.setup(LedSeq[x], GPIO.OUT)
    GPIO.output(LedSeq[x], False)



# set up variables
ButtonCounter = 0

print "Press the button"

# Wrap main content in a try block so we can
# catch the user pressing CTRL-C and run the
# GPIO cleanup function. This will also prevent
# the user seeing lots of unnecessary error
# messages.
try:

  # Loop until users quits with CTRL-C
  while True :
       
    if GPIO.input(7)==1:
      ButtonCounter += 1
      print "  Button pressed!"
      time.sleep(0.5)
      print "Press the button (CTRL-C to exit)"
    # light green LED
      GPIO.output(LedSeq[4], True) 
      GPIO.output(LedSeq[5], True) 
      print "before FaceReco"
    # face recognition program start to run
      subprocess.Popen("/home/pi/rpispy_vcu/camcv2 /home/pi/rpispy_vcu/faces.csv  1 5000", shell=True)
      print "after FaceReco"
    # press button twice, turn off program 
      if ButtonCounter>2:
        print "turn off  green LED and stop face recognition"
        GPIO.output(LedSeq[4], False) 
        GPIO.output(LedSeq[5], False)
        time.sleep(1.0)
        os.system("sudo shutdown -h now")
        #raise KeyboardInterrupt
        

except KeyboardInterrupt:
  # Reset GPIO settings
  GPIO.cleanup()




-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: subprocess.pOpen

Mon Nov 23, 2015 2:09 pm

So it looks like the camcv2 program does not like running from startup. Does it need the graphical desktop? Has the desktop started up by the time your script executes? Dpes camcv2 need a logged in user and is there a logged in user at that point?

Would it be worth trying to redirect the output of camcv2 to a file to see if any errors output:

Code: Select all

subprocess.Popen("/home/pi/rpispy_vcu/camcv2 /home/pi/rpispy_vcu/faces.csv  1 5000 >/home/pi/camcv2.log 2>&1", shell=True)
?
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

jixin
Posts: 14
Joined: Thu Jul 02, 2015 2:35 pm

Re: subprocess.pOpen

Tue Nov 24, 2015 7:07 pm

I tried and found camcv2 was running and there was no error report, because all the print notes I write in camcv2 is logged in the output file, but still there is no video window show up, and the Pi camera light is not on, which means Pi camera is not working.
To me, I think camcv2 is running after reboot, while the camera is not enabled. While I run cron.py in terminal, camcv2 working perfectly and the video window is shown.

Is there any thing wrong with the commend line "shell = True" ?
Any suggestions? Really appreciate your help.

-rst- wrote:So it looks like the camcv2 program does not like running from startup. Does it need the graphical desktop? Has the desktop started up by the time your script executes? Dpes camcv2 need a logged in user and is there a logged in user at that point?

Would it be worth trying to redirect the output of camcv2 to a file to see if any errors output:

Code: Select all

subprocess.Popen("/home/pi/rpispy_vcu/camcv2 /home/pi/rpispy_vcu/faces.csv  1 5000 >/home/pi/camcv2.log 2>&1", shell=True)
?

sprinkmeier
Posts: 410
Joined: Mon Feb 04, 2013 10:48 am
Contact: Website

Re: subprocess.pOpen

Tue Nov 24, 2015 8:07 pm

You can run multiple X servers concurrently. This is how multiple simultaneous user sessions are handled.

Programs use an environment variable to know which X server to talk to:

Code: Select all

$ echo $DISPLAY
:1
Usually the display is ":0", but I'm the second user on this box, so I got ":1".
In an ssh session on another box I get ":11", part of the X-forwarding over SSH magic that allows GUI redirection.

The script runs without this environment variable set, so even if the X server is already running it won't know which one to talk to.
Try something like

Code: Select all

sleep 90 ; env DISPLAY=:0 /your/script/here
(90 seconds is ludicrously long, tune that to suit)

If that fails try running

Code: Select all

xhost +
in a terminal before the delay ends. This disables X authentication (usually a Very Bad Idea!) but might help if the script is running as a different user than the X server, or $HOME isn't set up.

jixin
Posts: 14
Joined: Thu Jul 02, 2015 2:35 pm

Re: subprocess.pOpen

Tue Nov 24, 2015 9:43 pm

I tried again, there is error showed this time as below:
===
OpenCV Error: Image step is wrong (The matrix is not continuous, thus its number of rows can not be changed) in reshape, file /home/pi/OpenCV-2.3.1/modules/core/src/matrix.cpp, line 750
terminate called after throwing an instance of 'cv::Exception'
what(): /home/pi/OpenCV-2.3.1/modules/core/src/matrix.cpp:750: error: (-13) The matrix is not continuous, thus its number of rows can not be changed in function reshape
===
By the way, it still work when run in terminal with video window show up. just show error in the output file when reboot and run automatically.

-rst- wrote:So it looks like the camcv2 program does not like running from startup. Does it need the graphical desktop? Has the desktop started up by the time your script executes? Dpes camcv2 need a logged in user and is there a logged in user at that point?

Would it be worth trying to redirect the output of camcv2 to a file to see if any errors output:

Code: Select all

subprocess.Popen("/home/pi/rpispy_vcu/camcv2 /home/pi/rpispy_vcu/faces.csv  1 5000 >/home/pi/camcv2.log 2>&1", shell=True)
?

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: subprocess.pOpen

Wed Nov 25, 2015 2:53 pm

Hmm, would be nice to be familiar with these...

I don't think you answered this: Does it need the graphical desktop?

If so, maybe look at this http://www.raspberrypi-spy.co.uk/2014/0 ... e-desktop/ instead of cron?
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

jixin
Posts: 14
Joined: Thu Jul 02, 2015 2:35 pm

Re: subprocess.pOpen

Wed Nov 25, 2015 5:03 pm

Thank you very much for your help, and sorry for not fully understanding your instructions in the beginning.
I follow your instruction below in the link and put every needed file at /home/pi, instead of any subfolder. then the Pi camera works.


-rst- wrote:Hmm, would be nice to be familiar with these...

I don't think you answered this: Does it need the graphical desktop?

If so, maybe look at this http://www.raspberrypi-spy.co.uk/2014/0 ... e-desktop/ instead of cron?

Return to “Python”