Reputation: 1225
I am trying to use a script to start a nohup command. I have written a test program in Java to launch via the script, TestScript.java. My script contains the following:
#!/bin/bash
cd /home/user/ejava
nohup java TestScript > TestScript.out &
echo $! > /home/user/ejava/TestScript.pid
I use echo $! to get the last process ID and store it to a temp file. When I look in the TestScript.pid, the value is 37458.
When I run a 'ps -elf | grep TestScript', I get:
0 S user 37458 1 0 80 0 - 1065 rt_sig 11:51 pts/0 00:00:00 /bin/ksh /folder/tools/Linux_x86_64/bin/java TestScript
0 S user 37463 37458 0 80 0 - 8813549 futex_ 11:51 pts/0 00:00:00 /folder/tools/Linux_x86_64/bin/../java/jre_Linux_x86_64/bin/java TestScript
0 S user 37516 36224 0 80 0 - 1595 - 11:52 pts/0 00:00:00 grep TestScript
If I kill job 37458, then 37463 is still running and my output from the nohup is still being updated. The job is not killed.
How can I get the process id 37463 returned (or whatever correlates to the command with futex in it) so I can store it in a file and later use it to kill the process with another script? I would prefer not to have to search for the process ID but rather get it returned by some process.
Upvotes: 0
Views: 7667
Reputation: 2777
If you start your process in the background using the ampersand character at the end of your command then the shell variable ! is populated with the pid you are looking for:
john@ubuntu:~$ nohup java -cp . Tst > tst.out 2>&1 &
[1] 6901
john@ubuntu:~$ echo $!
6901
john@ubuntu:~$ ps -ef | grep java
john 6901 4844 0 09:27 pts/12 00:00:00 java -cp . Tst
john 6913 4844 0 09:27 pts/12 00:00:00 grep --color=auto java
john@ubuntu:~$
I found it here.
Upvotes: 0