Capturing the Process ID of a process


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Capturing the Process ID of a process
# 1  
Old 05-16-2007
Capturing the Process ID of a process

hi Everybody

I am trying to capture process id of a process which has been started through the execution of a shell script

Code:
CLASSPATH=.
CLASSPATH=${CLASSPATH}:sampleswing.jar
java -cp ${CLASSPATH} Sample
echo $! > pid.sh

above is the simple script file called swings.sh

When i execute swings.sh , script internally creates another java process.
I need the process id of the newly created java process and not the process id of the shell script.

Actually when i execute shell script, java process does not return until the java application is closed thru the application's exit button.so the command next to " java -cp ${CLASSPATH} Sample " does not get executed until the invoked java process is closed thru application's exit button.

In this case how can i get the process id of the java process simultaneoulsy when executing the shell script. (something like... java process should be daemon process and next line should be executed once the java process is started)

Suppose if i execute following command alone independently after running the above script without the last line in a different console , I get the process id of the newly created java process.
Code:
echo $!

My requirement is to execute shell script as well as get the process id of the java process on a single run of the shell script.

Kindly shed some light on getting the process id of the java process when executing the script.

I am storing this PID in another file for the very reason ,when java application hangs up, sometimes even after pressing ctrl+C, the process is not exiting
# 2  
Old 05-16-2007
$! is the process ID of the last command run in the background not the last command run. So if you do:

Code:
java -cp ${CLASSPATH} Sample &

Then it should work. Either that or change your script to:

Code:
CLASSPATH=.
CLASSPATH=${CLASSPATH}:sampleswing.jar
echo $$ > pid.sh
exec java -cp ${CLASSPATH} Sample

The effect of the exec command is to cause the command after it to be run in place of the current shell, but no new process is created. Thus it will have the same process ID as the shell it is run from.
# 3  
Old 05-16-2007
Thanks a lot Unbeliver
Ur first command worked out to be suitable for my requirement.
Now comes another problem, when i execute below script

Code:
CLASSPATH=.
CLASSPATH=${CLASSPATH}:sampleswing.jar
java -cp ${CLASSPATH} Sample &
echo "kill -9 "$! > killer.sh
chmod 777 killer.sh

output of the application "Sample" is getting displayed on the console from where i have executed the shell script.

Kindly help me in not directing the output and error to the console from where i have started the shell script.i do not want to handle those logs ... it can also be made nullified.

I also tried nohup command before
Quote:
java -cp ${CLASSPATH} Sample &
In this case, all logs were transferred to some file called nohup_out ...even i do not want that to happen beacuse internally i have used log4j to redirect my logs to some other files in another directory and also some error logs are still directed to the console from where i started the shell script.

Reply wud be very much appreciated
# 4  
Old 05-16-2007
If I read your reply correctly, I think you want to throw away all the output, not have it written to nohup.out.

If you use nohup then the only way to stop it creating a nohup.out file is to make sure you redirect both stdout and stderr to somewhere else. So for instance if your script was called 'run_sample.sh' you would use:

Code:
nohup ./run_sample.sh > /dev/null 2>&1 &

If this is not what you want then please explain again.
# 5  
Old 05-16-2007
Once again Thanks very much unbeliver

my requirement is to throw away the logs sent to the stdout and stderr .It seems it is working with ur command.

can u please explain the whole command as what you are tyring to do with this command with the split of each part in that command.

Quote:
nohup ./run_sample.sh > /dev/null 2>&1 &
In the above u are directing to /dev/null ... means what
2>&1 ... means what and also why & infront of 1

& .... to make the entire operation to be background process
Basically, run_sample.sh is going to contain the following commands
Code:
CLASSPATH=.
CLASSPATH=${CLASSPATH}:sampleswing.jar
java -cp ${CLASSPATH} Sample &
echo "kill -9 "$! > killer.sh
chmod 777 killer.sh

already i am using '&' inside shell script to make the java process run as background.

Do we still need to make the shell script run as background in order to achieve whatever i said .. basically i want final command that can go for prod enviroment.

please do give me final and consolidated command ....

Thanks once again and i very much appreciate
# 6  
Old 05-16-2007
Code:
nohup ./run_sample.sh > /dev/null 2>&1 &

The two '&' characters have very different meanings. Let me go through the line bit by bit.

nohup: This command allows you to run other commands in such a way that they ignore the SIGHUP signal. Most importantly this means if you exit the shell that they are started from they *do not* exit (ie. if youlogout from the system they continue to run).

./run_sample.sh: This, of course, is your script.

> /dev/null: '>' is short for '1>' and tells the shell to redirect file descriptor 1 (standard out). In this case it is redirected to /dev/null (ie. it it is thrown away).

2>&1: This is another command to the shell to redirect another file descriptor. In this case it tells the shell to redirect file descriptor 2 (thats the '2>' bit) which is standard error, to file descriptor '1' (thats the '&1' bit), File descriptor 1 has already been pointed at /dev/null. So all output to standard error goes to /dev/null as well.

&: At the end of the line this indicates to the shell that the process should be run in the background

Please note the order is important.
Code:
my_command > /dev/null 2>&1
  and
my_command 2>&1 > /dev/null

Have very different results. I'll leave you to work out the difference Smilie

If you wish just the java process to run uninteruptted in the background then you can just move the 'nohup inside the shell script:

Code:
CLASSPATH=.
CLASSPATH=${CLASSPATH}:sampleswing.jar
nohup java -cp ${CLASSPATH} Sample > /dev/null 2>&1 &
echo "kill -9 "$! > killer.sh
chmod 777 killer.sh

# 7  
Old 05-17-2007
Excellent explaination !!!!

Thanks a lot for ur replies, unbeliver

Now shell script have got some good shape in executing those java process.
Next concern is , i want to do preliminary check before executing java process inside the shell script ...
Code:
CLASSPATH=.
CLASSPATH=${CLASSPATH}:sampleswing.jar
nohup java -cp ${CLASSPATH} Sample > /dev/null 2>&1 &
echo "kill -9 "$! > killer.sh
chmod 777 killer.sh


Preliminary check is necessary to make sure that any already spawned java process are killed completely before starting another process with the shell script. For this purpose only , i have stored the process ID of the java process in a file called Killer.sh which contains command
Quote:
kill -9 pid
Basically i want to execute killer.sh as a first command before executing
Quote:
nohup java -cp ${CLASSPATH} Sample > /dev/null 2>&1 &
Suppose for the first time , if there is no killer.sh available , then there should not be any problem when i execute it.

Kindly get me a code that can be included before executing
Quote:
nohup java -cp ${CLASSPATH} Sample > /dev/null 2>&1 &
which kills the already running java process, also it indirectly make sure that only one java process can be run at any time.Code construct also shud handle if that file 'killer.sh' is not available.

Reply wud be very much appreciated
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Monitoring processes in parallel and process log file after process exits

I am writing a script to kick off a process to gather logs on multiple nodes in parallel using "&". These processes create individual log files. Which I would like to filter and convert in CSV format after they are complete. I am facing following issues: 1. Monitor all Processes parallelly.... (5 Replies)
Discussion started by: shunya
5 Replies

2. Shell Programming and Scripting

Capturing the return code from background process

Hi All, I was out not working on unix from quite sometime and came back recently. I would really appreciate a help on one of the issue I am facing.... I am trying to kick off the CodeNameProcess.sh in PARALLEL for all the available codes. The script runs fine in parallel. Let say there are... (1 Reply)
Discussion started by: rkumar28
1 Replies

3. Shell Programming and Scripting

Capturing the killed process logs

I have two set of questions. 1) To skip killing some process automatically. 2) To kill other process and capture their log. I have set of process, some needs to be killed gracefully and others should be skipped. Listed are the process. adm 1522... (1 Reply)
Discussion started by: murali1687
1 Replies

4. Shell Programming and Scripting

Capturing PIDs of same process at different instances

Hi, I'm gonna launch a process from my 'C' code. I'm gonna launch it a few times. I would like to capture the PID of that process each time I launch. I have to copy the each PIDs into a 'C' variable and I have to kill all of them when I exit from the 'C' code. My requirement is int... (3 Replies)
Discussion started by: suryaemlinux
3 Replies

5. Shell Programming and Scripting

Capturing the CPU% used by a process

Hi, I just wonder I need to write a script where I can check if a particular process is consuming X amount of CPU. I was thinking of using the ps command but doesn't seems to work. Any ideas. Thanks. (2 Replies)
Discussion started by: arizah
2 Replies

6. Shell Programming and Scripting

Capturing running process name

i'm looking to have my script capture it's own process name while running. i'm going to use this in the output of the script to track which script produced which output file(s). when i run: ps -ef | grep processname i only get as results a ps -ef listing for the grep inside my... (6 Replies)
Discussion started by: danmauer
6 Replies

7. Shell Programming and Scripting

Shell Script to Kill Process(number of process) Unix/Solaris

Hi Experts, we do have a shell script for Unix Solaris, which will kill all the process manullay, it used to work in my previous env, but now it is throwing this error.. could some one please help me to resolve it This is how we execute the script (and this is the requirement) ... (2 Replies)
Discussion started by: jonnyvic
2 Replies

8. AIX

Capturing Process on AIX boxes - IMP

Guys we all know what command 'COLUMNS=2047 /usr/bin/ps –eo pid,ppid,uid,user,args' does.It prints 5-column output for the running processes on a AIX box. Here is simple thing i need: I need to insert this tabular data in a db2 table. How do i need? I have created table with these five... (0 Replies)
Discussion started by: ak835
0 Replies

9. Shell Programming and Scripting

script to monitor process running on server and posting a mail if any process is dead

Hello all, I would be happy if any one could help me with a shell script that would determine all the processes running on a Unix server and post a mail if any of the process is not running or aborted. Thanks in advance Regards, pradeep kulkarni. :mad: (13 Replies)
Discussion started by: pradeepmacha
13 Replies

10. Shell Programming and Scripting

capturing process id

I am newbie to unix shells world. I am trying to capture a background process id into a file so that it can be killed later. this process is basically a java program running in background as: java TestApp & this returning process id immediately. So how can i redirect that pid into a file.... (1 Reply)
Discussion started by: bvreddy
1 Replies
Login or Register to Ask a Question