Unable to grep the process

I have user1 run a script called logginexpert.sh while has this line of code

sleep 888

I then login to another putty session with another user2 and try to grep for the logginexpert.sh process using

ps -ef | grep ex
SunOS mymac 5.11 11.2 sun4u sparc SUNW,SPARC-Enterprise

But, i dont get any results when i was expecting it to show the process logginexpert.sh that was started by user1.

Can you please help me fix this problem ?

Does it show up in the long list, when not grep ping?

Not sure what long list is but i grep for the entire script name and it still does not showup in the results.

ps -ef | grep logginexpert.sh
No results Found.

I also tried

 ps -ef >/tmp/tmp.tmp
grep ex /tmp/tmp.tmp
No results Found.

Can you even SEE the process using ps -ef

Do not use more or grep. Why? If the path to the file is part of the ps display for

 logginexpert.sh

then it may include the entire path - which can exceed what ps will show - too many characters.

PS: on Solaris pgrep may be your better choice for this kind of thing.

location of logginexpert.sh is /tmp/logginexpert.sh

 ps -ef | pgrep /tmp/testbkpme.sh
No results Found.
ps -ef | pgrep bk
No results Found.

ps -ef also does not show /tmp/logginexpert.sh but I'm able to grep the process by the pid but not with the script name.

Putty Session 1:

/tmp/logginexpert.sh &
[1]     4627

Putty Session 2:

You have new mail in /var/mail/user2
ps -ef | grep 4627
    user1 4627 10477   0 13:54:09 pts/11      0:00 sleep 888
ps -ef | grep ex
    user2 5650 15730   0 13:54:27 pts/16      No results Found.

So definitely the process is running.

---------- Post updated at 03:12 PM ---------- Previous update was at 02:36 PM ----------

Also ps -ef does not show the name of the script but i see this sleep 888 in the results.

Can you please help sort this out ? i want to grep by name of the script not by its contents.

Display the processes of user1

ps -fu user1

If "sleep" is spawned by the script then look for its parent process (given in column 3).
Maybe your grep is forged?

/bin/ps -fu user1 | /bin/grep ex

grep can match its own argument in the ps list. pgrep avoids that.

pgrep -u user1 ex

The issue is not to do with different users or different putty session. Becoz i Cannot find i.e grep for the logginexpert.sh script with the same user in the same putty session.

Also,

  /bin/ps -fu | /bin/grep ex
  /bin/ps: option requires an argument � u
  
  pgrep -u user1  ex
  No results Found.

/bin/ps fu  | /bin/grep ex
/bin/ps: illegal option -- f

/bin/ps u   | /bin/grep bk
Result not Found.
   

Let me know what are we missing ?

How do you expect grep to find it, then? Please show the output of /bin/ps -ef for ALL the relevant processes.

Here is the Output

     UID   PID  PPID   C    STIME TTY         TIME CMD
  netcfg  5865  4775   0   Nov 20 ?           0:36 /lib/inet/netcfgd
    root  4775  4775   0   Nov 20 ?           0:00 zsched
  daemon  5887  4775   0   Nov 20 ?           0:17 /lib/crypto/kcfd
    root  5997  4775   0   Nov 20 ?           1:17 /lib/inet/in.mpathd
    user2  7364  5339   0 04:01:03 ?           0:00 ksh -x pls_med_02001.sh
    root 12094 11832   0 10:49:27 pts/5       0:00 sudo -u user2 -i -H
    user2 13301 13290   0   Nov 28 pts/10      0:00 zsh
    root 11047  4775   0   Nov 20 ?          66:02 ./Agent.bin -a
    root 12877 12720   0   Nov 28 pts/9       0:00 sudo -u user2 -s -H
    root 12239 12094   0 10:49:30 pts/6       0:00 sudo -u user2 -i -H
    user2 16470 29038   0 04:04:28 ?           0:00 sleep 60
user1 26410 26381   0 12:04:26 pts/17      0:00 bash
    user2 15504 15503   0 04:03:44 pts/16      0:00 sleep 888
user1 26381 26380   0 12:04:24 pts/17      0:00 -ksh
user1 28576 28575   0 12:05:37 pts/8       0:00 -ksh
u1064978 12720 12433   0   Nov 28 pts/9       0:00 bash
    root 15626 15477   0 12:11:39 pts/16      0:00 sudo -u user2 -s -H
    user2 15730 15627   0 12:11:41 pts/16      0:00 bash
    user2 16716 16712   0 04:04:40 ?           0:00 tee -a sendemail.603486944.154.log
    root 10476 10328   0 12:21:43 pts/11      0:00 sudo -u user2 -s -H
    root  2512  2511   0 00:33:22 ?           0:01 cdcwatch 2511 -F -M
    user2 12241 12239   0 10:49:30 pts/6       0:00 -ksh
    user2 25389 25384   0 03:40:07 ?           0:00 ksh -x eload.sh 431 LOAD REAL 58731
    root 10328 26410   0 12:21:39 pts/17      0:00 sudo -u user2 -s -H
   rgopi  9324  4775   0        - ?           0:00 <defunct>
    root 13289 12877   0   Nov 28 pts/10      0:00 sudo -u user2 -s -H
    root 15477   781   0 12:11:36 pts/8       0:00 sudo -u user2 -s -H
    user2 16256 28862   0 04:04:23 ?           0:00 sleep 60
    user2 15503 15730   0 04:03:44 pts/16      0:00 bash
    user2 12309 12241   0 10:49:30 pts/6       0:00 zsh
    user2 15949 12309   0 10:50:45 pts/6       0:00 cat
user1   781 28576   0 12:06:36 pts/8       0:00 bash

You have new mail in /var/mail/user1

The one in BOLD is my process which i m not able to grep (find).

Which "ex" string do you want to match in that bolded line?

That process' parent process (ID 15503) is a bash shell - NO logginexpert.sh ! So I have to challenge your first statement in post#1.

k ... looks like i have to explain this again.

The script name is logginexpert.sh which has the sleep 888 line in it.

So, when i run logginexpert.sh in the background i see the sleep 888 which is shown in the output of my /bin/ps -ef command.

So, it is not listing the process with the name of the script i.e logginexpert.sh which is what i want !! ...instead it is listing the sleep 888 command that is in the script.

I have highlighted this before as well.

And I'd challenge that statement. A script would usually wait for its children (and then show up in the ps listing). I don't even believe you put sleep 888 into background with nohup or disown , as then, it's parent would become 1 ( init ).
Post the relevant part of logginexpert.sh .

more logginexpert.sh

echo wowww
sleep 888
echo wow

You are probably hitting the problem that you script is only showing as a bash process like me:-

Session 1:-

$ cat /tmp/robinsleep.sh 
sleep 888

$ /tmp/robinsleep.sh 

Session 2:

$ ps -ef|grep sleep
rbatte1 21561 21560  0 11:47 pts/9    00:00:00 sleep 888
rbatte1 21563 21537  0 11:47 pts/10   00:00:00 grep sleep
$ ps -fp 21560
UID        PID  PPID  C STIME TTY          TIME CMD
rbatte1 21560 21516  0 11:47 pts/9    00:00:00 bash
$

Robin

k... whats the solution ?

Well, that depends on what the overall process is. What are you trying to work out?

For instance:-

  • Are you trying to ensure you only have one copy of a script running at a time?
  • Do you have different scripts that must not run together?
  • Are you trying to raise alerts with script overrunning?

if we can understand the context, then we may be able to suggest a better solution that will be flexible and let you evolve and support the code for the future.

Robin

Interesting difference between script with "shebang" and one without:

cat shsleep1
#! /bin/bash
sleep 888

./shsleep1

ps -efH
.
.
.
  3272  3219  0  80   0 -  5698 wait   10:38 pts/3    00:00:00               /bin/bash
  4423  3272  0  80   0 -  3518 wait   13:12 pts/3    00:00:00                 /bin/bash ./shsleep1
  4424  4423  0  80   0 -  2205 hrtime 13:12 pts/3    00:00:00                   sleep 888


cat shsleep2
sleep 888

./shsleep2

ps -efH
.
.
.
  3272  3219  0  80   0 -  5698 wait   10:38 pts/3    00:00:00               /bin/bash
  4588  3272  0  80   0 -  5701 wait   13:15 pts/3    00:00:00                 /bin/bash
  4589  4588  0  80   0 -  2205 hrtime 13:15 pts/3    00:00:00                   sleep 888

I didn't realize that before.
Does it help you to shift a shebang into your script?

5 Likes

Seems to be a SysV thing. I reproduced this behavior on HP-UX and AIX.
If there is no shebang, the execve() does not change the args, so they remain from the previous fork() i.e. are a copy from the invoking shell.

If i remove #! /bin/bash from the script other commands are getting affected. So, I need #! /bin/bash else my script fails for other reasons.

The reason i wrote the script is only to check if the script is running as a process or not.

What can be done ?

The shebang must be there!

$ cat myscript
#! /bin/bash
$ sleep 888
./myscript &
[1] 7164
$ pgrep -l myscript
 7164 myscript