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
2 Likes

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

That worked perfectly. Thanks.

I am new to HA, have a script that works if I run from a terminal window via ssh or the terminal added to HA. I have been able to find where to build the automation, but how do you get the shell_command linked to a service? All the searches talk about scripts, but not adding a service as a shell script.

Thank you

I’m not sure exactly what you mean, if you created a shell command (and added to your yaml - default is /config/shell_commands.yaml) then the shell command is the service as you can see in the above example I posted.

Ok; Thank you. I was missing the part about adding that shell_commands.yaml to the configuration.yaml as an include. After I added the last line below the above example worked.

Configure a default setup of Home Assistant (frontend, api, etc)

default_config:

Text to speech

tts:

  • platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
#New line to configuration.yaml to include linux shell scripts
shell_command: !include shell_commands.yaml

Hi p4mr, I have the same problem that my shell script in /etc/periodic/… does not execute. I have removed the .sh suffix, added #!/bin/sh to the start of the file, but I won’t execute. Any other good ideas??

Thanks so much!

Have you changed the permissions on the file to allow it to execute ? Assuming it’s owned by “root”, you’ll need to run

chmod 755 <filename>

to change the permission to rwx (owner), and r-x (group, and other)

When you list the directory (ls -la), it should show up in yellow (or green, I’m colourblind :slight_smile: ) text to indicate it is an executable file.