Check if PID exists


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check if PID exists
# 8  
Old 03-11-2013
Try with awk then..

Code:
ps -ef | awk '$2==15160'

This User Gave Thanks to pamu For This Post:
# 9  
Old 03-11-2013
If your OS has the /proc filesystem (most modern ones do) you can also test like this

Code:
if [ -d /proc/$PID]
then
      echo "Process is still running"
else
     echo "Process has stopped"
fi

Edit: above withdrawn, (HP-UX dosn't have /proc). You could get ps to check for you:
Code:
if ps -p $PID > /dev/null 2>&1
then
    echo "Process is still running"
else
    echo "Process has stopped"
fi


Last edited by Chubler_XL; 03-11-2013 at 12:49 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 10  
Old 03-11-2013
Assuming the variable pid contains your pid and the user that executes the command has the right privileges:
Code:
kill -0 $pid 2>/dev/null && 
  printf '%d exists\n' $pid || 
    printf '%d does not exist\n' $pid

This User Gave Thanks to radoulov For This Post:
# 11  
Old 03-11-2013
Quote:
Originally Posted by Chubler_XL
Code:
if ps -p $PID > /dev/null 2>&1
then
    echo "Process is still running"
else
    echo "Process has stopped"
fi

In my opinion, if existence is all that's to be tested, this is by far the sanest suggestion in this thread. There's no point in generating extensive ps output only to search for something that was already known before ps was invoked (the pid).

radoulov's suggestion should be used, however, if what really matters is whether or not a process can be terminated by the effective user id running the shell script.

Regardless, there's no need for any ps | [grep | awk] pipelines.

Regards,
Alister
# 12  
Old 03-11-2013
HI u can use below idea

please use correct syntax below may not be excat sytntax but u will get and idea
Code:
c=`ps -ef | grep exactprocessname | grep -v grep|wc -l`

if [ c -gt 0 && c -lt 1 ]

echo "process is running"

elif

if [ c -gt 1]

echo "multiple instances of process running"

else
echo "processs not running"
fi


Last edited by zozoo; 03-11-2013 at 04:39 PM..
# 13  
Old 03-11-2013
Quote:
Originally Posted by zozoo
HI u can use below idea

please use correct syntax below may not be excat sytntax but u will get and idea
Code:
c=`ps -ef | grep exactprocessname | grep -v grep|wc -l`

That can't be used because what is given is the pid and not the process name. And, even if what was known were the process name, that approach leaves the door open to false positives, for example, if the process name matches a user name or an argument name, or matches some other field in the ps output. If process name were the key, pgrep is the best solution.

The suggestion in post #2 is similarly deficient.

Regards,
Alister

Last edited by alister; 03-11-2013 at 04:56 PM..
# 14  
Old 03-11-2013
Since pid is reused, it is better to look for having a log file open using fuser. The pid could exit and the new pid assigned to another process of yours that has nothing to do with the application. Check not only the pid and user but the application, at least.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check if file exists

I need to check whether a file exists and has been changed. The file should contain a specific string. The file should also have been changed within the last ten seconds. How do I do that? (3 Replies)
Discussion started by: locoroco
3 Replies

2. Shell Programming and Scripting

To check if file exists

Hi, I have the below code written. However I am not getting the desired output I am checking if the particular path has file in it. #!/bin/bash ls -l /IRS2/IRS2_ODI/INFILE/*LS* 1>/dev/null 2>/dev/null if then echo $? echo "File Exists" fi ... (3 Replies)
Discussion started by: Shanmugapriya D
3 Replies

3. Shell Programming and Scripting

Check if file exists or not

Hi, I want to check if the file exists or not in the directory. i am trying below code but not working. File="/home/va59657/Account_20090213*.dat" echo "$File" if ]; then echo "file found" else echo "file not found" fi However i am getting file not found even if file exits as... (5 Replies)
Discussion started by: Vivekit82
5 Replies

4. Shell Programming and Scripting

How to check if the URL exists?

Hi, I need to check if the URL exists. Below is my OS: SunOS mymac1 Generic_148888-04 sun4v sparc SUNW,SPARC-Enterprise-T5220 I do not have the curl set in the profile nor am i aware about its path. But i have wget. Please help me with params for the same. Can you help me check if... (6 Replies)
Discussion started by: mohtashims
6 Replies

5. UNIX for Dummies Questions & Answers

How to check if a number exists?

Hello, May i please know how do i check if the given input argument is one of the listed numbers then success else failure. I am using bash shell. if then echo "success" else echo "failure" fi Thank you. (2 Replies)
Discussion started by: Ariean
2 Replies

6. Shell Programming and Scripting

File exists, but cannot be opened.How to check- whether it could be opened to read when it exists

Hi #Testing for file existence if ; then echo 'SCHOOL data is available for processing' else echo 'SCHOOL DATA IS NOT AVAILABLE FOR PROCESSING' : i wrote a script, where it begins by checking if file exists or not. If it exists, it truncates the database... (2 Replies)
Discussion started by: rxg
2 Replies

7. Shell Programming and Scripting

check if files exists

i writing a program that checks if any .txt files exist in the current directory if it does, then it lists the files... i have got everything right, except the validation part doesnt work! .... if then ls *.txt else echo "file not found" everytime it tells me file not found!! by... (3 Replies)
Discussion started by: bshell_1214
3 Replies

8. Shell Programming and Scripting

Check to see if a file exists?

Hi. I'd like to have an IF-Then-Else statement where I can check to see if a file exists? We have the Bourne Shell by default. I'm looking for the syntax to do something like this: if myfile.txt exists then ...my code else ...my code end if Any help would be greatly... (5 Replies)
Discussion started by: buechler66
5 Replies

9. Shell Programming and Scripting

How to check if database exists?

Hi folks! First off I'm working with a Sybase DB. I'm using you're basic ISQL command to connect to my Sybase DB... isql -S$DB_SERVER -D$DB_NAME -U$DB_USR -P$DB_PWD <<!EOF > $log_file My question is, is there a way to determine if a database exists using shell script? For example, if... (2 Replies)
Discussion started by: Fatbob
2 Replies

10. Shell Programming and Scripting

How to check if a direcorty exists?

Hi Good people :D How do I check if a directory exists, if it does then carry on rest of the script, otherwise exit. ------------- cd $mainfolder/system1 #unzips files arrived in last 24 hrs into temp directory find * -mmin -1440 -exec unzip {} \; I'd like to check here if temp... (2 Replies)
Discussion started by: SunnyK
2 Replies
Login or Register to Ask a Question