LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 04-14-2016, 09:11 AM   #1
Shaun@SAGA
LQ Newbie
 
Registered: Jun 2006
Location: Kent, England
Distribution: RHEL 6.7 & 71
Posts: 9

Rep: Reputation: 0
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.
 
Old 04-14-2016, 02:31 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
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"
 
1 members found this post helpful.
Old 04-18-2016, 06:02 AM   #3
Shaun@SAGA
LQ Newbie
 
Registered: Jun 2006
Location: Kent, England
Distribution: RHEL 6.7 & 71
Posts: 9

Original Poster
Rep: Reputation: 0
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.
 
Old 04-18-2016, 08:19 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,846

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
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.
 
1 members found this post helpful.
Old 04-18-2016, 10:58 AM   #5
Shaun@SAGA
LQ Newbie
 
Registered: Jun 2006
Location: Kent, England
Distribution: RHEL 6.7 & 71
Posts: 9

Original Poster
Rep: Reputation: 0
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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash - Capture "Child" script output with "Parent" without using files or coproc keyword Jason_25 Programming 2 02-14-2016 07:51 PM
[SOLVED] Killing Child "su" process also kills parent pigsa Linux - General 1 05-27-2015 11:19 PM
AIX syslog options to Supresses the "Message forwarded from <log_host_name>: " string nagendrar AIX 5 06-29-2012 09:10 AM
[SOLVED] export contacts and email back to "Windows Mail" from Ubuntu Desdd57 Linux - Newbie 21 11-02-2010 10:39 PM
Is it posible to "fork" a process and run the parent first? cdog Programming 2 02-28-2007 04:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 01:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration