add some more useful background examples

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2019-04-29 18:50:36 +02:00
parent 3dfa5e44be
commit 7ce2c5be2c
9 changed files with 254 additions and 33 deletions

View File

@ -0,0 +1,105 @@
#!/bin/bash
# files: mycommands.sh.dist
# copy to mycommands.sh and add all your commands an functions here ...
export res
# your additional bahsbot commands ...
mycommands() {
case "$MESSAGE" in
'/run_'*)
myback="run_${MESSAGE#*_}"
if [ -x "./$myback.sh" ]; then
checkback "$myback"
if [ "$res" -gt 0 ] ; then
send_normal_message "${CHAT[ID]}" "Starte $myback"
background "./$myback.sh" "$myback"
else
send_normal_message "${CHAT[ID]}" "Prozess $myback laeuft bereits."
fi
fi
;;
'/kill_'*)
myback="run_${MESSAGE#*_}"
if [ -x "./$myback.sh" ]; then
checkback "$myback"
if [ "$res" -eq 0 ] ; then
killback "$myback"
send_normal_message "${CHAT[ID]}" "Beende $myback."
else
send_normal_message "${CHAT[ID]}" "Prozess $myback laeuft nicht."
fi
fi
;;
esac
}
# place your additional processing functions here ...
# returns true if function exist
_is_function()
{
[ "$(LC_ALL=C type -t "$1")" = "function" ]
}
# inifnite loop for waching a given dir for new files
# $1 dir to wtach for new files
watch_dir_loop() {
local newfile old
# wait for new files in WATCHDIR
inotifywait -q -m "$1" -e create --format "%f" \
| while true
do
# read in newfile
read -r newfile
#skip if not match or same name as last time
[ "${newfile}" = "${old}" ] && continue
sleep 0.2
# process content and output message
echo "$(date) found ${newfile}" >&2
# note: loop callback must a function in the calling script!
if _is_function loop_callback ; then
loop_callback "$1/$newfile"
else
echo "ERROR: loop_callback not found!" >&2
fi
done
} # 2>>"$0.log"
output_telegram() {
# output to telegram
sed <<< "${1}" -e ':a;N;$!ba;s/\n/ mynewlinestartshere /g'
} # 2>>"$0.log"
# name and localtion of the tml file
# $1 string to output
# $2 file to add file to
output_html_file() {
local date
date="$(date)"
output_file "$(sed <<< "<div class=\"newdeal\">$1 <br>${date}</div>" '
s/ my[a-z]\{3,15}\(start\|ends\)here.*<br>/<br>/g
s/ *mynewlinestartshere */<br>/
s/\n/<br>/
')"
} # >>"$0.log" 2>&1
# $1 string to output
# $2 file to add file to
output_file() {
local publish="${2}"
[ ! -w "${publish}" ] && echo "ERROR: file ${publish} is not writeable or does not exist!" && exit
# output at beginnung of file, add date to message
sed <<< "${1}" '
s/ *mynewlinestartshere */\n/
s/ my[a-z]\{3,15}\(start\|ends\)here.*//g
' >"$publish$$"
cat "$publish" >>"$publish$$"
mv "${publish}$$" "${publish}"
} # >>"$0.log" 2>&1

View File

@ -0,0 +1,42 @@
#!/bin/bash
# file: run_diskcusage.sh
# example for an background job display a system value
# This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
#### $$VERSION$$ v0.70-pre1-11-g3dfa5e4
# adjust your language setting here
# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment
export 'LC_ALL=C.UTF-8'
export 'LANG=C.UTF-8'
export 'LANGUAGE=C.UTF-8'
unset IFS
# set -f # if you are paranoid use set -f to disable globbing
# discard STDIN for background jobs!
cat >/dev/null &
source "./mycommands.sh"
# check if $1 is a number
re='^[0-9]+$'
if [[ $1 =~ $re ]] ; then
SLEEP="$1"
else
SLEEP=100 # time between time notifications
fi
NEWLINE=$'\n'
# output disk usgae every $1 seconds
WAIT=0
while sleep $WAIT
do
output_telegram "Current Disk usage ${NEWLINE} $(df -h / /tmp /usr /var /home)"
# only for testing, delete echo line for production ...
echo "Current Disk usage ${NEWLINE} $(df -h / /tmp /usr /var /home)"
WAIT="$SLEEP"
done

View File

@ -0,0 +1,32 @@
#!/bin/bash
# file: run_filename
# background job to display content of all new files in WATCHDIR
#
# adjust your language setting here
# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment
export 'LC_ALL=C.UTF-8'
export 'LANG=C.UTF-8'
export 'LANGUAGE=C.UTF-8'
unset IFS
# set -f # if you are paranoid use set -f to disable globbing
# discard STDIN for background jobs!
cat >/dev/null &
# watch for new logfiles
WATCHDIR="/my_special/dir_to_watch"
source "./mycommands.sh"
# test your script and the remove ...
WATCHDIR="/tmp"
# this is calles by watch loop
# $1 is name of the new file
loop_callback() {
# output content of file, you MUST trust creator of the file because it contest are sent as message to you!
output_telegram "Contents of ${1}: mynewlinestartshere $(cat "${1}")"
}
watch_dir_loop "$WATCHDIR"

View File

@ -0,0 +1,32 @@
#!/bin/bash
# file: run_filename
# background job to display all new files in WATCHDIR
#
# adjust your language setting here
# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment
export 'LC_ALL=C.UTF-8'
export 'LANG=C.UTF-8'
export 'LANGUAGE=C.UTF-8'
unset IFS
# set -f # if you are paranoid use set -f to disable globbing
# discard STDIN for background jobs!
cat >/dev/null &
# watch for new logfiles
WATCHDIR="/var/log"
source "./mycommands.sh"
# test your script and the remove ...
WATCHDIR="/tmp"
# this is calles by watch loop
# $1 is name of the new file
loop_callback() {
# output one simple line ...
echo "New file ${1} created in ${WATCHDIR}!"
}
watch_dir_loop "$WATCHDIR"

View File

@ -0,0 +1,34 @@
#!/bin/bash
# file: notify.sh
# example for an background job, run with startback notify.sh
# This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
#### $$VERSION$$ v0.70-pre1-11-g3dfa5e4
# adjust your language setting here
# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment
export 'LC_ALL=C.UTF-8'
export 'LANG=C.UTF-8'
export 'LANGUAGE=C.UTF-8'
unset IFS
# set -f # if you are paranoid use set -f to disable globbing
# discard STDIN for background jobs!
cat >/dev/null &
# check if $1 is a number
re='^[0-9]+$'
if [[ $1 =~ $re ]] ; then
SLEEP="$1"
else
SLEEP=10 # time between time notifications
fi
# output current time every $1 seconds
while sleep $SLEEP
do
date "+* It's %k:%M:%S o' clock ..."
done

View File

@ -1,9 +1,11 @@
#!/bin/bash
# file: calc.sh
# example for an interactive chat, run with startprog calc.sh
# This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
#### $$VERSION$$ v0.70-pre1-0-g490c472
#### $$VERSION$$ v0.70-pre1-11-g3dfa5e4
# adjust your language setting here
# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment

View File

@ -1,8 +1,10 @@
#!/bin/bash
# file: notify.sh
# example for an background job, run with startback notify.sh
# This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
#### $$VERSION$$ v0.70-pre1-0-g490c472
#### $$VERSION$$ v0.70-pre1-11-g3dfa5e4
# adjust your language setting here
# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment

View File

@ -1,30 +0,0 @@
#!/bin/bash
# This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
#### $$VERSION$$ v0.70-pre1-0-g490c472
# adjust your language setting here
# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment
export 'LC_ALL=C.UTF-8'
export 'LANG=C.UTF-8'
export 'LANGUAGE=C.UTF-8'
unset IFS
# set -f # if you are paranoid use set -f to disable globbing
echo "Why hello there.
Would you like some tea (y/n)?"
read -r answer
[[ $answer =~ ^([yY][eE][sS]|[yY])$ ]] && echo "OK then, here you go: http://www.rivertea.com/blog/wp-content/uploads/2013/12/Green-Tea.jpg" || echo "OK then."
until [ "$SUCCESS" = "y" ] ;do
echo 'Do you like Music? mykeyboardstartshere "Yass!" , "No"'
read -r answer
case $answer in
'Yass!') echo "Goody! mykeyboardendshere";SUCCESS=y;;
'No') echo "Well that's weird. mykeyboardendshere";SUCCESS=y;;
*) SUCCESS=n;;
esac
done
exit

View File

@ -1,9 +1,11 @@
#!/bin/bash
# file: question.sh
# example for an interactive chat, run with startprog question.sh
# This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
#### $$VERSION$$ v0.70-pre1-0-g490c472
#### $$VERSION$$ v0.70-pre1-11-g3dfa5e4
# adjust your language setting here
# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment