Friday, September 12, 2008

Listing Full Command Line on HP-UX

There is one very annoying limitation on HP-UX - there is no /proc folder and ps command can display maximum 63 characters from processes command line. There is no simple way how to display full command line of a specified process.

If you're working with Java programs, you know, that the first 63 characters barely reach the classpath definition. Usually it's just the path to the java binary.

Yesterday I needed to find out, if one java program is not running multiple times. And guess what, it's almost impossible to find out on a server with tens of different java programs running. After lot of searching I came up with this program - it lists all the running process ids and their respective command lines. There is no original code happening, it's basically, this forum post merged with this manual page.

#include <stdio.h>
#include <sys/pstat.h>
#include <sys/pstat/pstat_ops.h>
#define BURST 20
#define MAX_LENGTH 1024

struct pst_status pst[BURST];

int main(void)
{
char command [MAX_LENGTH];
        union pstun pu;

        int i, count;
        int idx = 0;/* index within the context */
        /* loop until count == 0, will occur all have been returned */

        while ((count=pstat_getproc(pst, sizeof(pst[0]),BURST,idx))>0)
        {
                for (i = 0; i < count; i++)
                {
                        pu.pst_command = command;
                        if (pstat(PSTAT_GETCOMMANDLINE, pu, MAX_LENGTH, 1, pst[i].pst_pid) == -1)
                        {
                                printf("ERROR: failure using pid(%d)\n", pst[i].pst_pid);
                                exit(-1);
                        }
                        printf("%d: %s\n",  pst[i].pst_pid, pu.pst_command);
                }
                idx = pst[count-1].pst_idx + 1;
        }

        if (count == -1)
                perror("pstat_getproc()");
}

Compile the code with the following command

cc pstat.c -o pstat +DD64

You have to include the +DD64 if you're on a 64bit system. Otherwise, you'll get following error:

pstat_getproc(): Value too large to be stored in data type

Of course, about five seconds after finishing this article I found a way how to do it with the original HP-UX ps command, so please disregard my blabbering :)

ps -exx

1 comments:

Anonymous said...

ps -exx will only show you the 1st 1022 characters.
As you said Java processes have lots of arguments.
I am still looking for away to get the entire command line, even if its more than 1022 characters.

The code sample that you provided is nice but only return 1022 characters
on HPUX 11.11