# CRON tasks question : Newb tries to launch simple project RPi4+BalenaOS/Cloud+Screenly

**URL:** <https://forums.balena.io/t/cron-tasks-question-newb-tries-to-launch-simple-project-rpi4-balenaos-cloud-screenly/279332>\
**Category:** balenaOS\
**Tags:** support, docker, raspberrypi4\
**Created:** [March 29, 2021, 2:36pm UTC](https://forums.balena.io/t/cron-tasks-question-newb-tries-to-launch-simple-project-rpi4-balenaos-cloud-screenly/279332 "2021-03-29T14:36:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pesant](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/pesant/32/5681_2.png) [@pesant](https://forums.balena.io/u/pesant)\
**Post date:** [March 29, 2021, 2:36pm UTC](https://forums.balena.io/t/cron-tasks-question-newb-tries-to-launch-simple-project-rpi4-balenaos-cloud-screenly/279332/1 "2021-03-29T14:36:44Z")

</div>

Hi there,  
I just started a personal project without any knowledge and know how to do it.  
I discovered 3 days ago, the BalenaCloud Platform and created an account. I followed step by step the tutorial and successed to connect my new Raspberry Pi4 B model 4GB RAM with BalenaOS on it linked to the Cloud Platform. I made my first application with :

- BalenaOS
- Redis
- srly-ose-celery
- srly-ose-server
- srly-ose-viewer
- srly-ose-websocket

Everything works fine. I had some problems to fit the digital signage exactly with my HDMI FullHD cheap TV’s screen and it was hard to find how to change UTC time to get the right time in the Screenly browser alike (I used commands directly in srly-ose-viewer’s Terminal in the cloud interface).

```
sudo rm /etc/localtime
sudo ln -s /usr/share/zoneinfo/Europe/Paris/etc/localtime

```

My question is **how to add tasks in Celery with Redis (I guess) linked to the worker (beat???) to get a kind of cron jobs to switch on/off the screen** by HDMI CEC every night and in the morning.  
I can switch off HDMI and put TV in Standby and wake it up by using commands in Terminal, it works fine but I don’t have any idea how to make it automaticlly.

```
echo 'standby 0.0.0.0' | cec-client -s -d 1
echo 'on 0.0.0.0' | cec-client -s -d 1

```

The tasks could be :

```
0 6 * * * echo 'on 0.0.0.0' | cec-client -s -d 1 >/dev/null 2>&1
30 20 * * * echo 'standby 0.0.0.0' | cec-client -s -d 1 >/dev/null 2>&1

```

I made some research and I understand I have to put that kind of code somewhere, but I don’t know how and where to do it…

```
    from celery.schedules import crontab

    app.conf.beat_schedule = {
        # Executes every day in the morning at 6:00 a.m.
        'add-every-morning': {
            'task': 'tasks.add',
            'schedule': crontab(hour=6, minute=00, day_of_week=0-6),
            'args': (16, 16), // <-- Don't know what it means!!!
        },
    }

    from celery.schedules import crontab

    app.conf.beat_schedule = {
        # Executes every day at night at 8:30 p.m.
        'add-every-night': {
            'task': 'tasks.add',
            'schedule': crontab(hour=20, minute=30, day_of_week=0-6), // 0=sunday
            'args': (16, 16), // <-- Don't know what it means!!!
        },
    }

```

`app.conf.timezone = 'UTC' // ???? Europe/Paris (yes, I'm French)`

Thank you in advance for your kind help!!

---

<div class="post-metadata">

**Author:** ![rahul-thakoor](https://avatars.discourse-cdn.com/v4/letter/r/9de053/32.png) [@rahul-thakoor](https://forums.balena.io/u/rahul-thakoor)\
**Post date:** [April 2, 2021, 1:43pm UTC](https://forums.balena.io/t/cron-tasks-question-newb-tries-to-launch-simple-project-rpi4-balenaos-cloud-screenly/279332/5 "2021-04-02T13:43:06Z")

</div>

Hey @pesant

Bienvenue 🙂 !

1. Regarding setting the timezone in your application container, you can [set a service variable](https://www.balena.io/docs/learn/manage/serv-vars/#environment-and-service-variables) `TZ` with the [desired value](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones).

2. As for setting up the cron job, you can possible achieve this with a cron task itself. I’m not sure how (and why) you would want to do it with celery?

You can find an example of a scheduler service [here](https://github.com/balenalabs/balena-dash/tree/master/scheduler). In the example it switches on/off the backlight on a PiDisplay. You would have to use the respective commands you specified

I hope this helps. Let us know

---

<div class="post-metadata">

**Author:** ![pesant](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/pesant/32/5681_2.png) [@pesant](https://forums.balena.io/u/pesant)\
**Post date:** [April 2, 2021, 8:40pm UTC](https://forums.balena.io/t/cron-tasks-question-newb-tries-to-launch-simple-project-rpi4-balenaos-cloud-screenly/279332/7 "2021-04-02T20:40:52Z")

</div>

> Hey @pesant
> 
> Bienvenue 🙂 !
> 
> 1. Regarding setting the timezone in your application container, you can [set a service variable](https://www.balena.io/docs/learn/manage/serv-vars/#environment-and-service-variables) `TZ` with the [desired value](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones).
> 2. As for setting up the cron job, you can possible achieve this with a cron task itself. I’m not sure how (and why) you would want to do it with celery?
> 
> You can find an example of a scheduler service [here](https://github.com/balenalabs/balena-dash/tree/master/scheduler). In the example it switches on/off the backlight on a PiDisplay. You would have to use the respective commands you specified
> 
> I hope this helps. Let us know

Hi @rahul-thakoor!  
Thanx to welcome me! 🙂

> Regarding setting the timezone in your application container, you can [set a service variable](https://www.balena.io/docs/learn/manage/serv-vars/#environment-and-service-variables) `TZ` with the [desired value](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones).

Yep, I get it, saw it by myslef few days ago and related to that post below, another expert like you confimed too few hours after I changed my configuration.

> [@Timezone Troubles](https://forums.balena.io/t/timezone-troubles/122430/19):
>
> Hi Vipul, Thanx to answer back. I don’t really know if it works or not by creating TIMEZONE. First, I created an environment variable as mentionned in the doc (not sure to remember). Anyway, it wasn’t what I expected. I thought it changed my logs with the right timezone and something happenned because services stopped and automatically restarted. UTC switch turned to grayed and off and TimeStamps too but the good (for me) was reading as variable I guess.! I tried TZ to…

Concerning **cron task** , cron doesn’t work for me and because of that :

> **[Remotely turn on/off a connected TV](https://forums.screenly.io/t/remotely-turn-on-off-a-connected-tv/108/14)**
>
> Hi, I’m just getting back on this - I’m still new to Docker, please could you show me how to how to use Celery in setting up the task of turning on and off the TVs. This is my current setup: #Install cec-client $ apt-get install libcec3...

(at the end of discussion, specially from _ **vpertesson** _)

So, thank you to re-open reflections about it and considering that possibility…  
I have to try and adapt the code below to my wishes:

```
#!/bin/bash

if [! -z ${ENABLE_BACKLIGHT_TIMER+x}] && ["$ENABLE_BACKLIGHT_TIMER" -eq "1"]

then

  (crontab -l; echo "${BACKLIGHT_ON:-0 8 * * *} /usr/src/backlight_on.sh") | crontab -

  (crontab -l; echo "${BACKLIGHT_OFF:-0 23 * * *} /usr/src/backlight_off.sh") | crontab -

fi

crond -f

```

I also saw that too:

> [@Trying tom make schedule for turning on off display/HDMI](https://forums.balena.io/t/trying-tom-make-schedule-for-turning-on-off-display-hdmi/5967):
>
> i am new to balena and trying to add display turn on/off to a application. the service appers on the application and running but not working . ther error code i am getting is 10.04.19 12:51:18 (+0200) display /usr/src/start.sh: 7: /usr/src/start.sh: Syntax error: end of file unexpected (expecting “then”) have tried too google the problem but no distinct explanation what the trouble is. have added the Device Environment Variables DISPLAY\_OFF 43 12 \* \* \* DISPLAY\_ON 0 7 \* \* \* ENABL…

Could you send me a simple procedure to make it?

1/ create my own scheduler script with vsCode, Atom etc…  
a) start.sh ← cron job  
b) hdmi\_standby.sh ← CEC-CLIENT bash command  
c) hdmi\_on.sh

2/ push it on my device with Balena-CLI

Do I have to have another container docker as `scheduler` in your example or I have to modify an actual one (srly-\*)?

Thank you very much!

---

<div class="post-metadata">

**Author:** ![dtischler](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/dtischler/32/2483_2.png) [@dtischler](https://forums.balena.io/u/dtischler)\
**Post date:** [April 9, 2021, 9:25pm UTC](https://forums.balena.io/t/cron-tasks-question-newb-tries-to-launch-simple-project-rpi4-balenaos-cloud-screenly/279332/8 "2021-04-09T21:25:52Z")

</div>

Hi Christophe, sorry for the delayed reply…I did find another example we have for Cron Jobs, located here: [GitHub - balena-io-playground/cron-example: Simple resin.io cron example.](https://github.com/balena-io-playground/cron-example)

In the Dockerfile, you can see how it is implemented: [cron-example/Dockerfile.template at master · balena-io-playground/cron-example · GitHub](https://github.com/balena-io-playground/cron-example/blob/master/Dockerfile.template#L12)

Hope that helps, thanks!

---

<div class="post-metadata">

**Author:** ![pesant](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/pesant/32/5681_2.png) [@pesant](https://forums.balena.io/u/pesant)\
**Post date:** [April 11, 2021, 11:43am UTC](https://forums.balena.io/t/cron-tasks-question-newb-tries-to-launch-simple-project-rpi4-balenaos-cloud-screenly/279332/10 "2021-04-11T11:43:28Z")

</div>

Hi @dtischler,

I will check this in hurry. Thanx for the links, I guess it will answer all my cron problems.

See you!

---

<div class="post-metadata">

**Author:** ![pesant](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/pesant/32/5681_2.png) [@pesant](https://forums.balena.io/u/pesant)\
**Post date:** [April 11, 2021, 11:47am UTC](https://forums.balena.io/t/cron-tasks-question-newb-tries-to-launch-simple-project-rpi4-balenaos-cloud-screenly/279332/11 "2021-04-11T11:47:09Z")

</div>

Hi again @dtischler,

Do you know how and where I can add it to my actual project?  
(Which service I have to choose? How to implement that code in actual code?)

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![dtischler](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/dtischler/32/2483_2.png) [@dtischler](https://forums.balena.io/u/dtischler)\
**Post date:** [April 12, 2021, 11:31pm UTC](https://forums.balena.io/t/cron-tasks-question-newb-tries-to-launch-simple-project-rpi4-balenaos-cloud-screenly/279332/12 "2021-04-12T23:31:46Z")

</div>

Hi Christophe, sorry to say, but no I do not have any idea where in your codebase you would need to introduce this…you might have better luck asking over on the Screenly Forums, as their community likely has more experience with this type of a topic.

---

<div class="post-metadata">

**Author:** ![rahul-thakoor](https://avatars.discourse-cdn.com/v4/letter/r/9de053/32.png) [@rahul-thakoor](https://forums.balena.io/u/rahul-thakoor)\
**Post date:** [April 14, 2021, 7:16am UTC](https://forums.balena.io/t/cron-tasks-question-newb-tries-to-launch-simple-project-rpi4-balenaos-cloud-screenly/279332/14 "2021-04-14T07:16:10Z")

</div>

Hey @pesant  
With respect to this:

> Could you send me a simple procedure to make it?  
> 1/ create my own scheduler script with vsCode, Atom etc…  
> a) start.sh ← cron job  
> b) hdmi\_standby.sh ← CEC-CLIENT bash command  
> c) hdmi\_on.sh  
> 2/ push it on my device with Balena-CLI  
> Do I have to have another container docker as scheduler in your example or I have to modify an actual one (srly-\*)?

I would suggest you add another service for the scheduler similar to what we have done in [balenaDash](https://github.com/balenalabs/balena-dash). You can find the scheduler files [here](https://github.com/balenalabs/balena-dash/tree/master/scheduler). You would need to modify your docker-compose file to include the service in the services section. Assuming you copied the files to a directory called `scheduler`, the following should work:

```auto
  scheduler:
    restart: always
    build: ./scheduler
    privileged: true

```

ofcourse, you will have to modify the [**backlight\_on.sh**](https://github.com/balenalabs/balena-dash/blob/master/scheduler/scripts/backlight_on.sh) and [**backlight\_off.sh**](https://github.com/balenalabs/balena-dash/blob/master/scheduler/scripts/backlight_off.sh) file to use the `cec-client` commands

Hope this helps

Thanks
