Linux ps command and scripting with nodejs

So today I am taking a look at the Linux ps command. This command can be used to get a snapshot of all the processes running on Linux at the moment. There are many options for the command that can be used to control selection and formating of the output. In this post I will be going over some typical examples of the Linux ps command, and a use case example with nodejs.

1 - Some basic examples of the linux ps command

In this section I will be going over some typical use case examples of the Linux ps command. There are allot of options and ways of formatting the output, and if you really want to dive deep into it all the best source on that might be the ps command manual page as always. However here I thought I would write about the top typical options and formatting tricks that I find myself using.

1.1 - See all process runiing on the system

So if I just want a list of all processes running at the present moment I will call the ps command with the -e option. If I just give the -e option and nothing else then all processes will be selected and the default formating will be used.

1
$ ps -e

1.2 - Seee just process for a given user

So there is getting a long list of everything that is running on the computer, but often I might just want to see what is running for just a given username. So if I just want what is running for a username such as pi I just need to use the uppercase U option of linux ps and pass the username. The result should be a list of processes that are ruining just for that username.

1
$ ps -U pi

1.3 - Just proceses of a given name

Sometimes I will want to select process by a given set command name, for that there is the upper case C option of linux ps.

1
2
3
4
5
6
7
8
9
10
$ ps -C chromium-browse
19422 ? 00:01:33 chromium-browse
19444 ? 00:00:00 chromium-browse
19446 ? 00:00:00 chromium-browse
19468 ? 00:00:46 chromium-browse
19472 ? 00:00:06 chromium-browse
19632 ? 00:00:13 chromium-browse
20058 ? 00:00:17 chromium-browse
20253 ? 00:00:15 chromium-browse
20431 ? 00:00:10 chromium-browse

1.4 - Custom output

Whe it comes to controlling what will be displayed for each process that will should up there is the lowercase o option as well as several other options that mean the same thing. To have full control over the output of ps you will want to read up on the STANDARD FORMAT SPECIFIERS of the Linux ps command, and again it would be best to look at the man pages when it comes to this because there are a lot of them.

1
2
3
4
ps -U avahi -o "pid uname comm"
PID USER COMMAND
375 avahi avahi-daemon
406 avahi avahi-daemon

2 - Example 1 of Linux ps and making a custom array of command names and process ids with nodejs

In this example I am using the spawn child process module method in nodejs to call the ps command from within a nodejs script. I am using the -o option to change the formating so that it just displays command names alone and then process ids next. I then used event handlers for the data event of the standard output to build a string, and then then end event of that stream to know that the list is done. Once I have the list I then used Array.filter and Array.map to build an array of arrays where each first element is a process name and each second index is a process id.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
let spawn = require('child_process').spawn;
let ps = spawn('ps', ['-e','--no-headers', '-o', 'comm,pid']);
let text = '';
ps.stdout.on('data', (data) => {
text += data.toString();
});
ps.stdout.on('end', (data) => {
let commands = text.split(require('os').EOL);
commands = commands.filter((comm)=>{
return comm.match(/^n/)
}).map((comm)=>{
return comm.replace(/ +/, ':|:').split(':|:')
})
console.log(commands);
// [ [ 'netns', '30' ], [ 'nfsiod', '47' ], [ 'node', '10018' ] ]
});

Not a practical example at least in its current state, but depending on what it is that I am trying to work out with Shell Scripting something like this could turn into something piratical of course. For example say I want to check if a process is not running first, and if not start it. Also say that I want to count the number of instances of that process that are currently running and if necessary call a kill command. In that case something like this would be a good starting point right?