0

I want the camera on my pi (Rpi 3B+) to take a picture every time it boots up. I've read through several other questions of people trying to do similar things, but I haven't ever found the script that was used to successfully achieve it. This is the first script I've written, so I'm guessing that the problem is with the script, rather than the init.d setup as that part is pretty easy to follow

Here's what I have:

# /etc/init.d/startup.sh
### BEGIN INIT INFO
# Provides:          startup.sh
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO

raspistill --annotate 12 -o /home/pi/Pictures/Startup/Startup_`date +%d%m%Y_%H%M-%S`.jpg
  1. I saved this to /etc/init.d/startup.sh
  2. then used sudo chmod +x startup.sh to make it executable
  3. Then sudo update-rc.d startup.sh defaults as I understand it, to register the script to run at startup.

(These instructions were pulled straight from the page I linked above. I don't at all have a thorough understanding of what they actually do. I'm just following the best directions I can find.)

I have tested the raspistill command that I used in the script, and I know that it does work, and saves a timestamped image to the designated folder. I can even run the script from the init.d folder (by double-clicking on it in the GUI) and it works from that location when I run it. One last thing to note here, is that it does ask me what I'd like to do when I double click it, giving the options: Execute, Execute in Terminal, Open, and Cancel. I don't know if that's relevant to my issue, but I want to get everything out there.

Can someone please help me figure out what I'm doing wrong? TIA!

3
  • The INIT info section at the beginning of my script, was also pulled directly from the linked page (and then I replaced my script name and location with the one in the example.)
    – Ronthebear
    Commented Nov 13, 2018 at 20:50
  • What operating system are you using? Is it Raspbian Stretch?
    – Ingo
    Commented Nov 14, 2018 at 19:12
  • Yes, Raspberrian Stretch installed via NOOBS method.
    – Ronthebear
    Commented Nov 16, 2018 at 13:20

2 Answers 2

1

You should have better used method 4: systemd from the linked page. systemd is the default init system and all others is only emulated by it with known limitations: Compatibility with SysV. Because your program runs from the command line I would use a systemd unit. Before doing it you should revert things you have done with init.d. Then create a new service with:

rpi ~$ sudo systemctl edit --force --full raspistill.service

In the empty editor insert these statements. You have to replace /absolute/path/to/ with the real one. Save this and quit the editor.

[Unit]
Description=Raspistill service
After=multi-user.target

[Service]
Type=oneshot
RemainAfterExit=yes
User=pi
WorkingDirectory=~
ExecStart=/absolute/path/to/raspistill --annotate 12 -o /home/pi/Pictures/Startup/Startup_`/bin/date +%d%m%Y_%H%M-%S`.jpg

[Install]
WantedBy=multi-user.target

Then enable the new service and reboot:

rpi ~$ sudo systemctl enable raspistill.service
rpi ~$ sudo systemctl reboot

Check with:

rpi ~$ systemctl status raspistill.service
rpi ~$ journalctl -b -e
3
  • Awesome, thanks for the help. I’ve been busy and haven’t gotten back to this to try it yet, but I wanted to say thanks!
    – Ronthebear
    Commented Nov 29, 2018 at 20:59
  • @Ronthebear I'm not sure if I met all edge conditions because lack of information. Tell me if it does not work. There are many options for the environment to make it run (man systemd.exec).
    – Ingo
    Commented Nov 30, 2018 at 0:05
  • I've been trying to run a time lapse using picam on startup. The systemd method from the linked webpage is the only method that works, out of four that I tried. Thanks!
    – smcs
    Commented Jul 15, 2021 at 10:18
0

so I'm guessing that the problem is with the script

I am no so sure it is your script that is the main issue.

Both init.d and systemd are responsible for loading and running services on a Linux derivative like raspbian. Systemd replaced init.d as the method to start hardware and software services, so Ingo (see below) is correct that systemd is to be preferred.

A issue I have encountered with starting services on a RPI is that it boots much faster than a conventional server, so quite often it has run through the startup services sequence before the hardware has fully initialised.
So it may be your script is running, but the camera is not ready. I have solved this in my use case by putting the command in a script and including a sleep to allow the hardware to catch up.

Save this file as: /usr/local/bin/startupstill

!/bin/bash
sleep 30
/absolute/path/to/raspistill --annotate 12 -o /home/pi/Pictures/Startup/Startup_`/bin/date +%d%m%Y_%H%M-%S`.jpg

Run the following commands, to ensure the file ownership is correct and it is executable:

chown pi:pi /usr/local/bin/startupstill

chmod u+x /usr/local/bin/startupstill

Then in Ingo's example systemd service script use Type=forking in the [Service] section, then change the following lines:


WorkingDirectory=/home/pi/Pictures/Startup
ExecStart=/usr/local/bin/startupstill

HTH

F

2
  • 1
    Please never send a service to the background by job control with & within a systemd service. This way systemd looses any control about the daemon. Programs started with systemd are already made a service. If you want the behavior of traditional UNIX daemons you must use Type=forking in the [Service] section. You should correct your answer.
    – Ingo
    Commented Mar 8, 2019 at 16:39
  • Happy to learn a better approach. The purpose of the approach was to stop the whole boot process hanging while the sleep ran down and defeating the purpose of the sleep of allowing the rest of the system to come up. Revised my solution with your proposal.
    – Falstone
    Commented Mar 11, 2019 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.