stout, stderr to syslog via function with if statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting stout, stderr to syslog via function with if statement
# 1  
Old 09-04-2010
stout, stderr to syslog via function with if statement

I have a function called sysLogger in a bash script that I am using to redirect stdout and stderr to syslog.

Part of the script contains an option to turn on debugging otherwise I want debugging output to go to /dev/null.

I am struggling to understand how to make this work using the function I have created to take stderr and stdout and redirect it to syslog. Sending output to syslog works fine (although I am seeing file descriptor left open errors, not sure if they are a problem). But if I add an "if" statement to try and redirect any output to /dev/null depending on whether debugging is enabled I get "broken pipe" errors.

I have tried using "tee" and various other options to just redirect to /dev/null but none worked and googling about process substitution and file descriptors has made more confused than anything.

The full script and config file is attached so you can see what I am trying to do, but here is the important bits.

Function:
Code:
####### logging
sysLogger () {
        local logPriority=$1
        local logMessage=$2
        case "${logPriority}" in

                critical)       logger -p local1.notice -t mugsyback.crit ${logMessage}
                ;;  
                warning)        logger -p local1.notice -t mugsyback.warn ${logMessage}
                ;;  
                info)           logger -p local1.notice -t mugsyback.info ${logMessage}
                ;;  
                debug)          if "${logLevel}" = "debug" ; then
                                        logger -p local1.notice -t mugsyback.debug ${logMessage}
                                        else > /dev/null
                                fi  
                ;;  
                *)              echo "logging option does not exist"
                ;;  
        esac
}

Once example of output being redirected (altered for purposes of this post)
Code:
mount -v /mnt/backup 2> >(sysLogger "critical") 1> >(sysLogger "debug") || { backupResult=1 ; cleanup ; }

I have the following in /etc/rsyslog.d/mugsyback.conf

Code:
cat /etc/rsyslog.d/mugsyback.conf 
# - mugsyback logging -
local1.notice                   /var/log/mugsyback.log

thanks
# 2  
Old 09-05-2010
The function must be:
Code:
####### logging
sysLogger () {
        local logPriority=$1
        local logMessage=$2
        case "${logPriority}" in
 
                critical)       logger -p local1.notice -t mugsyback.crit ${logMessage}
                ;;  
                warning)        logger -p local1.notice -t mugsyback.warn ${logMessage}
                ;;  
                info)           logger -p local1.notice -t mugsyback.info ${logMessage}
                ;;  
                debug)          if "${logLevel}" = "debug" ; 
                                     then
                                        logger -p local1.notice -t mugsyback.debug ${logMessage}
                                     else 
                                        logger -p local1.notice -t mugsyback.debug /dev/null
                                 fi  
                ;;  
                *)              echo "logging option does not exist"
                ;;  
        esac
}

# 3  
Old 09-05-2010
Edit: Sincere apologies, I have just realised I had not added "test" to the syntax. of the if statement. I have altered this post accordingly

Thanks, but it did not work for me.

I have created the following as a test script to simplify things.

I should also point out that the variable $logMessage is only used for manual entries into syslog otherwise the output from stderr and stdout is piped "into" logger.

e.g

Code:
sysLogger "info" "this text is added to syslog"

Code:
#!/bin/bash -x

logLevel=

####### logging
sysLogger () {
        local logPriority=$1
        local logMessage=$2
        case "${logPriority}" in

                critical)       logger -t mugsyback.crit ${logMessage}
                ;;  
                warning)        logger -t mugsyback.warn ${logMessage}
                ;; 
                info)           logger -t mugsyback.info ${logMessage}
                ;;  
                debug)	      	if test "${logLevel}" = "debug";
					then
						logger -t mugsyback.debug ${logMessage}
					else
						logger -t mugsyback.debug /dev/null
				fi
                ;;  
                *)              echo "logging option does not exist"
                ;;  
        esac
}

mount -v /mnt/backup 2> >(sysLogger "critical") 1> >(sysLogger "debug") || { echo "failed" ; exit 1 ; }

I get the following output (with bash -x)

Code:
[/C+ logLevel=
+ mount -v /mnt/backup
++ sysLogger critical
++ local logPriority=critical
++ local logMessage=
++ case "${logPriority}" in
++ logger -t mugsyback.crit
+ echo failed
failed
+ exit 1

And the following in /var/log/syslog

Code:
Sep  5 16:04:08 debvelopment mugsyback.crit: ++ sysLogger debug
Sep  5 16:04:08 debvelopment mugsyback.crit: ++ local logPriority=debug
Sep  5 16:04:08 debvelopment mugsyback.crit: ++ local logMessage=
Sep  5 16:04:08 debvelopment mugsyback.crit: ++ case "${logPriority}" in
Sep  5 16:04:08 debvelopment mugsyback.crit: ++ test '' = debug
Sep  5 16:04:08 debvelopment mugsyback.crit: ++ logger -t mugsyback.debug /dev/null
Sep  5 16:04:08 debvelopment mugsyback.debug: /dev/null
Sep  5 16:04:08 debvelopment mugsyback.crit: Broken pipe


Last edited by jelloir; 09-05-2010 at 03:15 AM..
# 4  
Old 09-05-2010
If I undestand what you wants, it must be:
Function:
Code:
####### logging
sysLogger () {
        local logPriority=$1
        local logMessage=$2
        case "${logPriority}" in
 
                critical)       logger -p local1.notice -t mugsyback.crit ${logMessage}
                ;;  
                warning)        logger -p local1.notice -t mugsyback.warn ${logMessage}
                ;;  
                info)           logger -p local1.notice -t mugsyback.info ${logMessage}
                ;;  
                debug)          if test "${logLevel}" = "debug" ; then
                                        logger -p local1.notice -t mugsyback.debug ${logMessage}
                                fi  
                ;;  
                *)              echo "logging option does not exist"
                ;;  
        esac
}

# 5  
Old 09-05-2010
Quote:
Originally Posted by john1212
If I undestand what you wants, it must be:
Function:
Code:
####### logging
sysLogger () {
        local logPriority=$1
        local logMessage=$2
        case "${logPriority}" in
 
                critical)       logger -p local1.notice -t mugsyback.crit ${logMessage}
                ;;  
                warning)        logger -p local1.notice -t mugsyback.warn ${logMessage}
                ;;  
                info)           logger -p local1.notice -t mugsyback.info ${logMessage}
                ;;  
                debug)          if test "${logLevel}" = "debug" ; then
                                        logger -p local1.notice -t mugsyback.debug ${logMessage}
                                fi  
                ;;  
                *)              echo "logging option does not exist"
                ;;  
        esac
}

Thanks again john1212, I had actually tried that already but it still fails with a pipe error.

I need something that handles the output like logger does but allows me to redirect it to /dev/null.
# 6  
Old 09-05-2010
please send /var/log/syslog
# 7  
Old 09-06-2010
Quote:
Originally Posted by john1212
please send /var/log/syslog
Code:
Sep  6 08:44:52 debvelopment mugsyback.crit: Broken pipe

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

A single statement without main function in c

A sample.c file is written with only one single statement. main; Segmentation fault occurred when executed that file. Any statement other than main; is written, for example unix; then it won't compile. why is this behaviour ! (2 Replies)
Discussion started by: techmonk
2 Replies

2. Shell Programming and Scripting

Ceil not working as function in awk statement

Hi, I have the following code in which i am trying to find ceil of 10th & 11th fields. For finding ceil i have a function in the awk statement. When i test it for some values say on command line it gives correct response(say $10=0 & $11=750). But when the same value occurs in a file having more 3... (5 Replies)
Discussion started by: siramitsharma
5 Replies

3. Shell Programming and Scripting

Running a function from a case statement

Hi, I have the below script that should take the command line option and run the desired script on another server. Only it doesn't seem to run the function, infact it just returns back to the command line. case $1 in 1) msgbacklog() ;; 2) jobstatus() ;; ... (10 Replies)
Discussion started by: chris01010
10 Replies

4. UNIX for Dummies Questions & Answers

Simplest way to format with If on stout?

So I'm trying to figure out a way to do some very simple formatting on standard output. I have a command that I will run (many many times) the output will either be true or false. So all i really want is to run the command and if its true write true in green and if its false to write false in red.... (10 Replies)
Discussion started by: MrEddy
10 Replies

5. Shell Programming and Scripting

Embed function in if statement

Hey everyone, I am just trying to figure out how to embed a function in an if statement. I have the following test script so far: PRIMARY=192.168.1.2 SECONDARY=192.168.1.1 function checkAlive { ping -c 1 -q $1 } if then echo "equaled 0" fi This... (1 Reply)
Discussion started by: msarro
1 Replies

6. Shell Programming and Scripting

Calling function in awk statement.

Hi All, I have an awk statement and a function defined in a script. I am trying to call the function from inside awk statement, i.e. awk ' myFunk () ;' filename But when I define myFunk() before awk, then I receive this error: s2.sh: line 48: syntax error: unexpected end of file and... (5 Replies)
Discussion started by: morningSunshine
5 Replies

7. Shell Programming and Scripting

read statement not working in a function

Pls this is emergency.I have written a script which is taking input from another script. and the contents of my second script are acting as functions to my main script.Now the problem is that in one of the functions i want the script ececution to stop and start when user enters any character r... (2 Replies)
Discussion started by: sumitdua
2 Replies

8. Shell Programming and Scripting

Using Subroutine / Function in case statement

I have issue running functions under case statement #!/bin/bash single() { Commands } multiple() { Commands } until ; do echo -e " \t \t M A I N - M E N U Perforce delete script \n" (1 Reply)
Discussion started by: sriram003
1 Replies

9. UNIX for Dummies Questions & Answers

How can I find files by date or size from stout?

Hello all I wander if I make for example " ls -l " And it gives me all the files in the directory with the additional info like data size and privileges But what if I like to filter the stout result for example by date When I try to do: echo "`ls -l`" | grep "Jan 12" it gives me the... (2 Replies)
Discussion started by: umen
2 Replies

10. UNIX for Advanced & Expert Users

sending syslog output to stderr or stdout

Is there a way to send the syslog output for a given facility to stderr or stdout? I do not want to use the "tail" command to achieve this, I would like it to go directly to stderr. Thanks in advance (1 Reply)
Discussion started by: dmirza
1 Replies
Login or Register to Ask a Question