Cannot execute shell scripts: Wrong json syntax?
-
Hi, first of all I love this service. So thank you for building Triggercmd!
Below 2 of my several attempts to triggers 2 scripts (they work if I launch them via SSH or the Rasbian GUI). It seems form the Triggercmd dashboard that the commands are running when triggered, and the reboot and shutdown ones actually do what they are supposed to. However, I have no luck running any shell scripts. I've also moved them from desktop to the root folder, but it didn't help. What can that be? Is it an issue with the syntax?
[ {"trigger":"Reboot","command":"shutdown -r","ground":"background","voice":"reboot","allowParams": "false"}, {"trigger":"shutdown","command":"shutdown now","ground":"background","voice":"shutdown hours","allowParams": "false"}, {"trigger":"Change numbers","command":"\/root\/key_stroke.sh","ground":"background","voice":"change numbers","allowParams": "false"}, {"trigger":"Show hours","command":"\/root\/hours_launcher.sh","ground":"background","voice":"show hours","allowParams": "false"}, ]
[ {"trigger":"Reboot","command":"shutdown -r","ground":"background","voice":"reboot","allowParams": "false"}, {"trigger":"shutdown","command":"shutdown now","ground":"background","voice":"shutdown hours","allowParams": "false"}, {"trigger":"Change numbers","command":"/root/key_stroke.sh","ground":"background","voice":"change numbers","allowParams": "false"}, {"trigger":"Show hours","command":"/root/hours_launcher.sh","ground":"background","voice":"show hours","allowParams": "false"}, ]
-
@riccardo-cereser, the problem might be the comma on the last line. The last one can't have a comma. If it does, the agent will consider the json invalid and restore the previous version.
I tested this one on my Raspberry Pi and it worked for me:
{"trigger":"Change numbers","command":"/root/key_stroke.sh","ground":"background","voice":"change numbers","allowParams": "false"},
This is my /root/key_stroke.sh script:
#!/bin/bash echo Testing >> /tmp/test.log
I see that it worked because my /tmp/test.log file gets another Testing line each time I run it using the Trigger button on the website.
I made my /root/key_stroke.sh script executable using this command:
chmod +x /root/key_stroke.sh
-
@russ Thanks, actually the comma in the end is a typo I’ve added just in the code snippets of the post. I’ve moved the scripts in a different folder, and the problem persist if a place the full path. However I have a new finding: I cannot launch any of those scripts from the terminal unless I enter in the exact folder. If I enter the full path like in your example, I get a message that the file does not exist. If I enter in the folder and simply type the file name, it works.
So I wonder how can I configure the JSON file to navigate to the folder and then run the script? Or anything else I could try ?
-
@riccardo-cereser, please try adding this to your script:
SCRIPTDIR=$(dirname "$0") cd $SCRIPTDIR
That way you can specify the full path to your script in the commands.json file, and when your script runs, it will cd to the directory where your script is.