I run this with a service call at 02:00 each morning and it removes the files I want it to.
You have no idea how long it took me to write this - but it’s a learning curve
Now I want to do more complex tasks: identify, copy and rename files for example and I’m a bit stuck.
I am aware of the 60 second run-time limit, and I’ve read the documentation I can find.
So my questions are:
“Can I use any bash commands in these shell scripts?”
“How can I spread these scripts over multiple lines?”
“Where can I find a good primer for shell scripting that would help me?”
# This command drops Linux buffers/cache and puts memory stats
# before and after the drop into a service response variable
shell_command:
drop_caches: |-
jfree() {
free -m | grep Mem: | cut -d" " -f2- | jq -sc '[ (["total","used","free","shared","buff_cache","available"], . ) ] | transpose | map({(.[0]): .[1]}) | add'
}
before=$(jfree)
sync
echo 1 >/proc/sys/vm/drop_caches
after=$(jfree)
echo "${before}${after}" | jq -c '{before: ., after: input}'
# This script drops caches and shows buffer memory drop
script:
shell_test:
sequence:
- service: shell_command.drop_caches
response_variable: response
- service: persistent_notification.create
data:
message: |
{% set j = response.stdout | from_json %}
{% set bb = j.before.buff_cache %}
{% set ba = j.after.buff_cache %}
Buffers memory dropped from {{bb}} to {{ba}}
The 1st script besides of dropping caches produces the following response which can be captured and used by a caller (actually, it is an object with 3 keys, where the first two are strings):