1

New here and know that launching programs is a well discussed topic, but I've got a slightly different need that I think is complicating the solution.

I've got a straight forward python script that is run using an input from a USB device (Bar Code Scanner) and I'm using to actuate another device using the GPIO interface. The desired function is for this program to run in the command line (headless) once all resources are available.

I've tried several other solutions like modifying my program to run using Crontab, rc.local, and other well discussed solutions but it seems either my home directory is not yet available or the boot order means that the input device isn't available yet. None of the solutions described earlier resulted in an error message or the desired function.

Continuing to look for an answer independently, ideas or resource material would be appreciated.

PSCampbell
  • 109
  • 3
  • Might help if you knew I was running Raspbian Stretch Lite – PSCampbell Aug 13 '18 at 05:16
  • "None of the solutions described earlier resulted in ... the desired function." -> Note that what running it from .bashrc actually does is run the script when an interactive shell is started (every time). If you are using autologin, that may happen shortly after boot. As others have commented, this is a bad way to go. The correct way to do this would be to either find out what boot service targets you need to use as a dependency, or use a manual loop with a sufficient sleep to wait for them -- in which case the methods you've rejected would be fine. – goldilocks Aug 13 '18 at 16:08

2 Answers2

1

You said you "... tried modifying my program to run using Crontab". Not certain exactly what that means, and you didn't show us what you tried, but you've supplied no reason that it shouldn't work. And so I'll suggest that you try it again, using something like this in your crontab

@reboot /bin/sleep N; /path/to/your/yourPythonscript

Where N is the number of seconds to sleep (10-15 secs is usually sufficient, but depending on your hardware, it may take longer) See man sleep for more details

sleep simply suspends your shell for the time specified while your systems marshals other resources during the boot process. This is necessary as cron regards @reboot to be the time that it's spawned, without regard to the availability of other resources. That said, cron is IMHO, the safest and easiest way to start things after a reboot.

Hope that helps.

ADDENDUM:

If you launch your program from .bashrc it may execute when you don't expect it to. Read man bash for details.

Seamus
  • 21,900
  • 3
  • 33
  • 70
-2

Well, I found my answer and sheepishly leaving it here for future Googlers:

I should have added the program to my .bashrc, which is pretty much the only method that I didn't attempt prior to posting. I found a decent tutorial on the various methods of accomplishing what I was needing here

PSCampbell
  • 109
  • 3
  • Please accept your answer so it is marked finish and will not pop up again in some month. – Ingo Aug 13 '18 at 09:14
  • 2
    A note for others DO NOT DO THIS . This would run EVERY time a script is run.. – Milliways Aug 13 '18 at 13:01
  • 1
    Please read man bash (esp the INVOCATION section), and consider editing your answer. We don't know exactly what you're doing, and this may be exactly right for you; However, as general advice, it is not. Your referenced tutorial even says: "your python program will run on boot and also every time when a new terminal is opened, or when a new SSH connection is made." – Seamus Aug 13 '18 at 15:52