Get the full command line argument of any running process with bash

Get the full commandline arguments of any running process with this tip.

This tip was submitted to the SearchEnterpriseLinux.com tip exchange by member Nico Coetzee. Please let other users know how useful it is by rating it below.


Get the full commandline arguments of any running process with this tip. Here's the situation: Your platform is Linux, and your script is bash. Here's your problem: When you do a normal ps, you can't see the full command line arguments of a process.

Run a script called monitor.sh to print the full command line arguments of any running process. Here's how it works: The script is run with ./monitor.sh <processname>. Internally, the script will run a loop with the ps command on the <processname> and extract the process ID. With each process ID, the script then cat through the relevant /proc/<processid>/cmdline file to get the full command line arguments.

Instead of ./monitor.sh <processname> you can also run it with ./monitor.sh <processid>.

Say I have a process like aterm, I will normally see the following from ps:

$ ps -aux | grep aterm
f2821796 11344  0.0  0.1  5672  340 ?        S    Jan13   0:00 aterm -t
As you can see, I can't always see the full command line option. With the monitor.sh script I can:
$ ./monitor.sh 11344
Full command line for 11344...
aterm -transparent -sl 200 -tint Green -sh 35% -bgtype cut -name Terminal -fg 
grey -fn -*-fixed-bold-*-*-*-15-*-*-*-*-*-*-*

======= START monitor.sh =======
#!/bin/bash

echo "Full command line for $1..."

for PRID in `/bin/ps  -ax | /bin/grep $1 | /bin/grep -v grep | /bin/grep -v 
monitor.sh | /bin/gawk '{ print $1 }'`
do
 /bin/cat /proc/$PRID/cmdline | /bin/gawk -F[[:cntrl:]] '{ for ( i = 1; i < 
NF+1; i++ ) printf( "%s ", $i ) } { print "n" }'

done
======= END monitor.sh =======

With line numbers:

     1 #!/bin/bash
     2 
     3 echo "Full command line for $1..."
     4 
     5 for PRID in `/bin/ps  -ax | /bin/grep $1 | /bin/grep -v grep | 
/bin/grep -v monitor.sh | /bin/gawk '{ print $1 }'`
     6 do
     7  /bin/cat /proc/$PRID/cmdline | /bin/gawk -F[[:cntrl:]] '{ for ( i = 1; 
i < NF+1; i++ ) printf( "%s ", $i ) } { print "n" }'
     8 
     9 done
This was first published in January 2004

Dig deeper on Introduction to Linux system administration

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchDataCenter

SearchServerVirtualization

SearchCloudComputing

SearchEnterpriseDesktop

Close