1. Overview

systemd is a modern system and service manager. It has a logging service named journal that centralizes the management of logs regardless of their origin. The log file is a binary file maintained by the systemd-journald daemon.

We may need to send log messages to the journal from the command line or scripts. In this tutorial, we’ll discuss how to send messages to the journal from the command line.

2. Using systemd-cat

One method for sending messages from the command line to the journal is using the systemd-cat command.

2.1. Using Without Any Options

Let’s run systemd-cat without using any options:

$ systemd-cat

Now, systemd-cat waits for us to enter messages. It sends everything typed in the command line to the journal.

We’ll use the journalctl command for following the logs in the journal. Let’s run journalctl in another terminal:

$ journalctl -f -n 0
-- Logs begin at Fri 2002-04-22 07:51:39 +03. --

We used the -f option of journalctl to follow the new logs appended to the journal. The -n option limits the number of most recent events shown. We passed 0 for this option, so we didn’t see any of the past events.

Now, let’s type messages in the terminal where systemd-cat is waiting for input:

$ systemd-cat
Hello World
Hello Journal

We typed two messages. First, we typed Hello World, and then Hello Journal. So, let’s check the output of journalctl:

$ journalctl -f -n 0
-- Logs begin at Fri 2002-04-22 07:51:39 +03. --
Oct 13 17:46:33 localhost.localdomain cat[379880]: Hello World
Oct 13 17:46:41 localhost.localdomain cat[379880]: Hello Journal

systemd-cat sent both messages to the journal, as expected. The logs included the dates when the messages were sent.

localhost.localdomain is the name of the machine we’re working on.

cat is an identifier used for identifying the logging tool. In our example, it’s cat as systemd-cat sent the messages.

379880 was the PID of the systemd-cat process.

2.2. Executing Commands

We can pass a command directly to systemd-cat:

$ systemd-cat echo "Hello Journal again"
$ echo $?
0

The exit status of running systemd-cat was 0, which means success. Let’s check the output of journalctl again:

$ journalctl -f -n 0
-- Logs begin at Fri 2002-04-22 07:51:39 +03. --
Oct 13 17:46:33 localhost.localdomain cat[379880]: Hello World
Oct 13 17:46:41 localhost.localdomain cat[379880]: Hello Journal
Oct 13 17:51:12 localhost.localdomain unknown[410416]: Hello Journal again

In this case, systemd-cat executed the command echo “Hello Journal again” by connecting the standard output and the standard error of the process to the journal. Therefore, we observed the message Hello Journal again in the journal, as expected.

2.3. Specifying Identifiers

The identifier of the last log message was unknown in the journalctl output. However, we can specify an identifier using the -t option:

$ systemd-cat -t which which ls

A new log message was appended to the journal:

$ journalctl -f -n 0
-- Logs begin at Fri 2002-04-22 07:51:39 +03. --
Oct 13 17:46:33 localhost.localdomain cat[379880]: Hello World
Oct 13 17:46:41 localhost.localdomain cat[379880]: Hello Journal
Oct 13 17:51:12 localhost.localdomain unknown[410416]: Hello Journal again
Oct 13 18:01:01 localhost.localdomain which[411658]: /usr/bin/ls

In this case, we executed the command which ls. The output of it was /usr/bin/ls. Since we used -t which while executing systemd-cat, the identifier of the log message was which instead of unknown.

3. Using logger

Another alternative is the logger command that has been available in Linux for a long time. It also sends messages to the journal.

3.1. Using Without Any Options

First, let’s run journalctl for following the log messages:

$ journalctl -f -n 0
-- Logs begin at Fri 2002-04-22 07:51:39 +03. --

Now, we can use logger for sending messages. We’ll run it without any options. In this case, it sends the messages typed in the command line to the journal:

$ logger

It started waiting for input from the standard input. Let’s enter a message:

$ logger 
A message from logger

We sent the message A message from logger using logger. Let’s check the output of journalctl:

$ journalctl -f -n 0
-- Logs begin at Fri 2002-04-22 07:51:39 +03. –-
Oct 13 18:10:31 localhost.localdomain alice[412233]: A message from logger

The message was appended to the journal, as expected. alice in the log message was the name of the user who sent the message.

3.2. Passing the Messages Directly

We can also pass the message to logger directly:

$ logger Another message from logger
$ echo $?
0

We were successful in sending the message as the exit status was 0. Let’s check the journal:

$ journalctl -f -n 0
-- Logs begin at Fri 2002-04-22 07:51:39 +03. –-
Oct 13 18:10:31 localhost.localdomain alice[412233]: A message from logger
Oct 13 18:13:13 localhost.localdomain alice[414514]: Another message from logger

The message was appended to the journal, as expected.

In addition to passing a message directly to logger, we can also direct the output of a command’s execution to the journal using logger. For instance, let’s send the output of which ls to the journal:

$ which ls | logger

In this example, we directed the output of which ls to the input of logger using a pipe. Here’s the output of journalctl after running this command:

$ journalctl -f -n 0
-- Logs begin at Fri 2002-04-22 07:51:39 +03. –
Oct 13 18:10:31 localhost.localdomain alice[412233]: A message from logger
Oct 13 18:13:13 localhost.localdomain alice[414514]: Another message from logger
Oct 13 18:15:50 localhost.localdomain alice[414700]:        /usr/bin/ls

Therefore, we could append the output of the command to the journal.

3.3. Specifying Tags

We can use the -t option of logger for writing a message with a specific tag. This is similar to the -t option of systemd-cat. A tag in the logger jargon corresponds to an identifier in systemd-cat. If we don’t specify a tag, then the user name is used as the tag, as we saw before. So, let’s try the -t option of logger:

$ logger -t my_tag One more message from logger

The new log message seems to be appended to the journal with the tag my_tag:

$ journalctl -f -n 0
-- Logs begin at Fri 2002-04-22 07:51:39 +03. –
Oct 13 18:10:31 localhost.localdomain alice[412233]: A message from logger
Oct 13 18:13:13 localhost.localdomain alice[414514]: Another message from logger
Oct 13 18:15:50 localhost.localdomain alice[414700]:        /usr/bin/ls
Oct 13 18:20:05 localhost.localdomain my_tag[414924]: One more message from logger

Therefore, we could append the message to the journal using a tag.

4. Conclusion

In this article, we discussed two different methods for sending messages to the journal from the command line.

Firstly, we discussed systemd-cat. This command comes with the new systemd service and system manager. We showed that we can execute commands using systemd-cat. The outputs of the executed commands were written to the journal.

Then, we discussed logger. We showed that this old Linux command can also send messages to the journal.