run command in background

Jan 19, 2012 at 9:47 AM

Hi,

I am trying to do the following. Run a shell script that set some variables, and after that runs another script (a service) in the background.

My code is:
ssh.Connect()
ssh.RunCommand("test.sh")
ssh.Disconnect()

=== test.sh ===
var1 = x
./service.sh &

=== service.sh ===
while (true); do
    echo "service running"
    ...
done

The problem is that RunCommand() doesn't ends until i kill "service.sh". Why? I run "test.sh" with RunCommand(), and that scripts end normally. The "service.sh" is detachend from it and sent to background.

Please help!
Regards Peter.

Jan 25, 2012 at 6:27 AM

Could someone answer to this???

Coordinator
Jan 25, 2012 at 1:32 PM

Hi,

 

Sorry that you nobody reply to this problem yet. I personally not a UNIX person so dont really know what might be a problem.

All RunCommand does is passing the command to the default shell for execution, waits for it to finish and then exits.

 

I guess what you can try to do is to execute command asynchronously and see if helps you.

 

Thanks,

Oleg

Jan 25, 2012 at 4:27 PM

Could it be that service.sh is still writing output to the shell, so RunCommand continues to listen until the shell is closed?

To test this theory, try something like this:

 

## test.sh

var1 = x
./service.sh > /dev/null &

Jan 27, 2012 at 7:19 AM

Thanks for the help guys!

The asynchronous execution solved the problem. I don't understand why, becouse if I do the same with a normal ssh client (eg. putty), then the script ends normally and I can close the shell too.

The redirect to /dev/null dosen't works. Even if the "service.sh" dosen't writes to the console output, the connection still hangs.

Thank again!
Regards Peter.