LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Can I "export" a message string back to a parent process on another server? (https://www.linuxquestions.org/questions/linux-server-73/can-i-export-a-message-string-back-to-a-parent-process-on-another-server-4175577446/)

Shaun@SAGA 04-14-2016 09:11 AM

Can I "export" a message string back to a parent process on another server?
 
Hello fellow Linux users,

I have a bash script that controls the refresh of an application environment. This consists of a set of web application services and two databases. The apps and databases can be (and usually are) on different servers, so the main script has to initiate different parts of the refresh using ssh; with system users set up with ssh-keygen and in some cases sudo authority.

I would like to handle alerting via the main script, so have a requirement for the child scripts being run on remote servers to be able to return a line of text to the parent. This is simple, using export, on a local machine but I am struggling rather getting it to work across ssh.

So;

Script 1 (Parent)
CHILD=/tmp/test_scripts/child.sh
echo "Running Child Process, $CHILD"
. /$CHILD
RETVAL=$?
echo "Back in the main script now"
echo "Script ended with return value = $RETVAL"
echo "Message parsed back;"
echo $ERROR


Script 2 (Child)
echo "Doing something"
echo "Doing something"
echo "breaking now"
export ERROR="It went bang and made my ears ring"
return 4


Output
Running Child Process, /tmp/test_scripts/child.sh
Doing something
Doing something
breaking now
Back in the main script now
Script ended with return value = 4
Message parsed back;
It went bang and made my ears ring


Does anyone have an idea how this might work when the child script is on another server??

Thanks in advance.

smallpond 04-14-2016 02:31 PM

When you issue a command via ssh, the command output from the remote system appears as output on the local system. So:

Code:

msg=$(ssh user@remote command)
echo "Script returned: $msg"


Shaun@SAGA 04-18-2016 06:02 AM

Working with this.
 
Thanks smallpond, it's something I can work with. Hadn't occurred to me to do it that way.

Only downside is that I was getting standard non-error output to the console from the remote job; issuing the ssh within a variable definition is hiding that now. I think the operator who runs this finds that output quite comforting, to know all the scripts are doing what they are supposed to do!

I guess I can get around that by echoing the whole output to the screen from the parent script once the remote child has ended. Just need to get round the fact the variable lumps individual lines together into one long string, not sure how yet but it can't be that difficult.

I can handle the alerting of an error by doing something like {echo "ERROR: This went wrong"} in the child and then awking out using the "ERROR:" label in the parent and handling it from there.

I might just leave this open for a couple more days, in case anyone has any other suggestions.

Thank you.

pan64 04-18-2016 08:19 AM

when you use the syntax:
Code:

. <path>/script.sh  #that is so called sourced script

CHILD=/tmp/test_scripts/child.sh
echo "Running Child Process, $CHILD"
. /$CHILD

the script will not run in a child process, but will be executed by the same shell. Therefore the variable ERROR
(export ERROR="It went bang and made my ears ring") will be visible in that shell (because it is not another one, not a parent process)
You cannot execute a remote script sourced, it is simply impossible (the same process cannot run on both hosts).

Also you can try the following:
ssh user@host script >/script.out 2>script.stderr
and you can handle the two files differently.

Shaun@SAGA 04-18-2016 10:58 AM

Thank you "pan64",

Excellent. This does indeed do exactly what I'm trying to achieve. Didn't think the error output channel would work cross servers and hadn't even thought to try it to be honest!

For anyone who picks up on this, the missing part of the puzzle is that the test you want to be handled by the parent script needs to be piped out to the error channel (2), like this;

echo "It went bang and made my ears ring" >2

Then run the ssh as indicated by the previous post to capture any error output into a file and then manipulate if from there.

Both great answers to be implemented in different ways. Will close this off now as answered.

Thanks again!


All times are GMT -5. The time now is 10:42 AM.