-1

Below is the output of executing two commands ads2 svcd& and ps -aux|grep ads2

nvidia@nvidia-desktop:~$ ads2 svcd&
[1] 4593
nvidia@nvidia-desktop:~$ ps -aux|grep ads2
nvidia    4593  0.5  0.0  39796 23864 pts/0    Sl   08:20   0:00 /opt/ads2/arm- 
linux64/bin/ads2svcd
nvidia    4603  0.0  0.0   6092   672 pts/0    S+   08:20   0:00 grep --color=auto ads2
nvidia@nvidia-desktop:~$ 
nvidia@nvidia-desktop:~$ 

the command ads2 svcd& runs a process related to ads2 software. with ps -aux|grep ads2 i displayed the whole processes that contains the name "ads2".

Now What i'm trying to do is to get the process number of the ads2 which in this example is 4593. So i wrote the follwing bash script:

#!/usr/bin/env bash
process="$(ps -aux|grep ads  | grep 'nvidia' | cut -d' ' -f 3)" 
echo "The current ads2 process is " $process 

The bash script outputs the following:

nvidia@nvidia-desktop:~$ ./test.sh 
The current ads2 process is 

As you see the process number is not filtered. So what i'm i doing wrong?

thanks in advance

Khaled
  • 555
  • 1
  • 6
  • 26
  • Try `pgrep ads2` – anubhava Mar 16 '21 at 07:33
  • Reinventing `ps` tools is a common but unhappy beginner problem. Just look for existing tools which do this better, like the one suggested by Anubhava. – tripleee Mar 16 '21 at 07:35
  • The concrete problem is that `cut` regards every space as a new column separator, so you are picking out the empty column between the second and third spaces in the `ps` output. Anything with more than just a couple of `grep`s should probably be refactored to Awk anyway. `ps -aux | awk '/ads/ && /nvidia/ { print $2 }'` (assuming you actually really want the PID, which is in the second column, not the third). – tripleee Mar 16 '21 at 07:38
  • Just do `echo $!`??? – oguz ismail Mar 16 '21 at 07:44
  • @tripleee `nvidia@nvidia-desktop:~$ ps -aux | awk '/ads/ && /nvidia/ { print $2 }' 3755 4922 7021 7037 7122` It prints a bunch of processes! – Khaled Mar 16 '21 at 08:03
  • Then you have multiple processes which match those regexes; I just corrected the obvious error, and have not posted this as an actual attempt to answer your question. My recommendation remains to abandon doing this on your own, and use an existing tool. – tripleee Mar 16 '21 at 08:05
  • @tripleee Tried the suggestion from anubhava also returned empty – Khaled Mar 16 '21 at 08:07
  • @oguzismail. what does `echo $!` do? – Khaled Mar 16 '21 at 08:09
  • Because the process you are looking for is apparently called `ads2svcd`. You can play around with the options to `pgrep` to match on a substring too. – tripleee Mar 16 '21 at 08:09
  • Immediately after `ads2 svcd&` the variable `$!` will contain the process ID of the background process. – tripleee Mar 16 '21 at 08:10
  • @tripleee. But that would print out the process ID and i want to assign it to process variable – Khaled Mar 16 '21 at 08:28
  • 1
    Then use `variable=$!` instead of `echo $!` – tripleee Mar 16 '21 at 08:29

1 Answers1

0

List all the processes in the current shell with $$ built-in variable

 ps --forest -gp $$
  PID TTY      STAT   TIME COMMAND
 3809 pts/1    Ss     0:01 bash
 4896 pts/1    T      0:00  \_ vim file.json
22965 pts/1    S+     0:00  \_ ssh dw
 3607 pts/0    Ss     0:01 bash
 2500 pts/0    R+     0:00  \_ ps --forest -gp 3607
 3327 tty2     Ssl+   0:00 /usr/lib/gdm3/gdm-x-session --run-script i3
 3329 tty2     Sl+    8:12  \_ /usr/lib/xorg/Xorg vt2 -displayfd 3 -auth /run/user/1000/gdm/Xauthority -background none -noreset -keeptty -verbose 3
 3346 tty2     S+     0:03  \_ i3

Just see the pid of them:

ps -opid --forest -gp $$
  PID
 3809
 4896
22965
 3607
 2688
 3327
 3329
 3346

If you need to use grep for any reason, use -opid,cmd with current shell:

ps -opid,cmd -gp $$ | grep vim
 3851 grep --color=auto vim
 4896 vim file.json

For all, just use -e

ps -e -opid,cmd | grep vim
 4141 grep --color=auto vim
 4896 vim file.json

The complete one, we have to ignore the grep itself:

ps -e -opid,cmd | grep vim | grep -v grep |  cut -d' ' -f 2
4896

Without double grep using comm option for ps

ps -opid,comm -gp $$ | grep vim
 4896 vim

of course the simplest one is pgrep

pgrep vim
4896 

NOTE for variable assignment there should NOT be any space:

# wrong
ads2ProcessId = $(pgrep ads2)

# right
ads2ProcessId=$(pgrep ads2)
Shakiba Moshiri
  • 21,040
  • 2
  • 34
  • 44
  • Thanks. However, when i assign the result to a variable like `ads2ProcessId = $(pgrep ads2) `, it returns an error `./test.sh: line 7: ads2process: command not found `. Do you know what causes the error? – Khaled Mar 16 '21 at 10:56
  • Do you use space for assignment : `ads2ProcessId=$(pgrep vim)` works, with no problem – Shakiba Moshiri Mar 16 '21 at 10:58
  • @k.jbaili Note the that command which is run is `nvidia` and not `ads2` so make sure you are looking for a right command. The name `ads2` is a sub-directory within `/opt/ads2/arm-linux64/bin/ads2svcd` – Shakiba Moshiri Mar 16 '21 at 11:05
  • 1
    Thank you alot. it works yes its because the spaces – Khaled Mar 16 '21 at 11:20