Tip

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

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

Requires Free Membership to View

"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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.