Created
December 30, 2021 01:38
-
-
Save quantum9Innovation/f866c294add01c148a860f2b68bab791 to your computer and use it in GitHub Desktop.
Run a script automatically on sunrise/sunset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Scripts that are run: | |
// On sunrise => ./day.sh | |
// On sunset => ./night.sh | |
// To run nothing: write a blank file | |
// Change these to match your location | |
const long = 0.0000 | |
const lat = 0.0000 | |
const { exec } = require('child_process') | |
const SunCalc = require('suncalc') | |
const times = SunCalc.getTimes(new Date(), long, lat) | |
const sunrise = Date.parse(times.sunrise) | |
const sunset = Date.parse(times.sunset) | |
const now = Date.now() | |
// Daytime | |
if (now > sunrise && now < sunset) { | |
let day_script = exec( | |
'./day.sh', | |
(error, stdout, stderr) => { | |
console.log(stdout); | |
console.log(stderr); | |
if (error !== null) { | |
console.log(`exec error: ${error}`); | |
} | |
}); | |
} | |
// Nighttime | |
else { | |
let night_script = exec( | |
'./night.sh', | |
(error, stdout, stderr) => { | |
console.log(stdout); | |
console.log(stderr); | |
if (error !== null) { | |
console.log(`exec error: ${error}`); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment