April 10, 201511 yr How do I tell a user created .sh script to write messages to unRAID's Syslog? For example (would something like this?): echo "message" >> location/unraid/syslog
April 10, 201511 yr I've studied this long and hard. There's a way to do it so all stdout/stderr messages get logged via a coprocess. With the prefix code, the name of the script is saved and use as the logger tag. If you are NOT on a tty, the output is fired off to a co-process, otherwise it is to the current tty. This allows you to debug and run interactively. if not on a tty A logger coprocess is fired off with the pid (so you can identify more then one running). stdout and stderr are saved. stdout/stderr is redirected to the co-process do your work. at the end redirect stdout/stderr to the old stdout/stderr and end the co-process #!/bin/bash [ ${DEBUG:=0} -gt 0 ] && set -x -v P=${0##*/} # basename of program R=${0%%/$P} # dirname of program P=${P%.*} # strip off after last . character TMPFILE=/var/log/${P}.$$ # COPROC[0] is connected to the standard output of the co-process # COPROC[1] is connected to the standard input of the co-process. if ! tty > /dev/null then coproc /usr/bin/logger -t${P}[$$] # Redirect stdout/stderr to logger exec 3>&1 4>&2 # Save stdout/stderr to 3/4 respectively eval "exec 1>&${COPROC[1]} 2>&${COPROC[1]} ${COPROC[0]}>&-" fi then at the end if ! tty > /dev/null ; then exec 1>&3 2>&4 # Restore stdout/stderr, exec 3>&- 4>&- # close 3/4 eval "exec ${COPROC[1]}<&-" # Close stdin of coprocess # This should cause output to via old stdout(cron) and get mailed fi and a full testing example. #!/bin/bash [ ${DEBUG:=0} -gt 0 ] && set -x -v P=${0##*/} # basename of program R=${0%%/$P} # dirname of program P=${P%.*} # strip off after last . character TMPFILE=/var/log/${P}.$$ PIDFILE=/var/run/${P}.pid SECONDS=0 if [ -z "${1}" ] then echo "Usage: $0 (arguments) " echo exit fi # COPROC[0] is connected to the standard output of the co-process # COPROC[1] is connected to the standard input of the co-process. if ! tty > /dev/null then coproc /usr/bin/logger -t${P}[$$] # Redirect stdout/stderr to logger exec 3>&1 4>&2 # Save stdout/stderr to 3/4 respectively eval "exec 1>&${COPROC[1]} 2>&${COPROC[1]} ${COPROC[0]}>&-" fi if [ -e "${PIDFILE}" ]; then PID=$( < ${PIDFILE} ) echo "$0: Already running (Pid=${PID}). exiting." ps -fp ${PID} exit fi echo $$ > ${PIDFILE} trap "[ -f ${PIDFILE} ] && rm -f ${PIDFILE} ${TMPFILE}; exit " HUP INT QUIT TERM trap "rm -f ${PIDFILE} ${TMPFILE}" EXIT echo "START `date`" sleep 3 echo "END `date` Duration: (${SECONDS})" # echo "TEST `date` fd:1" >&1 # echo "TEST `date` fd:2" >&2 # echo "TEST `date` fd:3" >&3 # echo "TEST `date` fd:4" >&4 if ! tty > /dev/null ; then exec 1>&3 2>&4 # Restore stdout/stderr, exec 3>&- 4>&- # close 3/4 eval "exec ${COPROC[1]}<&-" # Close stdin of coprocess # echo "STDOUT `date`" # echo "TEST `date` fd:1" >&1 # echo "TEST `date` fd:2" >&2 # echo "TEST `date` fd:3" >&3 # echo "TEST `date` fd:4" >&4 # echo "STDOUT `date`" # ls -ld /tmp/x.y.z # This should cause output to via old stdout(cron) and get mailed egrep " ${P}\[$$\]: " /var/log/syslog fi rm -f ${TMPFILE} ${PIDFILE}
April 10, 201511 yr This is how I use it for emhttp events Edit as needed. root@unRAIDb:/boot/local/bin# more /usr/local/emhttp/plugins/rsyncServer/event/* :::::::::::::: /usr/local/emhttp/plugins/rsyncServer/event/disks_mounted :::::::::::::: #!/bin/bash [ ${DEBUG:=0} -gt 0 ] && set -x -v P=${0##*/} # basename of program R=${0%%/$P} # dirname of program P=${P%.*} # strip off after last . character exec 3>&1 4>&2 # Save stdout/stderr to 3/4 respectively coproc /usr/bin/logger -t${0}[$$] eval "exec 1>&${COPROC[1]} 2>&1 ${COPROC[0]}>&-" if [ -x /etc/rc.d/rc.rsyncd ]; then echo "Starting /etc/rc.d/rc.rsyncd start" /etc/rc.d/rc.rsyncd start fi exec 1>&3 2>&4 # restore stdout/stderr from 3/4 respectively eval "exec ${COPROC[1]}>&-" :::::::::::::: /usr/local/emhttp/plugins/rsyncServer/event/unmounting_disks :::::::::::::: #!/bin/bash [ ${DEBUG:=0} -gt 0 ] && set -x -v P=${0##*/} # basename of program R=${0%%/$P} # dirname of program P=${P%.*} # strip off after last . character exec 3>&1 4>&2 # Save stdout/stderr to 3/4 respectively coproc /usr/bin/logger -t${0}[$$] eval "exec 1>&${COPROC[1]} 2>&1 ${COPROC[0]}>&-" if [ -x /etc/rc.d/rc.rsyncd ]; then echo "Starting /etc/rc.d/rc.rsyncd stop" /etc/rc.d/rc.rsyncd stop fi exec 1>&3 2>&4 # restore stdout/stderr from 3/4 respectively eval "exec ${COPROC[1]}>&-"
April 10, 201511 yr How do I tell a user created .sh script to write messages to unRAID's Syslog? For example (would something like this?): echo "message" >> location/unraid/syslog using snippets for just single messages #!/bin/bash [ ${DEBUG:=0} -gt 0 ] && set -x -v P=${0##*/} # basename of program R=${0%%/$P} # dirname of program P=${P%.*} # strip off after last . character # echo to syslog with your program/shell name and pid. logger -t${P}[$$] -puser.info "your message line" # cat a file to syslog cat (somefileoutput) | logger -t${P}[$$] -puser.info # or alternate form without co-process logger -t${P}[$$] -puser.info < somefile Note you can also fire off a coprocess on purpose and echo to that process FD with #!/bin/bash [ ${DEBUG:=0} -gt 0 ] && set -x -v P=${0##*/} # basename of program R=${0%%/$P} # dirname of program P=${P%.*} # strip off after last . character coproc /usr/bin/logger -t${P}[$$] echo "some output" > &${COPROC[1]} ls -l > &${COPROC[1]}
April 10, 201511 yr Author WOW. thanks for the detailed responses! I will have to read through this more closely after work and try and digest it. My eyes just popped out Before reading your posts I tried this echo "test" >> /var/log/syslog and it worked but didn't display all the nice time stamps and stuff at the front of the message
April 10, 201511 yr You can use the simpler example as logger as long as you take apart the process(script name) as in the examples. P=${0##*/} # basename of program R=${0%%/$P} # dirname of program P=${P%.*} # strip off after last . character logger -t${P}[$$] "message" You can even alias it with something like alias syslog="logger -t${P}[$$] -puser.info" so then you can do syslog "your message" alias only works interactively, if this is going to be run in a background/cron job, then you have to use a shell function as in syslog() { logger -t${P}[$$] -puser.info" $@ }
April 10, 201511 yr Author THANKS! exactly what I was looking for. logger -tvm-startup[$$] "message" = Apr 10 12:30:25 Pithos vm-startup[32274]: message
Archived
This topic is now archived and is closed to further replies.