Unable to grep the process


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unable to grep the process
# 15  
Old 12-06-2016
Bug

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

Session 1:-
Code:
$ cat /tmp/robinsleep.sh 
sleep 888

$ /tmp/robinsleep.sh

Session 2:
Code:
$ 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 ?
# 16  
Old 12-06-2016
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
# 17  
Old 12-06-2016
Interesting difference between script with "shebang" and one without:

Code:
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?
These 5 Users Gave Thanks to RudiC For This Post:
# 18  
Old 12-06-2016
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.
# 19  
Old 12-06-2016
Tools

Quote:
Originally Posted by RudiC
Interesting difference between script with "shebang" and one without:

Code:
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?
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 ?

Last edited by Don Cragun; 12-06-2016 at 11:10 PM.. Reason: Remove duplicated text.
# 20  
Old 12-07-2016
The shebang must be there!
Code:
$ cat myscript
#! /bin/bash
$ sleep 888
./myscript &
[1] 7164
$ pgrep -l myscript
 7164 myscript

# 21  
Old 12-07-2016
Can I draw you back to the questions in post 16? There might be a better way to achieve your overall goal rather than to be looking for processes all over the place. You might consider the risks that someone viewing the script may create a process that is matched by your grep, which could cause results to be unpredictable.



Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unable to grep using wildcard in a file.

I wish to check if my file has a line that does not start with '#' and has 1. Listen and 2. 443 echo "Listen 443" > test.out grep 'Listen *443' test.out | grep -v '#' Listen 443 The above worked fine but when the entry changes to the below the grep fails... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Shell Programming and Scripting

Unable to do grep in a script

Hi, I am trying to grep a filename from a script after taking the file name and other variables as keyboard input .When I run the grep command with the same filename on the prompt, it runs fine, but it is either not giving me the correct output or not running at all from the script using the... (13 Replies)
Discussion started by: dsid
13 Replies

3. UNIX for Dummies Questions & Answers

Unable to grep

I have a file with 2 lines of code Rome is in Romeo Romeo is in Rome How do I grep, so that only last line would be the outcome. sample output Romeo is in Rome I have tried with all possible greps but its resulting in both the lines in output. Please help. (6 Replies)
Discussion started by: gotamp
6 Replies

4. UNIX for Dummies Questions & Answers

cron job is unable to process

Hi All, I have got a shell script that excutes some job and mails me the output as an attachment. While running the script manually, its perfect. when i am scheduling the job through crontab, i am getting the mail. but the attachment but this is a blank file. after the scheduler run, i can... (2 Replies)
Discussion started by: gotam
2 Replies

5. Shell Programming and Scripting

Unable to Kill the process

Hi Guys, I am unable to kill the process . and same process is running so many times. If i am trying to kill once again it restarting/kicked off again. and now i am unable to login . i am getting error message like "error -cannot fork too many process " i know when we ll get this message... (8 Replies)
Discussion started by: kartheek
8 Replies

6. Shell Programming and Scripting

grep the process id and kill all the filtered process

Hi I want to write a shell script which can find the process id's of all the process and kill them eg: ps ax | grep rv_ 3015 ? S 0:00 /home/vivek/Desktop/rv_server 3020 ? S 0:00 /home/vivek/Desktop/rv_gps 3022 ? S 0:00 /home/vivek/Desktop/rv_show ... (7 Replies)
Discussion started by: vivek_naragund
7 Replies

7. BSD

Unable to process from lines (envelopes)

Hi, Some of the users on my freebsd server are getting the "unable to process from lines" error when accessing their mailbox. I've checked their mailbox and found that there was a blank line at the top of the mailbox. Everytime i remove it, it appears again sometime later. I've tried... (1 Reply)
Discussion started by: sc_king
1 Replies

8. HP-UX

Unable to kill a process

problem. (2 Replies)
Discussion started by: pvamsikr
2 Replies

9. Shell Programming and Scripting

unable to grep the following pattern

I have the following line in file1 elif ; then now if i try to grep this using following command grep -e "elif ; then" file1 it is showing nothing... how to grep such patterns (2 Replies)
Discussion started by: suri
2 Replies

10. Shell Programming and Scripting

Unable to kill sleeping process

Hi, I'm trying to delete a sleeping process (parent ID is not 1) with "kill -9" command by the owner of the process (infodba) but it doesn't get killed. Is there any way of killing this process without killing the parent process or rebooting? (I'm using HP Unix B.11.11) $ ps -eflx | grep... (0 Replies)
Discussion started by: stevefox
0 Replies
Login or Register to Ask a Question