22

How can I write an entry into /var/log/syslog from the command line?

31

Use the logger command.

logger Some message to write

There are several options available, including:

-i Log the process ID in each line
-f Log the contents of a specified file
-n Write to the specified remote syslog server
-p Specify a priority
-t Tag the line with a specified tag

See man 1 logger for more information on the tool.

9

Alternatively, you can write to syslog from python:

python -c 'import syslog; syslog.syslog("Hello World")'
  • 2
    Honest question: what would be the benefits of using Python for this over the logger command? – Drew Noakes Oct 30 '14 at 11:44
  • 1
    @DrewNoakes If you're running a - python - script from the command line, using the standard library is always better than relying on subprocess and call logger. You already have my vote, I'm just mentioning an alternative. – Sylvain Pineau Oct 30 '14 at 11:51
  • Thanks for the clarification. I guess this is true for all programming languages. Have a vote on me. – Drew Noakes Oct 30 '14 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.