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



  • 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.




  • Galactic Empire

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



  • 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.


  • Galactic Empire

    @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.


  • LAYER 8 Netgate

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

    system.log just being used as an example.



  • 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.


  • Rebel Alliance Developer Netgate

    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
    

Log in to reply