How to make sure the pid file is removed when the corresponding process terminates?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I read some related posts . I tried it, but it does not work on functions defined in the script.



ssh someuser@host /path/to/scirpt.sh


the script on the remote machine:



#!/bin/sh
startup_cleanup_pid()

java SomeMainClass 1>somelog 2>&1 &
PID=$!
echo $PID > pid
wait $PID
rm pid


startup_cleanup_pid 1>/dev/null 2>&1 &
#startup_cleanup_pid & #this does not work either
echo "about to exit"
exit 0


I would like to detach the child process. How can I do that?



EDIT:
What I am trying to achieve:



1) When I used the ssh command ssh someuser@host /path/to/startup_java_process.sh and the above script, the ssh session did not terminate which I didn't know why. It just hanged there. I would like the ssh session to terminate when hitting exit 0 in that script.



2) When the java process somehow terminates, make sure the pid file is removed as well.



The link mentioned in the comment pretty much solved my problem.










share|improve this question























  • do you want java? and/or the startup_cleanup_pid in the background?
    – danblack
    Aug 31 at 5:00










  • @danblack No familiar with the background mechanism. So I will explain what I'm trying to do achieve. 1) I would like the ssh command ssh someuser@host /path/to/startup_java_process.sh to terminate, not hanging there. (I don't know why it does not terminate. I see the "about to exit" echo, but it just hangs.) 2) When the java process somehow terminates, make sure the pid file is removed as well.
    – du369
    Aug 31 at 5:08







  • 1




    (1) Please edit your post so the title and the body are asking the same question. (2) Try adding < /dev/null on both lines where you are redirecting file descriptors 1 and 2.
    – G-Man
    Aug 31 at 5:14










  • @G-Man I just tried it. But it doesn't seem to work.
    – du369
    Aug 31 at 5:29






  • 1




    stackoverflow.com/questions/14679178/…
    – Ralph Rönnquist
    Aug 31 at 6:13














up vote
0
down vote

favorite












I read some related posts . I tried it, but it does not work on functions defined in the script.



ssh someuser@host /path/to/scirpt.sh


the script on the remote machine:



#!/bin/sh
startup_cleanup_pid()

java SomeMainClass 1>somelog 2>&1 &
PID=$!
echo $PID > pid
wait $PID
rm pid


startup_cleanup_pid 1>/dev/null 2>&1 &
#startup_cleanup_pid & #this does not work either
echo "about to exit"
exit 0


I would like to detach the child process. How can I do that?



EDIT:
What I am trying to achieve:



1) When I used the ssh command ssh someuser@host /path/to/startup_java_process.sh and the above script, the ssh session did not terminate which I didn't know why. It just hanged there. I would like the ssh session to terminate when hitting exit 0 in that script.



2) When the java process somehow terminates, make sure the pid file is removed as well.



The link mentioned in the comment pretty much solved my problem.










share|improve this question























  • do you want java? and/or the startup_cleanup_pid in the background?
    – danblack
    Aug 31 at 5:00










  • @danblack No familiar with the background mechanism. So I will explain what I'm trying to do achieve. 1) I would like the ssh command ssh someuser@host /path/to/startup_java_process.sh to terminate, not hanging there. (I don't know why it does not terminate. I see the "about to exit" echo, but it just hangs.) 2) When the java process somehow terminates, make sure the pid file is removed as well.
    – du369
    Aug 31 at 5:08







  • 1




    (1) Please edit your post so the title and the body are asking the same question. (2) Try adding < /dev/null on both lines where you are redirecting file descriptors 1 and 2.
    – G-Man
    Aug 31 at 5:14










  • @G-Man I just tried it. But it doesn't seem to work.
    – du369
    Aug 31 at 5:29






  • 1




    stackoverflow.com/questions/14679178/…
    – Ralph Rönnquist
    Aug 31 at 6:13












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I read some related posts . I tried it, but it does not work on functions defined in the script.



ssh someuser@host /path/to/scirpt.sh


the script on the remote machine:



#!/bin/sh
startup_cleanup_pid()

java SomeMainClass 1>somelog 2>&1 &
PID=$!
echo $PID > pid
wait $PID
rm pid


startup_cleanup_pid 1>/dev/null 2>&1 &
#startup_cleanup_pid & #this does not work either
echo "about to exit"
exit 0


I would like to detach the child process. How can I do that?



EDIT:
What I am trying to achieve:



1) When I used the ssh command ssh someuser@host /path/to/startup_java_process.sh and the above script, the ssh session did not terminate which I didn't know why. It just hanged there. I would like the ssh session to terminate when hitting exit 0 in that script.



2) When the java process somehow terminates, make sure the pid file is removed as well.



The link mentioned in the comment pretty much solved my problem.










share|improve this question















I read some related posts . I tried it, but it does not work on functions defined in the script.



ssh someuser@host /path/to/scirpt.sh


the script on the remote machine:



#!/bin/sh
startup_cleanup_pid()

java SomeMainClass 1>somelog 2>&1 &
PID=$!
echo $PID > pid
wait $PID
rm pid


startup_cleanup_pid 1>/dev/null 2>&1 &
#startup_cleanup_pid & #this does not work either
echo "about to exit"
exit 0


I would like to detach the child process. How can I do that?



EDIT:
What I am trying to achieve:



1) When I used the ssh command ssh someuser@host /path/to/startup_java_process.sh and the above script, the ssh session did not terminate which I didn't know why. It just hanged there. I would like the ssh session to terminate when hitting exit 0 in that script.



2) When the java process somehow terminates, make sure the pid file is removed as well.



The link mentioned in the comment pretty much solved my problem.







linux shell ssh






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 31 at 9:29

























asked Aug 31 at 4:54









du369

1012




1012











  • do you want java? and/or the startup_cleanup_pid in the background?
    – danblack
    Aug 31 at 5:00










  • @danblack No familiar with the background mechanism. So I will explain what I'm trying to do achieve. 1) I would like the ssh command ssh someuser@host /path/to/startup_java_process.sh to terminate, not hanging there. (I don't know why it does not terminate. I see the "about to exit" echo, but it just hangs.) 2) When the java process somehow terminates, make sure the pid file is removed as well.
    – du369
    Aug 31 at 5:08







  • 1




    (1) Please edit your post so the title and the body are asking the same question. (2) Try adding < /dev/null on both lines where you are redirecting file descriptors 1 and 2.
    – G-Man
    Aug 31 at 5:14










  • @G-Man I just tried it. But it doesn't seem to work.
    – du369
    Aug 31 at 5:29






  • 1




    stackoverflow.com/questions/14679178/…
    – Ralph Rönnquist
    Aug 31 at 6:13
















  • do you want java? and/or the startup_cleanup_pid in the background?
    – danblack
    Aug 31 at 5:00










  • @danblack No familiar with the background mechanism. So I will explain what I'm trying to do achieve. 1) I would like the ssh command ssh someuser@host /path/to/startup_java_process.sh to terminate, not hanging there. (I don't know why it does not terminate. I see the "about to exit" echo, but it just hangs.) 2) When the java process somehow terminates, make sure the pid file is removed as well.
    – du369
    Aug 31 at 5:08







  • 1




    (1) Please edit your post so the title and the body are asking the same question. (2) Try adding < /dev/null on both lines where you are redirecting file descriptors 1 and 2.
    – G-Man
    Aug 31 at 5:14










  • @G-Man I just tried it. But it doesn't seem to work.
    – du369
    Aug 31 at 5:29






  • 1




    stackoverflow.com/questions/14679178/…
    – Ralph Rönnquist
    Aug 31 at 6:13















do you want java? and/or the startup_cleanup_pid in the background?
– danblack
Aug 31 at 5:00




do you want java? and/or the startup_cleanup_pid in the background?
– danblack
Aug 31 at 5:00












@danblack No familiar with the background mechanism. So I will explain what I'm trying to do achieve. 1) I would like the ssh command ssh someuser@host /path/to/startup_java_process.sh to terminate, not hanging there. (I don't know why it does not terminate. I see the "about to exit" echo, but it just hangs.) 2) When the java process somehow terminates, make sure the pid file is removed as well.
– du369
Aug 31 at 5:08





@danblack No familiar with the background mechanism. So I will explain what I'm trying to do achieve. 1) I would like the ssh command ssh someuser@host /path/to/startup_java_process.sh to terminate, not hanging there. (I don't know why it does not terminate. I see the "about to exit" echo, but it just hangs.) 2) When the java process somehow terminates, make sure the pid file is removed as well.
– du369
Aug 31 at 5:08





1




1




(1) Please edit your post so the title and the body are asking the same question. (2) Try adding < /dev/null on both lines where you are redirecting file descriptors 1 and 2.
– G-Man
Aug 31 at 5:14




(1) Please edit your post so the title and the body are asking the same question. (2) Try adding < /dev/null on both lines where you are redirecting file descriptors 1 and 2.
– G-Man
Aug 31 at 5:14












@G-Man I just tried it. But it doesn't seem to work.
– du369
Aug 31 at 5:29




@G-Man I just tried it. But it doesn't seem to work.
– du369
Aug 31 at 5:29




1




1




stackoverflow.com/questions/14679178/…
– Ralph Rönnquist
Aug 31 at 6:13




stackoverflow.com/questions/14679178/…
– Ralph Rönnquist
Aug 31 at 6:13















active

oldest

votes











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f465918%2fhow-to-make-sure-the-pid-file-is-removed-when-the-corresponding-process-terminat%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f465918%2fhow-to-make-sure-the-pid-file-is-removed-when-the-corresponding-process-terminat%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)