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

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

    If you want to use logger so that the message appears both in the system logs and in some file of yours, you might do

      logger -s your message 2> $HOME/somefile
    

    since the -s option to logger also outputs on stderr which is redirected to the file with 2>

    You could want to use 2>> $HOME/somefile to append (not overwrite) your $HOME/somefile (read about bash redirections), and (see logger(1) for details) you may prefer to pass the program option --id=$$ to logger.

提交回复
热议问题