To get regular weather updates into my Redis database the scheme I am trying to use is as follows
I have a PHP script that fetches the weather from the relevant weather API. It goes something like this
<?php
function getWeather()
{
if (weatherupdaterequired)
{
//weather API call
//parse and store to database
}
}
while (true)
{
getWeather();
sleep(30);
}
?>
which is stored in my /usr/local/bin
folder. In the same folder I have a shell script, runweather
which does just this:
#!/bin/sh
nohup php /usr/local/bin/echoweather.php >/dev/null 2>&1 &
I normally tend to use #!/bin/bash
but in this instance I found that when run at startup - as you will see below - only #!/bin/sh
works. I assume that this has something to do with the bash shell not yet being available.
I then created a symlink to runweather
ln -s /usr/local/bin/runweather /etc/init.d/runweather
and then another symlink
ln -s /etc/init.d/runweather /etc/rc2.d/S99runweather
A few explanatory notes
- It is
/usr/local/bin/echoweather.php
that is doing all the real work. It runs at 30s intervals and sleeps when not working - Just prior to terminating each run it places an ephemeral Redis key
$redis-
setEx("weatherreport",29,$echoCount)` which I can use to keep tabs on its health - Placing the shell script that gets
echoweather.php
running at startup in/usr/local/bin
, then symlinking it in/etc/init.d
only to then symlink it again in/etc/rc2.d
might look convoluted. I did this since I found that if I place the actual shell script in/etc/init.d
and then symlink it to/etc/rc2.d
it does not execute.
This scheme is working. I rebooted my server several times and checked on the health of echoweather.php
by looking for the weatherreport
key in Redis via redis-cli - always present and correct. However, I am a rank amateur when it comes to dealing with Ubuntu startup scripts. Perhaps there is a simpler way to do things? I'd be much obliged to anyone who might be able to comment.