Help needed - Cron job not working with HA OS

Hi,

Am having a couple of issues getting HA to execute a cron job, and hoping a fresh set of eyes can point me in the direction as to why.

  1. Firstly, whenever I reboot HA, the cron job gets removed from the crontab file. How do I stop this from happening ?

  2. Secondly, I can’t actually get HA to run the cron job anyway.

In terms of setup, this is my HA install.

image

This is the code that I’ve added to the crontab file

*/15     *       *       *       *       run-parts /root/config/shell_scripts

This should run all/any shell scripts it finds in the shell_scripts folder every 15 minutes, although it doesn’t (it never runs at all). If I change it to the below (which should be another way of running the script), that also doesn’t work.

*/15     *       *       *       *       /bin/sh /root/config/shell_scripts/update_audio_choices.sh

I’ve checked the .sh script has correct executable settings, and can be run locally from that folder

image

Issuing “crontab -l” shows that the line(s) have been entered correctly, so am at a loss as to why they’re not running.

Does anyone have any ideas what I’m doing wrong ?

Not exactly solved, but I’ve got a workaround.

  1. I removed the “.sh” suffix, and added #!/bin/sh to the start of the file to let cron know it’s a shell executable.
  2. I then moved the script into /etc/periodic/hourly, so it now it runs on an hourly basis.

It’s not perfect, but does the job I need. I’d still like to know how to set it up as a cron job though if anyone has any ideas.

1 Like

Instead of Cron jobs you could schedule it directly into HA using automation and shell commands. I have tons of these that I use for various purposes and doing it in HA has the added benefit of being able to set conditions on which scripts to execute and when. For example, here is what I use to truncate my log every hour:

shell_commands.yaml:

shell_rotate_log: '/config/logreset.sh'

automation:

alias: 'Schedule: Clear Logs Each Hour'
description: ''
trigger:
  - platform: time_pattern
    hours: /1
    minutes: '59'
condition: []
action:
  - service: shell_command.shell_rotate_log
    data: {}
mode: single
1 Like

Doh, I hadn’t thought of doing it that way ! :man_facepalming:

That worked perfectly. Thanks.