0

So I'm trying to make this python script run on launch. It should remain mostly still, but when the motion sensor is triggered, I want the camera to start recording and the motor to move. Ultimately, this process needs to happen when I'm headless, so having it start automatically on bootup is the easiest way to do this. I've tried using this init.d file to make it run automatically, but I get the following error message:

Starting mylauncher from: can't read /var/mail/gpiozero from: can't read /var/mail/gpiozero from: can't read /var/mail/picamera /home/pi/Detector.py: 4: /home/pi/Detector.py: import: not found from: can't read /var/mail/time /home/pi/Detector.py: 7: /home/pi/Detector.py: Syntax error: "(" unexpected

I googled it, and people suggested putting in #!/usr/bin/python as the very first line, but when I do that, I get

File "/etc/init.d/mylauncher", line 18 case "$1" in ^ SyntaxError: invalid syntax

How can I get rid of both of these errors and make sure the script runs on startup?

Ramon
  • 1,121
  • 10
  • 25
NeonCop
  • 55
  • 1
  • 1
  • 12
  • Welcome to Stack Overflow! Try to do as much debugging as possible on your issue (specially since your testing conditions are difficult to recreate), and provide as much information about your findings as possible, for someone to be able to help you. Make sure you upvote any answer you find useful, and after leaving enough time for everyone to answer (half a day or so), select the answer, if any, that best answers your question. – Ramon Jul 06 '17 at 19:38

2 Answers2

0

Did you add #!/usr/bin/python to the init.d script? They probably meant that you should add it to the python script.

#!/usr/bin/python is telling a shell what to use to run a script, so that in the line

# run application you want to start
/home/pi/Detector.py

it will use python to execute the script.

Ramon
  • 1,121
  • 10
  • 25
  • SO CLOSE! thank you so much for that, now it successfully starts the program once. However, once the video is finished recording the first time, the motor just starts spinning and never stops. Is there anyway to make it run the python script unlimited times? When I run it straight from the python editor, the script has no problem with the motor, this only happens when I use the sudo /etc/init.d/mylauncher start command. Also, when I rebooted the Pi, the python script didn't activate either, which is weird because that little test sort of worked. – NeonCop Jul 06 '17 at 19:01
  • It sounds like you want a daemon process (it stays running on the background all the time). You can either run your script as a [daemon from the sell](https://stackoverflow.com/questions/3430330/best-way-to-make-a-shell-script-daemon), or have the [python script itself create a daemon](http://web.archive.org/web/20131017130434/http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/) and exit. Also, the usual way to enter a python script is by `if __name__ == "__main__":`. – Ramon Jul 06 '17 at 19:29
  • I'm not sure why the motor keeps moving only when you run it from the shell. Although from taking a quick look at your python code, it looks like you do all the motor operations before you actually start recording anything? In any case, your "daemon" would probably have an infinite loop of some sort (and ideally a way to exit cleanly if you kill your daemon - hint: you have to handle `SIGTERM` signal). – Ramon Jul 06 '17 at 19:29
  • I'd recommend implementing the daemon, and testing it from a regular shell before you start it at bootup from `init.d`. Make sure you can start it, that it works, that you can stop it, etc. – Ramon Jul 06 '17 at 19:31
  • wait, shouldn't it start at bootup anyway because it is in the init.d folder? And wow, thanks for those links, I've only been doing this for a week so I'll do my best to try to implement that. – NeonCop Jul 06 '17 at 19:43
  • Yes, I meant that you should go back and test your daemon functionality before putting it into `init.d` (i.e. remove it from `init.d` for now). I say that because it may be difficult for you to debug a script that is run at bootup. Using the [second link](http://web.archive.org/web/20131017130434/http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/) should be fairly easy, you basically just have to put your code under `def run(self):` – Ramon Jul 06 '17 at 20:01
  • While I'd love to continue helping you with this, it goes beyond the scope of the question and the QnA nature of SO. You can always ask more questions regarding daemons later - although that is no replacement for your own debugging and googling!! Cheers :) – Ramon Jul 06 '17 at 20:02
0

Ok, I figured it out. Turns out my Python code was missing a while True: before starting the main body of code, which was making it run once instead of multiple times. In addition to that, I had deleted some critical components from the init.d shell file that appeared to be comments but were really essential information about when to start and stop it. Also, I had to put #!/usr/bin/python before my Python script so the init.d file would know it was reading it in python. Lastly, I used python /home/pi/Detector.pyinstead of just /home/pi/Detector.py, because it wasn't working with the latter even though I had the #!/usr/bin/python in the python script. In the end, it was not necessary to daemonize either the shell or the python script.

NeonCop
  • 55
  • 1
  • 1
  • 12