How to display list of processes sorted in reverse order for a bash, tsch, ksh shell in Unix - bash

Was wondering if someone could help me with this... I want to display a list of running processes sorted in reverse order. The reverse order is to be based on process identification value - PID.
I was also wondering does it matter for the processes to be displayed in a certain shell? do I have to include something specific in the line of code or merely just change shells. I want to do this for both tsch and bash.
I have looked up the commands but I am not sure how to bring it together. See below:
ps = is the command that shows information about processes running in memory
-p = by process ID
r = running processes
sorting method?
Not sure how to bring it together.
Thanks for the help

This will do it:
ps aux | sort -k2 -rn
The ps command varies depending on OS. I can confirm the above will work in Linux and FreeBSD.

ps has it's own sort on linux, This is how I use it:
ps -eo pid,user,rss,vsz,pmem,comm,lstart --sort rss
In your case
ps aux --sort pid

In bash the following command will do it:
~$ ps aux --sort -pid | less
Normal (descending) order is achieved by specifying --sort pid or --sort +pid. The latter doesn't make much sense though.

Related

BASH commands: pkill/pgrep

I'm struggling with understanding the difference between using pgrep, as opposed to using ps | grep.
Apart from the fact that pgrep will only list the PID, what is the difference?
When I run an instance of kwrite in the background of my terminal, as well as opening one up using the GUI, why is the output given by ps | grep kwrite only one PID, when two instances of kwrite are running?
On a slightly different note, why would using pkill to kill a crashed instance of kwrite not be a good way of killing a process? The only explanation that I could come up with, is that it would kill all processes with the string "kwrite" in them, and that may not be only the instance of kwrite I wanted to kill.
Thank you for your help.
ps | grep will fork 2 processes, pgrep only one.
ps | grep whatever may also list grep whatever depending when ps exits, pgrep whatever will not.
ps will need additonal command-line switches to show up both kwrite processes. (see: man ps and try ps -ef next time)
By default, ps selects all processes with the same effective user ID (euid=EUID) as the current user and associated with the same terminal as the invoker.
Try running a handfull kwrite instances, preferably with important and unsaved data to find out, why killing the crashed one using pkill might not be the most clever idea.

what is the difference in output for ps -www and ps command in unix

What is the difference between unix commands ps & ps -www, from man pages I saw this statement -w Wide output. Use this option twice for unlimited width., but when I use the -www I don't see any difference in output.
-bash-3.2$ ps 18451
PID TTY STAT TIME COMMAND
18451 ? Ds 1:02 ora_xxxx
-bash-3.2$ ps -www 18451
PID TTY STAT TIME COMMAND
18451 ? Ds 1:02 ora_xxxx
wide in the sense that it doesn't truncate output, not in the sense that it will output extra categories. Try it with ps -eF and ps -ewF and you will likely see the difference (depending on terminal size). If you instead want to display full output with more categories do ps -f or ps -F instead (or ps -o to specify which categories you want displayed)
Note that -ww gives you unlimited width, adding more ws does not make it longer.
One of my favourites is ps -efHww. It shows all processes as a hierarchy along with start time and consumed CPU time, with complete command lines.
Yes, there is a difference. You do not see any in your example, since the output is not wide enough. But if you have an output that exceeds a certain width (terminal width), then you will see a difference: the columns are not chopped any more. The result is that the output is wrapped over the line endings.

How to query for Ruby scripts currently running on Unix?

I have a few Ruby scripts: a.rb, b.rb and c.rb. These scripts are called from corresponding wrapper shell scripts: a.sh, b.sh and c.sh.
All these scripts are in a distributed environment:
`a.sh` and `a.rb` are on serverA
`b.sh` and `b.rb` are on serverB
`c.sh` and `c.rb` are on serverC
I need to write a script call.rb and its wrapper call.sh script, which should check for all the scripts currently running on the distributed environment.
I have the logic which will determine the different hosts that I have and how to communicate to these different hosts.
When any Ruby script is running, the command:
ps aux
shows:
ruby a.rb
I have no ideas on how to query for different scripts currently running. One thing to note is that there might be other Ruby scripts running in the system too, but I need to check only for a.rb, b.rb, or c.rb.
If you're doing a heartbeat check, or setting up a keep-alive check, why not have the files save their PID to a file at their startup, and then delete it when they quit?
The building blocks are:
$$ is the current process ID for a running script.
Ruby will run a block named BEGIN {} at start-up, before variables are defined. You can use that to create a PID file. Typically we use something like "#{ File.basename($0) }.pid" to create the filename.
Ruby will run a block named END {} at shut-down, as a last task. You can use that to remove the PID file.
Put the PID files in a well-known place. Where that is is left as an exercise for you to figure out for your OS.
Have your watchdog scan those, grab the PIDs, scan the process list for the PID IDs, possibly correlating them to the name of the pid file.
You can figure out more icing to put on your cake.
You can simply execute commands via SSH like this:
ssh user#host "ps -ef | grep '(ruby|[^.]+\.rb)'"
Grepping the output of ps for the script names would also work:
ps -ef | grep '(a.rb|b.rb|c.rb)'
Edit: If you don't want grep itself to show up in the process list, filter it like this:
ps -ef | grep '(a.rb|b.rb|c.rb)' | grep -v grep
If you want to solve this in Ruby, you could use a process monitoring tool like God. Monit, Bluepill or Eye

is there any way to know the pid of a launched program?

if I launch a bash script as a child, I can pass its own pid to the parent by using $$.
Is there any way to find the pid of a program that I launch from a script in background
like:
ping x.x.x.x &
what's the pid of that ping ?
(I just hope I expressed my self correctly ... my English is not the best)
PS. I'm looking for a simple and clean solution, I can imagine something like:
ping -t10000 -W10 x.x.x.x &
then
ps ax | grep 'ping -t10000 -W10 x.x.x.x'$
but is too complicated, also even that I used switches to personalize it is not clean, it may catch another processes in the system
The variable $! has the PID of the last background process you started.
Use this: $! right after executing the command whose PID you want. It means, though that you need to use an ampersand (&) after the command, to start it in background. E.g.:
my_command & CMDPID=$!
echo "$CMDPID"

How can I find out what a command is executing in Terminal on MacOs

After I run a shell script (which call a bunch a other scripts depend on conditions. which is too complicated to understand), I can execute a command 'gdbclient' at my MacOS terminal.
But when I do 'which gdbclient' and 'alias gdbclient', it shows nothing.
Is there anyway for me to find out what 'gdbclient' is actually doing?
You can open up another terminal window and type: ps
That will list all the running processes.
If your script is running as a different user than the current one, you can use ps -ef to list all running processes.
If you know the PID of the process that ran your script, you can find all child processes via parent PID using ps -f | grep [pid]
You can use the Activity Monitor to check things out pretty thoroughly. To get the right privileges to see everything going on you can do:
sudo open /Applications/Utilities/Activity\ Monitor.app/
Dtrace can give you some helpful information: dtrace
to find process 'gdbclient':
ps aux | grep gdbclient
That wont tell you what it's "doing" but that it's running

Resources