Getting arguments passed to command

Neeraj Singh

By Neeraj Singh

on September 22, 2013

In previous blog we discussed ruby code where we used ps -ocommand. In this blog let's discuss how to get arguments passed to a command.

What is the issue

In the referred blog we are trying to find if --force or -f argument was passed to the git push command.

The kernel knows the arguments that was passed to the command. So the only way to find that answer would be to ask kernel what was the full command. The tool to deal with such issues is ps.

In order to play with ps command let's write a simple ruby program first.

1# sl.rb
2puts Process.pid
3puts Process.ppid
4sleep 99999999

In terminal execute ruby sl.rb. In another terminal execute ps.

1$ ps
2  PID TTY           TIME CMD
382246 ttys000    0:00.51 -bash
487070 ttys000    0:00.04 ruby loop.rb a, b, c
582455 ttys001    0:00.40 -bash

So here I have two bash shell open in two different tabs in my terminal. First terminal tab is running s1.rb. The second terminal tab is running ps. In the second terminal we can see the arguments that were passed to program s1.

By default ps lists all the processes belonging to the user executing the command and the processes started from the current terminal.

Option -p

ps -p87070 would show result only for the given process id.

1$ ps -p 87070
2  PID TTY           TIME CMD
387070 ttys000    0:00.04 ruby loop.rb a, b, c

We can pass more than on process id.

1$ ps -o pid,command -p87070,82246
2  PID COMMAND
382246 -bash
487070 ruby loop.rb a, b, c

Option -o

ps -o can be used to select the attributes that we want to be shown. For example I want only pids to be shown.

1$ ps -o pid
2  PID
382246
487070
582455

Now I want pid and command.

1$ ps -o pid,command
2  PID COMMAND
382246 -bash
487070 ruby loop.rb a, b, c
582455 -bash

I want result only for a certain process id.

1$ ps -o command -p87070
2COMMAND
3ruby loop.rb a, b, c

Now we have the arguments that were passed to the command. This is the code that article was talking about.

For the sake of completeness let's see a few more options.

Option -e

ps -e would list all processes.

1$ ps -e
2  PID TTY           TIME CMD
3    1 ??         2:56.20 /sbin/launchd
4   11 ??         0:01.90 /usr/libexec/UserEventAgent (System)
5   12 ??         0:02.11 /usr/libexec/kextd
6   14 ??         0:09.00 /usr/sbin/notifyd
7   15 ??         0:05.81 /usr/sbin/securityd -i
8   ........................................
9   ........................................

Option -f

ps -f would list a lot more attributes including ppid.

1$ ps -f
2  UID   PID  PPID   C STIME   TTY           TIME CMD
3  501 82246 82245   0  2:06PM ttys000    0:00.51 -bash
4  501 87070 82246   0  4:54PM ttys000    0:00.04 ruby loop.rb a, b, c
5  501 82455 82452   0  2:07PM ttys001    0:00.42 -bash

Stay up to date with our blogs. Sign up for our newsletter.

We write about Ruby on Rails, ReactJS, React Native, remote work,open source, engineering & design.