List of selectable units for journalctl

grepsystemd

In a CentOS 7 server, I want to get the list of selectable units for which journalctl can produce logs. How can I change the following code to accomplish this?

journalctl --output=json-pretty | grep -f UNIT | sort -u  

In the CentOS 7 terminal, the above code produces grep: UNIT: No such file or directory.

EDIT:

The following java program is terminating without printing any output from the desired grep. How can I change things so that the java program works in addition to the terminal version?

    String s;
    Process p;
    String[] cmd = {"journalctl --output=json-pretty ","grep UNIT ","sort -u"};
    try {
        p = Runtime.getRuntime().exec(cmd);
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        while ((s = br.readLine()) != null)
            System.out.println("line: " + s);
        p.waitFor();
        System.out.println ("exit: " + p.exitValue()+", "+p.getErrorStream());
        BufferedReader br2 = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        while ((s = br2.readLine()) != null)
            System.out.println("error line: " + s);
        p.waitFor();
        p.destroy();
    } catch (Exception e) {}  

Best Answer

journalctl can display logs for all units - whether these units write to the log is a different matter.

To list all available units and therefore all available for journalctl to use:

systemctl list-unit-files --all

As to your java code, in order to make pipes work with Runtime.exec() you could either put the command in a script and invoke the script or use a string array, something like:

String[] cmd = {"sh", "-c", "command1 | command2 | command3"};
p = Runtime.getRuntime().exec(cmd);

or:

Runtime.getRuntime().exec(new String[]{"sh", "-c", "command1 | command2 | command3"});
Related Question