ferdi-t
Posts: 6
Joined: Sun Sep 02, 2018 7:45 am

Sunwait + cron, but it's not working

Sun Sep 02, 2018 7:54 am

Hello friendly people :-)

I'm setting up a cron-entry for sunwait, with the intention of controlling my lights. Alas: after several days of headaches, I haven't been able to get it right.

It's prolly something silly, as I have been able to get it to work on a previous install.

Does anyone know what I'm doing wrong?

My current crontab entry is:

Code: Select all

0 9 * * * /usr/local/bin/sunwait sun down -12.00.00 53.21917N, 6.56667W ; /bin/bash /home/pi/kastaan.sh >/dev/null 2>&1
(the -12 seems kinda strange, but it's for testing purposes).

The script itself is verified to run from cron using:

Code: Select all

44 9 * * * /bin/bash /home/pi/kastaan.sh >/dev/null 2>&1
Output of sunwait -p 53.21917N, 6.56667W:

Code: Select all

Using location:             53.219170N, 6.566670W
Date:                        2 Sep 2018
Local time:                  9:51
Day length:                 13:35 hours
With civil twilight         14:44 hours
With nautical twilight      16:13 hours
With astronomical twilight  17:52 hours
Length of twilight:  civil   0:34 hours
                  nautical   1:18 hours
              astronomical   2:08 hours
Current specified time zone: CEST (2 from UTC)
Sun transits meridian 1425 CEST
                   Sun rises 0739 CEST, sets 2111 CEST
       Civil twilight starts 0703 CEST, ends 2147 CEST
    Nautical twilight starts 0618 CEST, ends 2232 CEST
Astronomical twilight starts 0529 CEST, ends 2321 CEST
Output of /var/log/syslog shows the command was fired off, no errors.

Code: Select all

Sep  2 09:00:01 downloadserver CRON[1026]: (root) CMD (/usr/local/bin/sunwait sun down -12.00.00 53.21917N, 6.56667W ; /home/pi/kastaan.sh >/dev/null 2>&1)
Any help is GREATLY appreciated!

epoch1970
Posts: 2022
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: Sunwait + cron, but it's not working

Sun Sep 02, 2018 2:41 pm

I suggest writing scripts in /usr/local/bin that would start with "#!/bin/bash" and made executable.
Then your crontab entry (entries) would be simpler, e.g. "0 9 * * * /usr/local/bin/myscript.sh" or "0 9 * * * /usr/local/bin/myscript.sh with some passed arguments"
You can run your scripts outside cron to make sure they work.
If they work when you launch them but not when cron does, it is a matter of default environment: environment variables available to cron ($PATH etc.) are limited by default.
Also, sending everything to /dev/null doesn't help in debugging.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

ferdi-t
Posts: 6
Joined: Sun Sep 02, 2018 7:45 am

Re: Sunwait + cron, but it's not working

Sun Sep 02, 2018 3:10 pm

Thanks! The thing that puzzles me... the scripts run fine when I tell cron to execute them at any given time. But when I use sunwait, nothing gets executed.

Scripts are owned by root and made executable, btw.

epoch1970
Posts: 2022
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: Sunwait + cron, but it's not working

Sun Sep 02, 2018 3:32 pm

Found this: https://www.raspberrypi.org/forums/view ... p?t=173333
(at least the first posts before the fight begins for no reason...)
HTH
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

User avatar
scruss
Posts: 1804
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Sunwait + cron, but it's not working

Sun Sep 02, 2018 11:53 pm

Two things:
  1. You've got a stray comma in your command: it should be sunwait -p 53.21917N 6.56667W with no comma between the lat and the long
  2. If you're having cron run this at 09:00 and telling it to trigger at (sundown - 12) hours, it may never run. F'rinstance, here in Toronto, sundown's just passed (19:50). Twelve hours before that is 07:50, before the 09:00 cron trigger, so it's unlikely to fire the day you expected.Set the cron trigger to just after 2 am to be safe.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

ferdi-t
Posts: 6
Joined: Sun Sep 02, 2018 7:45 am

Re: Sunwait + cron, but it's not working

Mon Sep 03, 2018 5:16 am

Thanks! The comma might be it.

User avatar
DougieLawson
Posts: 33857
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Sunwait + cron, but it's not working

Mon Sep 03, 2018 9:50 pm

Your times for running it are off by miles. BTW, if you're 6.5W of Greenwich that puts you in Ireland and your timezone is wrong or you've got your East and West mixed up.

I'm assuming your longitude is the correct value, if not the numbers below are junk.
Your latest sunset is 20:58 GMT/21:58 IST (in June), your earliest is 16:08 (in December)
Your earliest sunrise is 03:58 GMT/04:48 IST (in June), your latest is 08:41 (in December)

So your crontab entries need to run at 04:45 waiting for up to +4:00 for sunrise and at 16:00 for +6:00 for sunset. (Note: Cron uses local times, so things get interesting with DST start and end.)

I got those times using the Sunrise/Sunset table I can generate at: http://aa.usno.navy.mil/data/docs/RS_OneYear.php
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

DirkS
Posts: 9065
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Sunwait + cron, but it's not working

Mon Sep 03, 2018 11:00 pm

DougieLawson wrote:
Mon Sep 03, 2018 9:50 pm
BTW, if you're 6.5W of Greenwich that puts you in Ireland and your timezone is wrong or you've got your East and West mixed up.
Looking at the name of the script I would say that W should be E

danjperron
Posts: 3037
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Sunwait + cron, but it's not working

Tue Sep 04, 2018 3:11 am

ok you start the check for sun set - 12Hrs from 9AM


So the sunset will be 20:09 according to your geolocation , Near Dublin.

20:09 - 12Hrs = 8:09

Hum... is the sun set - 12Hrs already pass ?????? Then you have to wait another day to check it.

I do have my lamp post using this method and it works fine

Code: Select all

0 14 * * * /usr/local/bin/sunwait -v sun down  45.5017N 75.5673W; mosquitto_pub -t "cmnd/sonoff_plug2/power" -m "1"
0 0  * * * mosquitto_pub -t "cmnd/sonoff_plug2/power" -m "0"

ferdi-t
Posts: 6
Joined: Sun Sep 02, 2018 7:45 am

Re: Sunwait + cron, but it's not working

Mon Sep 24, 2018 3:07 pm

Thanks for all the replies. But several tries later it still isn't working.

The last entry I tried was this one, but I tried a great number of variations.

Code: Select all

20 16 * * * /usr/local/bin/sunwait sun down -3.00.00 53.21917N 6.56667E; /home/pi/milight/milight 2 on
Now, how do I actually go about troubleshooting? The flag -p increases verbosity, but where is the output of sunwait and the targeted script/program logged?

Thanks a bunch!

DirkS
Posts: 9065
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Sunwait + cron, but it's not working

Mon Sep 24, 2018 3:36 pm

ferdi-t wrote:
Mon Sep 24, 2018 3:07 pm
Now, how do I actually go about troubleshooting?
Don't use cron.
Start off with minimal parameters (e.g. just a start time) and build it up from there.

danjperron
Posts: 3037
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Sunwait + cron, but it's not working

Mon Sep 24, 2018 4:44 pm

ok different angle what is milight ?

A C compiled version ?
A bash script ?

User avatar
DougieLawson
Posts: 33857
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Sunwait + cron, but it's not working

Mon Sep 24, 2018 5:28 pm

What are you trying to achieve?

This is my crontab in /etc/cron.d/sunwait

Code: Select all

[email protected]:/etc/cron.d # cat sunwait
45 4 * * * root /usr/local/bin/sunwait civ up +0:00:00 51.2N 1.13W ; /usr/local/bin/sunny.sh light;
30 15 * * * root /usr/local/bin/sunwait civ down -0:00:00 51.2N 1.13W ; /usr/local/bin/sunny.sh dark;
[email protected]:/etc/cron.d #
This is /usr/local/bin/sunny.sh

Code: Select all

#!/bin/bash

NOW=$(TZ=Europe/London date '+%T %d/%m')

if [ "$1" == "reboot" ]; then
  if [ ! -f /var/run/lightordark ];
  then
    echo $1' @ '$NOW > /var/run/lightordark
  fi
  else
  echo $1' @ '$NOW > /var/run/lightordark
fi

if [ "$1" == "light" ]; then
  /usr/local/bin/gpio -g pwm 18 0
else
  /usr/local/bin/gpio -g pwm 18 1023
fi
That writes a file at sunrise and sunset that can be used by any other program.

For example

Code: Select all

// (C) Copyright 2016, Dougie Lawson. All rights reserved.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
  FILE *fp;
  const char *delim = " ";
  char lightordark[40];
  char *stringp = lightordark;
  char *token;

  char filename[21] = "/var/run/lightordark";

  fp = fopen(filename,"r");
  fgets(lightordark, sizeof lightordark, fp);
  token = strsep(&stringp, delim);
  printf("LoD: %s", token);

  if (strcmp(token, "dark") == 0) {
    printf("Dark Token: %s\r\n", token);
    // do stuff if it's dark here
  }
  else
  {
    printf("Light Token: %s\r\n", token);
    // do stuff if it's light here
  }

  token = strsep(&stringp, delim);
  token = strsep(&stringp, delim);
  printf("Time: %s\r\n", token);

  fclose(fp);
  return 0;
}
I should probably re-write that sample in C++ as it has better string handling than plain old C.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

ferdi-t
Posts: 6
Joined: Sun Sep 02, 2018 7:45 am

Re: Sunwait + cron, but it's not working

Tue Oct 02, 2018 1:22 pm

What are you trying to achieve?
I'm ultimately trying to get the lights to switch on some time before sundown.

At the moment the script is partially working by putting the command inside a bash-script:
30 15 * * * /usr/local/bin/sunwait sun down -1.00.00 53.21917N 6.56667E; /bin/bash /home/pi/milight/slow.sh
The strange thing is that the script now executes at sundown exactly... the -1 is ignored completely.

I managed to get it working before, so I have no idea why it's causing me headaches so far... any theories?

danjperron
Posts: 3037
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Sunwait + cron, but it's not working

Tue Oct 02, 2018 2:23 pm

-1.00.00 are you sure!
Is it not suppose to be -1:00:00?

DirkS
Posts: 9065
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Sunwait + cron, but it's not working

Tue Oct 02, 2018 2:27 pm

ferdi-t wrote:
Tue Oct 02, 2018 1:22 pm
The strange thing is that the script now executes at sundown exactly... the -1 is ignored completely.
Have you tried the -p option to show more information about times, etc,

Code: Select all

sunwait -p 53.21917N 6.56667E

User avatar
scruss
Posts: 1804
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Sunwait + cron, but it's not working

Tue Oct 02, 2018 5:11 pm

Another thought: which version of sunwait are you running? The original, or the later “sunwait4windows”? I've only ever run the original, and my suggestions have been based on it.

Using ±hh.mm.ss for time offset rather than ±hh:mm:ss will likely confuse either version of sunwait. The code appears to parse arguments based on the presence of colons, which it takes to mean a time offset.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

ferdi-t
Posts: 6
Joined: Sun Sep 02, 2018 7:45 am

Re: Sunwait + cron, but it's not working

Sat Oct 06, 2018 5:47 am

That might actually.be it. Thanks.

Return to “Beginners”

Who is online

Users browsing this forum: davidcoton, EENerd and 45 guests