How do you automatically run Pi commands at startup?

I am using a bed-mounted Logitech c920 and can log into my OctoPi via Putty and use the following commands to get the webcam into sharp focus:

sudo v4l2-ctl --set-ctrl=focus_auto=0
sudo v4l2-ctl --set-ctrl=focus_absolute=20

The problem is that when I reboot the OctoPi the settings are lost. My question is how do I get the Pi to run these commands automatically when booting? I've tried using Crontab but the OctoPi doesn't seem to support it:

crontab -e
@reboot sleep 10 && sudo v4l2-ctl --set-ctrl=focus_auto=0
@reboot sleep 10 && sudo v4l2-ctl --set-ctrl=focus_absolute=20

Thanks!

use the normal startup scripts...

Exactly! Where are they? The only startup files I remember configuring for the OctoPi were octopi-network.txt for the network settings and octopi.txt to enable MJPG streamer for the webcam but I don't think you can use bash commands in those scripts as they are OctoPi-specific.

I am finally able to run startup commands by ssh'ing into the OctoPi and adding them to mjpg-streamer/start.sh

cd\
nano mjpg-streamer/start.sh
sudo v4l2-ctl --set-ctrl=focus_auto=0
sudo v4l2-ctl --set-ctrl=focus_absolute=40

2018-09-28_013944

1 Like

Just created an account just to thank you,
as I had the exact same problem :slight_smile:
cheers!

hmm, whats changed? i had this all setup, but i changed to Raspberry pi 4 and did a clean install, every time i add any combination of these commands to either my mpeg streamer start.sh or rc.local the cam just straight up stops working. i can how ever Manually type these commands in putty/ssh and no problems, its been a while... any hints guys? very frustrating

Keep in mind the security context when things happen:

  1. Run a command from ssh and you're the pi user
  2. Run a command in rc.local, for example and you're the root user

What path does the pi user have and what path does the root user have? If you don't know then it's best practice to give full paths to everything you're trying to run.