How can I log to a specific file in linux using logger command?

后端 未结 7 1979
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-07 16:07

I will run the following script:

#!/bin/bash
./myprogram

#get exit code
exitvalue=$?

#log exit code value to /var/log/messages
logger -s \"exit code of my prog         


        
7条回答
  •  忘了有多久
    2021-02-07 17:02

    You can create a small logger function like this on top of your script file:

    #! /bin/bash
    
    #LOG FILE location
    log_file=/tmp/mylogfile.log
    
    #the LOG function
    LOG()
    {
    time=$(date '+%Y-%m-%d %H:%M:%S')
    echo "$time"" >>> "$1 >>${log_file}
    }
    
    
    message= echo "test logger message"
    #to send loggers to your log file use
    LOG "my message logged to my log file with timestamp = ""$message" 
    

    check output :

    head -1 /tmp/mylogfile.log
    2019-09-16 14:17:46 >>> my message logged to my log file with timestamp = test logger message
    

提交回复
热议问题