duckredbeard
Posts: 144
Joined: Mon Apr 29, 2019 1:53 am

Using crontab schedule a script that loops

Thu Aug 27, 2020 6:30 pm

I have a home security system that monitors a few doors and a motion sensor. Instead of having it start on boot (which I've not figured out), I'd like to have it start on crontab. Perhaps starting it every hour as a safeguard would be good. Since this script loops, would crontab start a duplicate of the script? Would I then have multiple instances of the script always running? Should I have a crontab to kill it before it starts it again?
You can't know your limits until you exceed them.

User avatar
thagrol
Posts: 3731
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Using crontab schedule a script that loops

Thu Aug 27, 2020 10:00 pm

duckredbeard wrote:
Thu Aug 27, 2020 6:30 pm
I have a home security system that monitors a few doors and a motion sensor. Instead of having it start on boot (which I've not figured out), I'd like to have it start on crontab. Perhaps starting it every hour as a safeguard would be good. Since this script loops, would crontab start a duplicate of the script?
Unless you take steps to prevent it, yes.
Would I then have multiple instances of the script always running?
Probably, but that depends on your script and what resources you're using. If there are conflicts you may find one or all instances of your script will crash.
Should I have a crontab to kill it before it starts it again?
Possibly.

Personally I'd use systemd and make sure it's configured to restart the script if/when it exits.

You might also find it worthwhile having your script check to see if there is an instance already running and exit immediately if there is.

A forum search should point you in the right direction for using systemd.
Arguing with strangers on the internet since 1993.

RonR
Posts: 1683
Joined: Tue Apr 12, 2016 10:29 pm
Location: US

Re: Using crontab schedule a script that loops

Thu Aug 27, 2020 10:06 pm

Simply use a 'sleep' command inside the loop.

Launch the script from /etc/rc.local to run in the background by adding a trailing ' &' to the command line.

Code: Select all

#!/bin/bash

SLEEP=15  # Execute the loop 4 times per minute.

while :
do

# Do the monitoring

  sleep ${SLEEP}
done

jbudd
Posts: 1499
Joined: Mon Dec 16, 2013 10:23 am

Re: Using crontab schedule a script that loops

Fri Aug 28, 2020 7:12 pm

use systemd and make sure it's configured to restart the script if/when it exits.
Yes!
Simply use a 'sleep' command inside the loop.
Surely it's not a good idea for a home security system to sleep for 15 seconds?

RonR
Posts: 1683
Joined: Tue Apr 12, 2016 10:29 pm
Location: US

Re: Using crontab schedule a script that loops

Fri Aug 28, 2020 7:24 pm

jbudd wrote:
Fri Aug 28, 2020 7:12 pm
Simply use a 'sleep' command inside the loop.
Surely it's not a good idea for a home security system to sleep for 15 seconds?

That was just an example of a polling loop. Make the sleep value 0.1 seconds and it will poll the doors and motion sensor 10 times per second (which is almost certainly overkill and an unnecessarily heavy load on the Pi).

User avatar
thagrol
Posts: 3731
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Using crontab schedule a script that loops

Fri Aug 28, 2020 9:55 pm

RonR wrote:
Fri Aug 28, 2020 7:24 pm
jbudd wrote:
Fri Aug 28, 2020 7:12 pm
Simply use a 'sleep' command inside the loop.
Surely it's not a good idea for a home security system to sleep for 15 seconds?

That was just an example of a polling loop. Make the sleep value 0.1 seconds and it will poll the doors and motion sensor 10 times per second (which is almost certainly overkill and an unnecessarily heavy load on the Pi).
TBH, I interpreted the problem that needed solving to be more along the lines of "how do I make sure my script which already has a polling loop is always running?" rather than "how do I use cron to poll my sensors at a regular interval". Hence my suggestion to use systemd with restart on failure.

duckredbeard was quite clear in his post that "this script loops" so a further external polling loop isn't needed.
Arguing with strangers on the internet since 1993.

RonR
Posts: 1683
Joined: Tue Apr 12, 2016 10:29 pm
Location: US

Re: Using crontab schedule a script that loops

Fri Aug 28, 2020 10:12 pm

thagrol wrote:
Fri Aug 28, 2020 9:55 pm
duckredbeard was quite clear in his post that "this script loops" so a further external polling loop isn't needed.
duckredbeard wrote:
Thu Aug 27, 2020 6:30 pm
Instead of having it start on boot (which I've not figured out), I'd like to have it start on crontab.

I read that as the OP not knowing how to start a script on boot that loops. The OP certainly left a bit to the reader's interpretation. Maybe he'll return and clarify what he's actually looking for.

User avatar
thagrol
Posts: 3731
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Using crontab schedule a script that loops

Fri Aug 28, 2020 10:16 pm

RonR wrote:
Fri Aug 28, 2020 10:12 pm
Maybe he'll return and clarify what he's actually looking for.
Indeed.
Arguing with strangers on the internet since 1993.

duckredbeard
Posts: 144
Joined: Mon Apr 29, 2019 1:53 am

Re: Using crontab schedule a script that loops

Sat Aug 29, 2020 12:33 pm

I've had issues where a power outage or forced reboot requires me to manually restart the program. Since the pi is in my garage ceiling and without a monitor, I use TeamViewer to interface remotely. Sure I could use other methods (VNC, SSH), I just haven't gotten around to exploring those options.

The goal is to have the most reliable way to start this program on boot. Even if it a few minutes after boot and power is stable. This is the highest goal. A process that isn't an excessive burden on the pi is also a priority.

I'd be glad to share the program, but I need to redact a few lines first.
You can't know your limits until you exceed them.

duckredbeard
Posts: 144
Joined: Mon Apr 29, 2019 1:53 am

Re: Using crontab schedule a script that loops

Sat Aug 29, 2020 12:36 pm

I've had issues where a power outage or forced reboot requires me to manually restart the program. Since the pi is in my garage ceiling and without a monitor, I use TeamViewer to interface remotely. Sure I could use other methods (VNC, SSH), I just haven't gotten around to exploring those options.

The goal is to have the most reliable way to start this program on boot. Even if it a few minutes after boot and power is stable. This is the highest goal. A process that isn't an excessive burden on the pi is also a priority.

I'd be glad to share the program, but I need to redact a few lines first.
You can't know your limits until you exceed them.

User avatar
neilgl
Posts: 2544
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Using crontab schedule a script that loops

Sat Aug 29, 2020 12:53 pm

Won’t a simple @reboot in crontab work? e.g.

Code: Select all

@reboot python3 -u /home/pi/scripts/neo.py >> /home/pi/scripts/neo.log 2>&1

duckredbeard
Posts: 144
Joined: Mon Apr 29, 2019 1:53 am

Re: Using crontab schedule a script that loops

Sat Aug 29, 2020 1:36 pm

neilgl wrote:
Sat Aug 29, 2020 12:53 pm
Won’t a simple @reboot in crontab work? e.g.

Code: Select all

@reboot python3 -u /home/pi/scripts/neo.py >> /home/pi/scripts/neo.log 2>&1
I have found that to be unreliable. Adding to etc/rc.local has also been unreliable.
You can't know your limits until you exceed them.

User avatar
thagrol
Posts: 3731
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Using crontab schedule a script that loops

Sat Aug 29, 2020 2:07 pm

neilgl wrote:
Sat Aug 29, 2020 12:53 pm
Won’t a simple @reboot in crontab work? e.g.

Code: Select all

@reboot python3 -u /home/pi/scripts/neo.py >> /home/pi/scripts/neo.log 2>&1
Only if the script in question doesn't rely on networking or the desktop GUI. Those almost ceratinly won't be available when the cron job runs. Even if the desktop is available, the environment in which cron runs won't be configured to allow access to it.
duckredbeard wrote:
Sat Aug 29, 2020 1:36 pm
I have found that to be unreliable. Adding to etc/rc.local has also been unreliable.
In which case, the first step in solving the problem is surely to find out why it's unreliable. Try capturing standard out and standard error from your script into a log file then examining the log.

Both cron and rc.local run with a different environment (including current working directory) to you login shell. rc.local will also run your script as root. cron may do so if you set it up via "sudo crontab -e" ratehr than "crontab -e".
Arguing with strangers on the internet since 1993.

pfletch101
Posts: 630
Joined: Sat Feb 24, 2018 4:09 am
Location: Buffalo, NY, USA

Re: Using crontab schedule a script that loops

Sat Aug 29, 2020 9:25 pm

duckredbeard wrote:
Sat Aug 29, 2020 1:36 pm
neilgl wrote:
Sat Aug 29, 2020 12:53 pm
Won’t a simple @reboot in crontab work? e.g.

Code: Select all

@reboot python3 -u /home/pi/scripts/neo.py >> /home/pi/scripts/neo.log 2>&1
I have found that to be unreliable. Adding to etc/rc.local has also been unreliable.
Running from systemd is generally the best way to handle a script that needs to start at reboot and run continuously while the system is running. If you configure your 'After' conditions for the service correctly you can usually ensure that systemd does not try to start it until all its prerequisites are satisfied. On the quite rare (in my experience) occasions where there is still a problem at reboot, adding a sleep(30) at the start of the script will usually solve it

duckredbeard
Posts: 144
Joined: Mon Apr 29, 2019 1:53 am

Re: Using crontab schedule a script that loops

Sat Aug 29, 2020 9:30 pm

pfletch101 wrote:
Sat Aug 29, 2020 9:25 pm
duckredbeard wrote:
Sat Aug 29, 2020 1:36 pm
neilgl wrote:
Sat Aug 29, 2020 12:53 pm
Won’t a simple @reboot in crontab work? e.g.

Code: Select all

@reboot python3 -u /home/pi/scripts/neo.py >> /home/pi/scripts/neo.log 2>&1
I have found that to be unreliable. Adding to etc/rc.local has also been unreliable.
Running from systemd is generally the best way to handle a script that needs to start at reboot and run continuously while the system is running. If you configure your 'After' conditions for the service correctly you can usually ensure that systemd does not try to start it until all its prerequisites are satisfied. On the quite rare (in my experience) occasions where there is still a problem at reboot, adding a sleep(30) at the start of the script will usually solve it
I will look into this. In the middle of replacing the pi with a 4GB 4 and hope to be done migrating code tomorrow before hardware mount.

Thanks for the info!
You can't know your limits until you exceed them.

User avatar
neilgl
Posts: 2544
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Using crontab schedule a script that loops

Sat Aug 29, 2020 10:36 pm

Er it always works for me on a headless pi, so it can’t be unreliable? What fails if you run it like that?

pfletch101
Posts: 630
Joined: Sat Feb 24, 2018 4:09 am
Location: Buffalo, NY, USA

Re: Using crontab schedule a script that loops

Sat Aug 29, 2020 10:47 pm

neilgl wrote:
Sat Aug 29, 2020 10:36 pm
Er it always works for me on a headless pi, so it can’t be unreliable? What fails if you run it like that?
Running scripts from cron with @reboot generally works either if they don't try to access any resources outside the Pi (e.g. other local or remote systems, or sensors with significant initialization requirements) or if the script handles the non-immediate availability of a remote resource by waiting and retrying. In my experience, it doesn't usually work otherwise.

User avatar
thagrol
Posts: 3731
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Using crontab schedule a script that loops

Sun Aug 30, 2020 12:04 am

pfletch101 wrote:
Sat Aug 29, 2020 9:25 pm
duckredbeard wrote:
Sat Aug 29, 2020 1:36 pm
neilgl wrote:
Sat Aug 29, 2020 12:53 pm
Won’t a simple @reboot in crontab work? e.g.

Code: Select all

@reboot python3 -u /home/pi/scripts/neo.py >> /home/pi/scripts/neo.log 2>&1
I have found that to be unreliable. Adding to etc/rc.local has also been unreliable.
Running from systemd is generally the best way to handle a script that needs to start at reboot and run continuously while the system is running. If you configure your 'After' conditions for the service correctly you can usually ensure that systemd does not try to start it until all its prerequisites are satisfied. On the quite rare (in my experience) occasions where there is still a problem at reboot, adding a sleep(30) at the start of the script will usually solve it
Gues you didn't read through the entire thread. systemd was suggested in the second post...

Plus moving to systemd won't make things more reliable unless you know why things aren't working reliably at the moment.
Arguing with strangers on the internet since 1993.

User avatar
thagrol
Posts: 3731
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Using crontab schedule a script that loops

Sun Aug 30, 2020 12:14 am

pfletch101 wrote:
Sat Aug 29, 2020 10:47 pm
neilgl wrote:
Sat Aug 29, 2020 10:36 pm
Er it always works for me on a headless pi, so it can’t be unreliable? What fails if you run it like that?
Running scripts from cron with @reboot generally works either if they don't try to access any resources outside the Pi (e.g. other local or remote systems, or sensors with significant initialization requirements) or if the script handles the non-immediate availability of a remote resource by waiting and retrying. In my experience, it doesn't usually work otherwise.
There are many other reasons why running scripts from cron may not work*. For example:
  • Different user (root vs normal user) though that often boils down to a permissions problem
  • Different and restricted $PATH
  • Different shell
  • Different set of environment variables
  • Different current working directory (something the script relies on may not be found or the script does run but doesn't write it's output where it's expected to)
  • Script/program requires the desktop
  • Script requires a framebuffer but no monitor conected (currently 4B only)
  • ...
Without knowing what the error messages are it's nigh on impossible to suggest a fix.

*: assuming the script itself is bug free.
Arguing with strangers on the internet since 1993.

User avatar
neilgl
Posts: 2544
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Using crontab schedule a script that loops

Sun Aug 30, 2020 11:34 am

Yes, what does fail for the OP when running it @reboot?

Return to “Beginners”