Netgate Discussion Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Search
    • Register
    • Login

    [SOLVED] What is the correct way to write to the log file from a shell script?

    Scheduled Pinned Locked Moved Development
    8 Posts 5 Posters 2.1k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • G
      guardian Rebel Alliance
      last edited by guardian

      I'm doing a bit of custom scripting for maintenance functions on the network, and I want to write some info to the system log. What is the correct way to write to the sytem log from a shell script?

      Thanks in advance for any info/hints/etc.

      If you find my post useful, please give it a thumbs up!
      pfSense 2.7.2-RELEASE

      1 Reply Last reply Reply Quote 0
      • K
        kpa
        last edited by

        https://www.freebsd.org/cgi/man.cgi?logger(1)

        1 Reply Last reply Reply Quote 1
        • NogBadTheBadN
          NogBadTheBad
          last edited by

          Don't think logger writes to the firewall logs, it will however write to a remote syslog server.

          Andy

          1 x Netgate SG-4860 - 3 x Linksys LGS308P - 1 x Aruba InstantOn AP22

          1 Reply Last reply Reply Quote 0
          • K
            kpa
            last edited by

            No no, logger is the standard command line frontend for the whole syslog system. Depending on how you set up the system or how you use logger (command line options) the logs can be sent to a remote server however.

            NogBadTheBadN 1 Reply Last reply Reply Quote 1
            • NogBadTheBadN
              NogBadTheBad @kpa
              last edited by NogBadTheBad

              @kpa said in What is the correct way to write to the log file from a shell script?:

              No no, logger is the standard command line frontend for the whole syslog system. Depending on how you set up the system or how you use logger (command line options) the logs can be sent to a remote server however.

              Quite correct :)

              I tried it with -h 127.0.0.1 thinking you need to define the host to send to and it didn't work, without the -h option it does.

              Andy

              1 x Netgate SG-4860 - 3 x Linksys LGS308P - 1 x Aruba InstantOn AP22

              1 Reply Last reply Reply Quote 0
              • DerelictD
                Derelict LAYER 8 Netgate
                last edited by

                clog /var/log/system.log > /tmp/system.log.txt

                system.log just being used as an example.

                Chattanooga, Tennessee, USA
                A comprehensive network diagram is worth 10,000 words and 15 conference calls.
                DO NOT set a source address/port in a port forward or firewall rule unless you KNOW you need it!
                Do Not Chat For Help! NO_WAN_EGRESS(TM)

                1 Reply Last reply Reply Quote 0
                • G
                  guardian Rebel Alliance
                  last edited by guardian

                  Thanks all for the replies @kpa, @NogBadTheBad, @Derelict

                  That was easy-writing to /var/log/system.log requires no options and will work from any account.

                  For the benefit of then next person looking a couple of noob friendly examples/helpful hints:

                  A simple log message requires no parameters other than the message itself:

                  # logger "*TEST* logger test"
                  # #Note: clog is necessary to view messages
                  # clog /var/log/system.log | grep '*TEST*'
                  

                  Jun 27 11:51:23 hostname root: TEST logger test

                  Messages are tagged with the original login name - messages after su custom are still tagged as root:
                  Jun 27 11:58:53 hostname root: TEST from user su custom
                  Jun 27 12:14:36 hostname custom: TEST login from custom instead of su

                  Log messages can be tagged with a custom tag instead of the userid and optionally include the pid of the process making the entry with -i

                  $ logger -i -t IDENTIFIER "*TEST* from User custom with -i -t"
                  

                  Jun 27 12:05:11 hostname IDENTIFIER[7748]: TEST from User custom with -i -t

                  For remote logging or other more complicated use cases I'll say RTFM :)
                  `
                  NB: hostname is the hostname of the pfSense, and has been altered for purposes of these examples.
                  The asterisks either side of TEST wiere stipped by the board, and appeared in the actual output.

                  If you find my post useful, please give it a thumbs up!
                  pfSense 2.7.2-RELEASE

                  1 Reply Last reply Reply Quote 0
                  • jimpJ
                    jimp Rebel Alliance Developer Netgate
                    last edited by

                    You can also pipe to logger for logging command output:

                    $ echo BLAH | logger
                    $ clog /var/log/system.log | grep TEST
                    Jun 28 11:01:51 clara root: TEST
                    

                    Remember: Upvote with the 👍 button for any user/post you find to be helpful, informative, or deserving of recognition!

                    Need help fast? Netgate Global Support!

                    Do not Chat/PM for help!

                    1 Reply Last reply Reply Quote 1
                    • First post
                      Last post
                    Copyright 2025 Rubicon Communications LLC (Netgate). All rights reserved.