SOLVED Trigger script when Raspberry Pi3 reboots

I have been playing around with FFMpeg and figured out how to take a snapshot of my cameras and save it to a file. I have also created a .sh script on the Pi.
Whats the best way to trigger this script on reboot?

My cctv.sh script

  GNU nano 2.7.4                                                                          File: cctv.sh

#!/bin/bash
#ffmpeg cctv script to get snapshot from cctc cameras

ffmpeg -y -rtsp_transport tcp -i rtsp://admin:sinola10@192.168.1.80:554/Streaming/Channels/302 -an -vf fps=1/10 -s 800x450 -update 1 /etc/openhab2/html/img.jpg &

I have tried to edit the rc.local file as suggested in a tutorial I read with -:

#!/bin/sh -e
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.

FLAG="/opt/afterfirstboot.lock"
FIRSTBOOT="/boot/first-boot.sh"

# Only executed after first boot
if [ ! -f "$FLAG" ]; then
  echo "[openHABian] Flag file 'afterfirstboot.lock' not found, continuing with '$FIRSTBOOT' script."
  if [ ! -f "$FIRSTBOOT" ]; then "[openHABian] 'first-boot.sh' not found, Exiting."; exit 0; fi
  if (/bin/bash "$FIRSTBOOT"); then
    echo "[openHABian] Finishing up... "
    echo "[openHABian] Created after first boot script succeeded (see /etc/rc.local). Do not delete." > "$FLAG"
  else
    echo "[openHABian] Script '$FIRSTBOOT' failed."
  fi
fi

sudo bash /home/cctv.sh &

exit 0

# vim: filetype=sh

I also think I have set permissions correctly… but go easy on me I have a serious Linux learning curve.

[20:06:46] root@openHABianPi:/etc# cd ~
[20:07:00] root@openHABianPi:~# ls -l
total 8
-rwxr-xr-x  1 root root  229 Jul 11 19:23 cctv.sh
drwxr-xr-x 19 root root 4096 Jul  7 18:49 ffmpeg
[20:07:05] root@openHABianPi:~#

If I execute the script manually it works but when I reboot the Pi it does not work, could some guru point me in the right direction please? What have I missed or not learnt correctly?

To bring this on-topic for this forum, could you use an OpenHAB rule that runs after it starts back up?
The newbie here is curious.

1 Like

I was wondering about that as well, but have not got a clue how to do it! :thinking:

why not use crontab?

just do

sudo crontab -e

(or without sudo, depends for which user)
and then

@reboot /path/to/yourscript.sh

or if you want some delay after reboot

@reboot perl -le "sleep 120" && /path/to/yourscript.sh

this will wait 2minutes

if you really want to handle that in OH
then use something like this as a rule:

rule "OH Start"
when
    System started
then
    val mydata =  executeCommandLine("/path/to/yourscript.sh")
    logInfo("reboot script executed",mydata)
end
2 Likes

Technically speaking, the “system started” event means a re-read of the rules file. This is also true when OH restarts of course. But it will also run when you touch the rules file.

1 Like

You don’t say it but it appears that you are running openHABian. In that case it (nor many other Linux distros any more) use Init.d anymore. You need to configure the script to start up using systemd.

Here is an example of a .service file I use to start sensorReporter

[Unit]
Description=Reports status and sensor readings over MQTT and REST
Requires=networking.service
Wants=network-online.target
After=network.target network-online.target

[Service]
WorkingDirectory=/opt/sensorReporter
User=root
Type=simple
ExecStart=/opt/sensorReporter/sensorReporter.py /opt/sensorReporter/sensorReporter.ini

[Install]
WantedBy=multi-user.target

I know I know, I shouldn’t be running a service as root like this. I should change that.

See the systemd link above for an explanation of each field.

I wouldn’t use openHAB for this. The effort involved and all the many ways it could break really makes using the proper approach (i.e. using the OS mechanisms) the only viable options IMHO.

Systemd is actually really easy to work with to start services like this at boot. You just need to create a file, put it in the right folder, and call sudo systemd enable MyService.service and you are good to go.

crontab could work as well. Though I would still recommend using the proper tool for the proper job. In this case, if I understand correctly, the script runs forever, not just a quick run and stop. Cron is not intended to run stuff that never exit.

This is also part of the problem with running this from openHAB. The script never exits so if you use executeCommandLine then that Rule will never exit, tying up one of the five Rules treads that are available forever. There is a similar problem using the Exec binding, though the overall impact to openHAB is somewhat less.

That is true, though I don’t think that would necessarily be a problem because I think that the executeCommandLine that is stuck running before the reload of .rules file will be killed and thus the script itself is killed. So a reload would relatively cleanly restart the script.

I still don’t recommend it.

Does that mean the thread should have been reported as off-topic? This is not a Linux forum except where OH is involved.

It is in the Off Topic category.

1 Like

well we can argue about that, but technically speaking it does not matter what kind of script cron run.
I use @reboot flag for ages on various systems and as it is most reliable solution out there with less than low afford to do it, I’ll go with it anytime :wink:

it is not intended to run services on reboot yes, but scripts? different story

What’s the difference between a service and script that never exits? That’s the distinction here. This script is starting a program and keeping it running as long as the system is running. Sounds like a good description of a service? So why not use the mechanism built into the operating system for services to run it?

Sorry, I did not see any category when I looked at the top of the thread.

Off Topic shows up as no category. So if you don’t see a category it is automatically off topic.

Well thanks for all the advice everyone! I started this thread then went to bed, woke up and found this discussion!
Apologies to the Off Topic police :wink: I was tired and missed giving it a catagory.

Looking at it I will probably give the Systemd a shot and see how it goes.

Thanks for the info Rich, wow something else to learn systemd… yes I am running OpenHABian by the way.
Right I have tried my best but can not get this working, my brain is boiling at the moment. I have read quite a few tutorials on systemd singing its praises and saying its easy…Hmm maybe its something fundamental or staring right back at me.

I have an .sh file as below in the root directory I think (go easy on me I havent touched Linux until this week!).

ffmpeg -y -rtsp_transport tcp -i rtsp://***********@192.168.1.80:554/Streaming/Channels/301 -an -vf fps=1/10 -s 800x450 -update 1 /etc/openhab2/html/img.jpg &

This works as it should when I manually trigger it from Putty logged in as Sudo su.

I Have also created a service file called cctv.service as below, sorry copied your root thing as well :wink:

[Unit]
Description=System service to restart cctv.sh on reboot.

[Service]

User=root
Type=simple
ExecStart=/cctv.sh

[Install]
WantedBy=multi-user.target

When I inspect to see if the service is running, I get this

[16:08:12] root@openHABianPi:~# systemctl status cctv
â—Ź cctv.service - System service to restart cctv.sh on reboot.
   Loaded: loaded (/etc/systemd/system/cctv.service; enabled; vendor preset: enabled)
   Active: inactive (dead)

So I thought permissions, these are whats set

[16:00:59] root@openHABianPi:~# ls
cctv.service  cctv.sh  ffmpeg
[16:01:01] root@openHABianPi:~# ls -l
total 12
-rwxr-xr-x  1 root root  155 Jul 12 16:02 cctv.service
-rwxr-xr-x  1 root root  229 Jul 12 13:38 cctv.sh
drwxr-xr-x 19 root root 4096 Jul  7 18:49 ffmpeg

I am trying to get cctv.sh to run in the background (hence the &) taking snapshots from my cameras and feeding them to my habpanel (when it arrives!). I would have liked to use the cctv binding but my NVR is a cheap fake :unamused:
Is my Execstart line in the service file correct for an .sh file in the root directory? Also after following the tutorial on the link you posted I am not sure if I need anything more in the [unit] section?
Any pointers greatly received.
Cheers

Once you create the .service file in the proper directory (I forget if it’s /etc/systemd/services or /var/lib/systemd/services) with the proper permissions you next need to enable it.

sudo systemctl enable cctv.service

Then you start it by running

sudo systemctl start cctv.service

You do not want to run a service like this with &. Systemd handles running it in the background for you.

The path is correct. A more appropriate path would be /opt/cctv or perhaps even /etc/cctv. There are lots of reasons I wont get into for why you never put files directly in /.

I suspect that the problem is the /.

After running systemctl start, you can run systemctl status and see the output from the script. That usually tells you what went wrong if it’s something like a permission problem.

Hi Rich,
Thanks for the pointers. I dont really know what was going on. I tried all the things you suggested above but it didnt want to start the service, just kept getting dead warnings in the shell.
So I deleted all the files, services etc and started again!
It now works, so here are my settings and files for anyone else that gets this problem.

service file located in /etc/systemd/system with permissions set to 644 (had to add the After=network.target to delay the service unless there was a network connection)

[Unit]
Description=System service to restart cctv.sh on reboot.
Requires=network.target
After=network.target

[Service]
Type=simple
ExecStart=/bin/bash /opt/cctv.sh

[Install]
WantedBy=multi-user.target

and my cctv.sh file located in /opt with permissions set to 644 (Note the removal of the &)

#!/bin/bash
#ffmpeg cctv script to get snapshot from cctc cameras

ffmpeg -y -rtsp_transport tcp -i rtsp://USER:PASWORD@192.168.1.80:554/Streaming/Channels/301 -an -vf fps=1/10 -s 800x450 -update 1 /etc/openhab2/html/img.jpg

and lastly the sitemap

Frame label=""
        Text label="CCTV" icon="camera" {
            
            Image url="http://192.168.1.119:8080/static/img.jpg" refresh=10000
        }

Hope this helps anybody else. Now just need to add some more cameras and look at a Habpanel setup…
And thanks to those that helped!

1 Like