Search this blog ...

Tuesday, February 26, 2013

xargs - is command executed once or multiple times – examples

xargs is used to execute a command one or potentially multiple times using the standard input as arguments to that command.

What sometimes in unclear is whether the command invoked by xargs is executed once or multiple times.

This blog post should clear things up …

Firstly, here is the xargs program usage in layman’s terms

xargs [xargs options] [command] [arguments for command based on xargs standard input]

The standard input would typically be output that was piped from an earlier command such as find / ls / echo etc.  It doesn’t have to be though, you can invoke xargs as a standalone no-arg command and simply type the input and terminate with a control-d to signal end-of-terminal.

Let’s try a simple example combining the xargs --verbose option so that we can see the command-line that xargs will execute on the standard error output before it is actually invoked. In the screenshot below, I invoke the xargs command, and then enter 1 to 5 separating each with a newline (enter), followed by a terminating ctrl-d.  As I provided the --verbose option to xargs, it wrote out the command that it will execute and the arguments that it is going to provide to that command.

image

The output above shows that be default if you don’t provide an explicit command for xargs to invoke, it will leverage /bin/echo. You can also see xargs invoked this echo command just a single time. What may not be obvious is how xargs processed the standard input to come up with arguments to supply the (in this case 'echo') command. xargs will by default treat whitespace and newlines as delimiters. In the example below, I entered: 1 TAB 2 NEWLINE 3 SPACE 4 NEWLINE 5 NEWLINE ctrl-d.

image

So how and under what conditions does the command that xargs executes get invoked multiple times?

Well.. you can either explicitly tell xargs that a particular command can only operate on a specific number of arguments at a time, and that the command should be reinvoked as required to work through the remaining arguments; OR … xargs may determine itself that the command to execute along with any arguments hits a maximum command-line length, in which case it automatically splits the arguments across multiple command invocations.

Let’s first explicitly tell xargs that a command invocation should only work on a maximum number of arguments at a time.  You do this through the -n option …

image

The above example first shows supplying a “-n1” option which results in a command being invocated for each argument. Later the “-n2” option is leveraged which result in a command being invoked for every two arguments. The final xargs test above shows how 1 TAB 2 NEWLINE 3 SPACE 4 NEWLINE 5 NEWLINE ctrl-d is processed with the “-n1” option.  You can see that xargs immediately starts invoking commands after each line of input is processed.

As mentioned prior, xargs may itself automatically split arguments across multiple commands if a maximum command-line character length is reached. The xargs binary will likely have a default size limit hardcoded to the operating system ARG_MAX length.

image

You can explicitly tell xargs the max command-line length using the --max-chars or -s options.  The length comprises the command and initial-arguments and the terminating nulls at the ends of the argument strings.  As seen below “/bin/echo 1” will take 12 chars, and “/bin/echo 1 2” will take 14 chars (including terminating null).

/bin/echo 1 2
12345678901234

Let’s try out this “-s” option…

image

Here is a final example that ties everything together.  It demonstrates the following:

  1. xargs receiving piped standard-input from a prior command (i.e tr / find).
  2. xargs using the --verbose option to output the command that will be invoked
  3. xargs being told to leverage an explicit delimiter “-d” option, e.g “\n” – newline
  4. xargs invoking an explicit command (the Unix “file” command)
  5. xargs invoking an explicit command once per argument (“-n1”)
  6. find command leveraging the “-print0” option to delimit search results by the null character (rather than newline), in conjunction with the “-0” (or --null) xargs option so that any search results which contain whitespace are treated correctly as a single command-argument.

image

image

Wednesday, February 6, 2013

Java Applet terminated in browser when debugging – solution

I recently had to troubleshoot an old applet that was not working as expected. I intended to leverage JDeveloper to debug the applet utilizing JPDA socket debug steps listed in: http://docs.oracle.com/javase/6/docs/technotes/guides/plugin/developer_guide/debugger.html

I was able to successfully connect/attach to the VM from JDeveloper and start stepping through code, however within a minute or so, the applet would unexpectedly terminate.  You can imagine how frustrating this was.  A quick search on google listed possible causes such as browser plugin watchdogs, need for administration privileges, JRE bugs etc. etc.

It turns out the problem was caused due to a breakdown in the heartbeat messages sent between the browser VM and client VM.  Invoking the browser with the environment variable JPI_PLUGIN2_NO_HEARTBEAT=1 enabled the debugging to proceed as intended without the applet/Java-plugin being killed / terminating.

Below are the steps I performed to successfully debug an applet:

From Windows Control Panel choose Java > "Java" tab > "View..." button for Java Runtime Environment Settings
Double click table column value for Runtime Parameters for the user JRE entry.

Set the value as
-agentlib:jdwp=transport=dt_socket,server=y,address=8453,suspend=n

click "OK"
Click "Apply" on the Java Runtime Environment Settings page
Click "OK" on the Java Runtime Environment Settings page

jre
For debugging, the following advanced options should also be set for the JRE:

"Advanced" tab > Settings > Debugging
Enable tracing
Enable logging

"Advanced" tab > Settings > Java console
Choose Show console

Having configured the JRE options, we now need to disable that heartbeat message mechanism before we launch the browser.  As mentioned above, failure to perform this will result in the Applet / JRE abnormally terminating typically under a minute or so of stepping in to code.
You will likely see an exception along the lines of
basic: JVM[id=0]-Heartbeat heartbeat dead, exception. dT=NNN seconds.

From command-prompt
set JPI_PLUGIN2_NO_HEARTBEAT=1

And then launch browser, e.g.

%LOCALAPPDATA%\Google\Chrome\Application\chrome.exe --disable-hang-monitor

e.g. C:\Users\mshannon\AppData\Local\Google\Chrome\Application\chrome.exe --disable-hang-monitor

The "--disable-hang-monitor" option passed to Chrome prevents it from popping up the ‘plugin not responding’ dialog.

image

Additional command-line options for Browser JVM Troubleshooting can also be set; These result in verbose console messages such as rendering the parameters supplied to the applet.

set JPI_PLUGIN2_DEBUG=1
set JPI_PLUGIN2_VERBOSE=1

Check for JRE logs/traces in %USERPROFILE%\AppData\LocalLow\Sun\Java\Deployment\log

e.g. C:\Users\mshannon\AppData\LocalLow\Sun\Java\Deployment\log

Once I was able to properly step through code, I found the cause of the problem with our applet.  The applet was no longer able to obtain from the browser an OAM 11g webgate cookie which had been set to be httponly.

Simple Solution - Black Wallpaper Background in Android

I’ve just got hold of an Android Samsung Galaxy S2 phone, and I want a fixed background color, in particular black.  Why does Google make this so difficult.

Here is my solution that involves installing nil/zero android apps that took less than a minute.

Step 1. Go to GSMARENA.COM and lookup the Display size resolution for your phone.  For the galaxy S2, it is 480 x 800 pixels.

Step 2. Fire up a basic paint application, such as Windows paint.

Step 3. Resize the image to the resolution from above

Step 4. Choose the appropriate color you want for the wallpaper/background, and leverage the fill/bucket tool to fill the image

Step 5. Save the image as a GIF, JPG, or PNG

Step 6. Email the image to your gmail account accessible from your android phone

Step 7.Open Gmail application and the email containing image just sent

Step 8. Click Save and View on Image

Step 9. Press Menu button in Viewer, and Choose “Set as” option and Home screen wallpaper or Lock screen wallpaper etc.

Step 10. Choose the correct orientation for the image – portrait etc, and click Save.

Congratulation, you should now have a fixed background.

Snap1Snap2Snap3Snap4

If you have a 480x800 phone, you can probably just save the image linked below.

black