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

后端 未结 7 1990
佛祖请我去吃肉
佛祖请我去吃肉 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 16:53

    I think your better choice would be to use the date command rather then logger in cases where you don't want to write to the syslog files (and don't have privs to do so).

    See "timestamp before an echo" for details on how to use date to prefix a message with a date and write it to a file.

    You create a bash function that looks like the following, adjusting the date format string to get what you want:

    echo_time() {
        echo `date +'%b %e %R '` "$@"
    }
    

    In your bash script, you would then use:

    echo_time "Your message here" >> ${LOGFILE}
    

    Which would put the following in your ${LOGFILE} file:

    Mar 11 08:40 your message here
    

提交回复
热议问题