Problem Description
I’m trying to create a JavaScript rule in OpenHAB that runs every day at 1 AM to delete old records from a MySQL database. The rule executes the following command:
mysql --batch -B -u MYUSER -p'MYPASSWORD' -D 'MYDATABASE' -e "DELETE FROM 'MYITEM' WHERE DATE(time) = CURDATE() - INTERVAL 2 DAY;"
The command works fine when run manually in the terminal. However, when OpenHAB executes it using actions.Exec.executeCommandLine()
, the output is null and the deletion does not happen.
My JavaScript Rule
rules.JSRule({
name: "clean MySQL at 1 AM",
description: "Deletes records from the day before yesterday at 1 AM",
triggers: [
triggers.GenericCronTrigger("0/10 * * * * ?") // Runs at 1:00 AM daily
],
execute: (event) => {
let command = mysql --batch -B -u MYUSER -p'MYPASSWORD' -D 'MYDATABASE' -e "DELETE FROM 'MYITEM' WHERE DATE(time) = CURDATE() - INTERVAL 2 DAY;";
actions.Log.logInfo("MySQL Cleanup", "Executing MySQL delete command...");
let result = actions.Exec.executeCommandLine(time.Duration.ofSeconds(300), command); // Timeout set to 5 seconds
actions.Log.logInfo("MySQL Cleanup", "Command output: " + result);
},
tags: ["MySQL CleanUp at 1 am"],
id: "clean MySQL"
});
What I’ve Tried
Running the command manually in the terminal →
Works fine
Checking OpenHAB logs (
tail -f /var/log/openhab/openhab.log
) → Command output is null [rg.openhab.core.io.net.exec.ExecUtil] - Failed to execute commandLine '[mysql --batch -B -u MYUSER -p'MYPASSWORD' -D 'MYDATABASE' -e "DELETE FROM 'MYITEM' WHERE DATE(time) = CURDATE() - INTERVAL 2 DAY;"]'
added to exec.whitelist →
Still fails
Using the full path
/usr/bin/mysql
→ Still fails
Questions for the Community
- Why does
actions.Exec.executeCommandLine()
returnnull
when executing MySQL? - Does OpenHAB require any special permissions for MySQL?
- Would it be better to use a different approach (e.g., a cron job outside OpenHAB)?
Any help would be greatly appreciated!
If anyone has successfully run MySQL commands from an OpenHAB rule, I’d love to hear how you did it!