Write to custom log file from a Bash script

前端 未结 5 606
渐次进展
渐次进展 2021-02-03 18:47

In Linux, I know how to write a simply message to the /var/log/messages file, in a simple shell script I created:

#!/bin/bash
logger \"have fun!\"
<         


        
5条回答
  •  北荒
    北荒 (楼主)
    2021-02-03 19:03

    @chepner make a good point that logger is dedicated to logging messages.

    I do need to mention that @Thomas Haratyk simply inquired why I didn't simply use echo.

    At the time, I didn't know about echo, as I'm learning shell-scripting, but he was right.

    My simple solution is now this:

    #!/bin/bash
    echo "This logs to where I want, but using echo" > /var/log/mycustomlog
    

    The example above will overwrite the file after the >

    So, I can append to that file with this:

    #!/bin/bash
    echo "I will just append to my custom log file" >> /var/log/customlog
    

    Thanks guys!

    • on a side note, it's simply my personal preference to keep my personal logs in /var/log/, but I'm sure there are other good ideas out there. And since I didn't create a daemon, /var/log/ probably isn't the best place for my custom log file. (just saying)

提交回复
热议问题