hanish
hanish

Reputation: 67

Exiting a shell script running on the host

this is my script code that I launch from machine 1 to machine 2 (remotely)

 #!/bin/bash
 nohup perl server.pl &
 echo "script ends"
 exit

the file server.pl runs in a while(1) kind of loop listening for sockets forever.

the problem I am facing now is : as i launch this shell script remotely after i invoke this script i have other work to be done inside the file which launches this shell script. eg:

#this script launches the shell script 
$cmd = "sabkuch.sh";
$ssh->system($cmd);#using Openssh perl package to remotely run commands
#do some other processing
print "Welcome to USA\n";

When running the above code I am unable to print statement until i give a Ctrl-C.

If i launch this shell script on machine 2 itself ( without remotely launching it from machine 1) i get the desired result, i don't know where i am going wrong, what could be the problem?

PS : i have commented all the print statements in shell script ( to avoid some stdout problems bringing the process to fg again inspite of & )

Upvotes: 0

Views: 87

Answers (1)

mbethke
mbethke

Reputation: 935

That's not a problem in Perl or SSH, it's the way you use nohup. The script will hang just the same if you call it as

ssh host2 ./sabkuch.sh

nohup only truly detaches to let the calling ssh process terminate if you redirect stdout/stderr like this:

nohup perl server.pl >/dev/null 2>&1 &

Upvotes: 1

Related Questions