Whats the best way of implementing cronjobs for an application. I have deployed my application to three devices and had to individually setup cronjobs for each of them. Is there a method to include the cronjob in the build, so that it can be included in future deployments?
Hi @pjs
Just to clarify, this is a cronjob for a running application or a cronjob you’re running on the host OS? You should be able to install both cron
and a crontab
file for your application as part of the Dockerfile for your application (or service) depending on the base image OS.
If this isn’t what you meant, could you go into a bit more detail about what you’re trying to achieve?
Best regards,
Heds
Hi, thanks for the quick response. The cronjob is to turn on and off TV monitors using CEC. I’d set them up under the main terminal.
Hey again,
In that case, it sounds like you should definitely just be able to use crontab
in your Dockerfile to do this:
A relevant portion would be something like this:
RUN apt-get update && apt-get -y install cron
COPY monitor-cronjob /usr/src/app/monitor-cronjob
RUN crontab /usr/src/app/monitor-cronjob
with a relevant entry script called via CMD
which runs cron
on startup.
Hopefully this answers your question!
Best regards,
Heds
Thanks very much. Will try that. One other question I have, with my current cronjobs, some of my devices sometimes fail in running. I have three Raspberry 3 devices running screenly OSE. Some days the cronjob on all devices will successfully turn on and off the TVs, but some days its a miss, two might turn on and one remains off.
I know Raspberry Pis do have wifi power management which turns off the wifi during inactivity, all the devices are on wifi. Do you think it could be that the wifi might be off during some of the cronjob calls, and since the device is unable to get the current time, the cronjob fails?
What do you think could be the cause of this?
Hi again,
You’re very welcome.
The time issue sounds interesting, but if your devices are connecting via wifi, then they’ll still be connected to our VPN (even if your application is not streaming logs) which will allow remote access. This uses a heartbeat to ensure that a connection can occur when required, and as such NTP should be synced correctly at all times. So it doesn’t sound like it’s datetime related, as cron should be able to acquire the correct time whenever it polls.
Does your cronjob do anything to ensure the instruction to turn on/off the monitors has succeeded (is there an API or something similar you can query)? If so, might be worth retrying until it’s confirmed? If it’s a bitset (or similar), maybe setting it to ‘off’ (or ‘on’) several times might work if maybe it fails the first time?
If you could get some logging going which reports the time when this happens, it would help prove it isn’t time related.
Best regards,
Heds