
Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: August 26, 2024
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.
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.
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:
Alternatively, we can use the -p option with the ps command to specify the PID as we did in the previous section.
xargs is a powerful and versatile command-line utility in Linux and Unix-like operating systems.
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.
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:
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.
When using methods to avoid truncation of command output in Linux, there are several important points to consider:
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.
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.