Quote:
Originally Posted by jhonnappier2007
Say, i am starting tomcat from startup.sh , Here i need the pid of the tomcat process not the startup.sh script
|
Welcome!
Here you go:
I have a job/script/process whatever you call it:
myJob.sh
It does its work just as tomcat or httpd or sshd would do their work. Okay this far?
But let me tell you what it does:
[root@localhost ~]# cat /tmp/myJob.sh
Code:
#!/bin/bash
for count in `seq 1 3600`; do
echo $count
sleep 1
done
exit 0
Okay, let us run it in the background from the command prompt:
Code:
[root@localhost ~]# /bin/bash /tmp/myJob.sh &> /dev/null &
[1] 3612
So, we already have one instance of the job/script we just executed above.
Now, let's create more PIDs of it by executing the same from within a script - that is what you really want to do.
So, here is the script to run the above script in the background:
[root@localhost ~]# cat procTrack.sh
Code:
#!/bin/bash
/bin/bash /tmp/myJob.sh &> /dev/null &
echo $! >> my_pids.log
echo "PID of myJob.sh is: $! - Saved to my_pids.log"
exit 0
Now let's run the above script:
Code:
[root@localhost ~]# ./procTrack.sh
PID of myJob.sh is: 3641 - Saved to my_pids.log
Okay, so how many PIDs have we got by now?
Code:
[root@localhost ~]# ps aux | grep myJob.sh | grep -v grep
root 3612 0.0 0.2 5152 1312 pts/1 S 09:12 0:00 /bin/bash /tmp/myJob.sh
root 3641 0.0 0.2 5152 1308 pts/1 S 09:12 0:00 /bin/bash /tmp/myJob.sh
Okay, I know you (I too) forgot what the previous PID was. So, let's not worry because we saved the second one in a file:
Code:
[root@localhost ~]# cat my_pids.log
3641
Can you see your loved one above?
You can edit "procTrack.sh" and say something like this:
Code:
echo "Process myJob.sh Executed at `date` with the PID: $!" >> myPIDs.log
That way you will also have a record of process execution and associated PID. Later on you can simply say:
Code:
grep 'myJob.sh' myPIDs.log
and you will have a good track of your jobs/PIDs.
Here it is:
Code:
[root@localhost ~]# cat procTrack.sh
#!/bin/bash
/bin/bash /tmp/myJob.sh &> /dev/null &
echo "MyJob.sh Executed at `date` with the PID of: $!" >> my_PIDs.log
exit 0
[root@localhost ~]#
Code:
[root@localhost ~]# ps aux | grep myJob.sh | grep -v grep
root 5138 0.0 0.2 5152 1308 pts/1 S 09:32 0:00 /bin/bash /tmp/myJob.sh
[root@localhost ~]# ./procTrack.sh
[root@localhost ~]# ps aux | grep myJob.sh | grep -v grep
root 5138 0.0 0.2 5152 1308 pts/1 S 09:32 0:00 /bin/bash /tmp/myJob.sh
root 5452 0.0 0.2 5152 1308 pts/1 S 09:33 0:00 /bin/bash /tmp/myJob.sh
[root@localhost ~]# ./procTrack.sh
[root@localhost ~]# ps aux | grep myJob.sh | grep -v grep
root 5138 0.0 0.2 5152 1308 pts/1 S 09:32 0:00 /bin/bash /tmp/myJob.sh
root 5452 0.0 0.2 5152 1308 pts/1 S 09:33 0:00 /bin/bash /tmp/myJob.sh
root 5470 0.0 0.2 5152 1308 pts/1 S 09:34 0:00 /bin/bash /tmp/myJob.sh
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# cat my_PIDs.log
MyJob.sh Executed at Tue Nov 15 09:32:48 EST 2011 with the PID of: 5138
MyJob.sh Executed at Tue Nov 15 09:33:58 EST 2011 with the PID of: 5452
MyJob.sh Executed at Tue Nov 15 09:34:03 EST 2011 with the PID of: 5470
I hope, I have done my best to help you.
Anyways, cheers!