LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   PID of a script file (https://www.linuxquestions.org/questions/linux-newbie-8/pid-of-a-script-file-821415/)

sajhak 07-22-2010 02:18 AM

PID of a script file
 
Hi all,

I have a script (A.sh) to start tomcat server (catalina.sh)

After executing A.sh i need to get the PID of the A.sh file.

if i do a ps -ef | grep A.sh

doesn't return anything ,

could anybody please help me to get this done

Thanks a lot
sajith

grail 07-22-2010 02:20 AM

What is in the script A.sh that keeps it running?
Remember you will only get a PID in that fashion if the script is still running or in a wait state

alli_yas 07-22-2010 02:37 AM

Post the contents of A.sh here - grail is 100% - if your script merely invokes one of the Tomcat startup scripts; your script will terminate and thus you may not see it in the ps -ef

sajhak 07-22-2010 02:59 AM

Thanks for the quick responses alli_yas and grail

the content in my A.sh script is as follows

#!/bin/sh
if [ "$1" = "run" ]; then
exec sh $CATALINA_HOME/bin/catalina.sh run

elif [ "$1" = "stop" ]; then
exec -a pse sh $CATALINA_HOME/bin/catalina.sh stop
fi


the content in catalina.sh script is the standard catalina.sh which comes with Apache Tomcat

Thanks in advance
sk

William (Dthdealer) 07-22-2010 03:27 AM

If you did not use the exec command ( which quits the script after the command is run ) you could then add a wait $(pidof catalina) at the end of the script to tell it to wait until catalina exits ( when you issue a STOP ) or until it dies for any reason ( if you issue a START ).

sajhak 07-22-2010 04:08 AM

Thanks William,

that worked, when i remove the exec command

Now I'm having some other question, that is, when i do a ps - ef | grep A.sh , it outputs the PID,
then i need to execute "cat /proc/<PID>/status" and the name of the process appear as "sh"

Is there any way that i can customise the name of the process that im getting?

Thanks in advance
sa

William (Dthdealer) 07-22-2010 05:00 AM

I'm assuming you are executing the shell script with sh, but then it will also have the process name sh.

Instead of running the script with
Code:

$ sh A.sh
Make the file executable
Code:

$ chmod u+x A.sh # You only need to do this once
And then execute it with either
Code:

$ exec A.sh
 ... or ...
$ ./A.sh # Recommended, but only if you are in the right directory
 ... or ...
$ directories/where/the/script/is/located/A.sh


sajhak 07-22-2010 05:20 AM

Cool

Thanks wiliaam and all the others.

i got my prob solved

Thanks

William (Dthdealer) 07-22-2010 05:29 AM

Quote:

Originally Posted by sajhak (Post 4041646)
Cool

Thanks wiliaam and all the others.

i got my prob solved

Thanks

You're welcome

Remember you can replace the grep and kill with this
Code:

killall A.sh
Killall takes the name of the process, unlike kill which requires the PID.


All times are GMT -5. The time now is 10:45 PM.