1. Overview

In the dynamic realm of Linux system administration, it’s common to encounter processes with commands that go beyond the terminal screen. In fact, this can pose a challenge when trying to glean the full context of a particular process.

For example, the default behavior of the ps command, which is the linchpin for process inspection, truncates long commands, leaving administrators and users with a potentially incomplete picture.

In this tutorial, we’ll delve into various strategies and techniques to effectively circumvent the truncation of command lines in the output of ps, thereby offering a clear view of the complete command associated with any given process.

2. Understanding the Problem

To set the stage, let’s begin with a scenario: a process, denoted by a given process ID (PID), is executing a long command with many arguments.

Let’s use the standard ps command to inspect this process by specifying its PID after the -p option:

$ ps -p 12345
USER PID TTY STAT TIME COMMAND
user 12345   S  09:00 0:00 /usr/java/jdk1.6.0_37/bin/java -Xms64m -Xmx512m...

In the above example, we notice that the command /usr/java/jdk1.6.0_37/bin/java -Xms64m -Xmx512m is truncated. So, this leaves us with an incomplete view of the process execution environment. Furthermore, this truncation makes it difficult to fully understand what the process is doing.

In the following sections, we’ll highlight some methods to overcome this problem and avoid the truncation of long commands in the output of the ps command.

3. Using the ww Option of ps

Normally, the output in the command field of ps is truncated because it requires more columns to be displayed than the default on most terminals. To be clear, the default number of columns is 80.

Before providing real-life examples, let’s first understand the functionality of the w option. In essence, w uses 132 columns to display the same information that ps usually tries to fit within 80. However, if we use ww instead, ps is free to use as many columns as necessary.

Let’s check an example with additional options:

$ ps auxww | grep "12345"
USER  PID %CPU %MEM  TTY  STAT TIME COMMAND
user 12345 0   0  108472   1896  06:03  /usr/java/jdk1.6.0_37/bin/java -Xms64m -Xmx512m -Dflume.monitoring.type=GANGLIA -Dflume.monitoring.hosts=prod.hostname.ru:8649 -cp /etc...

As we can see, the output isn’t truncated after using the ww option.

Next, let’s break down the code snippet above:

  1. aux is a set of combined options, where a shows information about all processes, u provides detailed information, and x displays information about processes without a controlling terminal
  2. ww ensures no truncation is imposed
  3. the | symbol pipes the output
  4. grep selects the lines that match the PID of 12345

Alternatively, we can use the -p option with the ps command to specify the PID as we did in the previous section.

4. Using xargs

xargs is a powerful and versatile command-line utility in Linux and Unix-like operating systems.

4.1. Theoretical Understanding

xargs is designed to read items from standard input (stdin) and execute a command with those items as arguments to the supplied command. Particularly, this makes xargs useful for performing operations on a large number of files, directories, or other inputs.

xargs can read items from stdin or a file. By default, it separates each item with spaces, tabs, or newlines. Additionally, xargs assembles the input items into arguments with the appropriate separators and passes them to the specified command.

Critically, if there are too many items to fit into a single command line, xargs splits them and executes the command multiple times if necessary.

4.2. Avoiding Line Truncation Using xargs

Linux provides a virtual filesystem located at /proc that contains information about processes, organized by PID. The cmdline file within the directory of each process holds the complete command line for a process.

After seeing how xargs works in theory, let’s apply it to our case:

$ xargs -0 printf '%s\n' < /proc/12345/cmdline
/usr/java/jdk1.6.0_37/bin/java
-Xms64m
-Xmx512m
-Dflume.monitoring.type=GANGLIA
-Dflume.monitoring.hosts=prod.hostname.ru:8649
-cp
/etc/flume-ng/conf/acrs-event:/usr/lib/flume-ng/lib/*:/etc..

Now, let’s explore the code snippet:

  1. /proc/12345/cmdline contains the null-terminated command line
  2. -0 tells xargs to expect null-terminated input
  3. printf ‘%s\n’ prints each argument followed by a newline character
  4. < redirects the data from the cmdline file of the process with PID 12345 as input to xargs

In summary, this approach effectively handles null-terminated arguments, ensuring that they are printed correctly without truncation. Moreover, it’s particularly useful when dealing with command lines that contain spaces or special characters.

5. Points to Consider

When using methods to avoid truncation of command output in Linux, there are several important points to consider:

  1. quoting and escaping: properly quoting or escaping special characters in commands is often critical, especially when dealing with spaces or symbols that have special meanings in the shell
  2. version differences: different versions of commands or utilities may behave differently, so what works on one system might not work exactly the same way on another
  3. filesystems permissions: some commands or tools may require specific permissions to access certain files or information, hence, we ensure that we have the necessary permissions to execute the commands and access the relevant files

So, by keeping these points in mind, we can effectively use various methods to avoid truncation and obtain complete information about processes or commands in a Linux environment.

Finally, it’s important to understand the underlying principles of each method and apply them appropriately based on our specific requirements and the characteristics of the system we’re working on.

6. Conclusion

Ensuring complete visibility of commands associated with running processes is crucial for effective system management.

In this article, we understood and identified the problem. Afterward, we looked at the ww and the xargs methods to avoid the truncation of the command field, in case the command is very long.