LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-11-2011, 11:50 AM   #1
jerunh
LQ Newbie
 
Registered: Mar 2011
Location: Atlanta, Ga
Posts: 7

Rep: Reputation: 0
How to run a program remotely using a custom environment?


{ Summary }
I am trying to to execute multiple programs on multiple remote machines. Each program needs to be run in a custom Environment that is not known at login time. Currently I am using ssh to connect and run the program.

My issue is that I'm not sure how setup all the Environment Variables that I need on the remote machine. I was able to write all the environment variables/values (NAME=VALUE) that I needed to a file and place on a shared file server (prog.environment).

I thought the best way to approach this would be to write my own utility (custom.shell) that would interpret the environment file and then execute the desired command.

{ custom.shell }
Code:
#!/bin/tcsh

# determine absolute path of program
set PROG=`readlink -m $0`

set CUR_DIR = $PROG:h
set DONE = 0
# Find Project Root Directory
while ($DONE == 0)
    if (-e $CUR_DIR/docs/license.txt ) then
        set PROJ = $CUR_DIR
        set DONE = 1
    else if ($CUR_DIR == "") then
        echo "Error: Not a project directory."
        exit
    else 
        set CUR_DIR = $CUR_DIR:h
    endif
end

while ($#argv > 0)
    set arg = $argv[1];
    if ($arg == "--envfile" then
        shift
        if ($#argv >= 1) then
            set ENV_FILE = $argv[1]
            
            # Make sure the file exists
            if (-e $ENV_FILE) then
                
                # tmpchar is needed so that the foreach process a 
                # whole line at a time. Damn you csh!
                set tmpchar = "~"
                foreach TMPLINE (`cat $ENV_FILE | tr " " "$tmpchar"`)
                    set LINE = `echo $TMPLINE | tr "$tmpchar" " " | sed 's/=/ /'`
                    if ($LINE != "") then
                        set ENV_NAME = `echo $LINE | cut -d' ' -f1`
                        set ENV_VAL  = `echo $LINE | cut -d' ' -f2-`
                        setenv $ENV_NAME $ENV_VAL
                    endif
                end
            else
                echo "Error: $ENV_FILE does not exist."
                exit
            endif
        else
            echo "Error: Filename not provided"
        endif

    else if ($arg == "-c") then
        shift
        
        if ($#argv >= 1) then
            set COMMAND="$ARG"
        else
            echo "Error: No command specified"
            exit
        endif
    else
        echo "Error: Unknown argument: $arg"
    endif
    
    shift
end

#Execute the Command
echo "DEBUG: $COMMAND"
$COMMAND

{ Invocation }
Code:
ssh myserv4 -n "/path/to/my/project/scripts/custom.shell --envfile /path/to/my/envfile/env.file -c sh -c 'set -a;DISPLAY=localhost:10.0;RELEASE=;/path/to/my/executable/bin/start.sh 0 1 222 -cpu 3'" &

{ Result }
My ssh session seems to be operating correctly. My custom.shell script seems to be getting called correctly. The custom.shell script seems to be producing the command that I want correctly with the exception that the single quotes are getting stripped out.

{ Output }
Code:
    DEBUG: sh -c set -a;DISPLAY=localhost:10.0;RELEASE=;/path/to/my/executable/bin/start.sh 0 1 222 -cpu 3
{ Questions }
  1. How do I get my single quotes so that the sh command can interpret the whole -c command?
  2. Is there a better way to accomplish this? I feel like I'm riding a Go-Kart on a cross country trip. There has got to be a better way!
 
Old 06-11-2011, 12:42 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by jerunh View Post
... with the exception that the single quotes are getting stripped out. ...
Change the approach. I.e. on your job launching machine generate a wrapper script in such a manner it has all the environment variable settings and the whole actual command line inside, 'scp' the script to target machine and run it from where.

And, by the way, 'tcsh' is buggy - I saw instances of various versions of 'tcsh' on different machines in the pool executing the same script in a different manner - POSIX-compatible shells seem to be much more robust in that respect.
 
1 members found this post helpful.
Old 06-12-2011, 04:31 AM   #3
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
It should work to escape another set of quotes:
Code:
ssh -n "fubar \"'foobar'\""
But do you need the outer ones at all? I could imagine that:
Code:
ssh myserv4 -n /path/to/my/project/scripts/custom.shell --envfile /path/to/my/envfile/env.file -c sh -c "'set -a;DISPLAY=localhost:10.0;RELEASE=;/path/to/my/executable/bin/start.sh 0 1 222 -cpu 3'" &
could work too.
 
0 members found this post helpful.
Old 06-12-2011, 06:20 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Reuti View Post
It should work to escape another set of quotes:
Code:
ssh -n "fubar \"'foobar'\""
But do you need the outer ones at all? I could imagine that:
Code:
ssh myserv4 -n /path/to/my/project/scripts/custom.shell --envfile /path/to/my/envfile/env.file -c sh -c "'set -a;DISPLAY=localhost:10.0;RELEASE=;/path/to/my/executable/bin/start.sh 0 1 222 -cpu 3'" &
could work too.
One should remember that different types of shells have different quoting rules. That's why in the first place I suggested to write a wrapper script - in such a case the shell and therefore its quoting rules is known beforehand.
 
1 members found this post helpful.
Old 06-13-2011, 02:12 PM   #5
jerunh
LQ Newbie
 
Registered: Mar 2011
Location: Atlanta, Ga
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Sergei Steshenko View Post
Change the approach. I.e. on your job launching machine generate a wrapper script
Thanks. This worked for me. Albeit Ugly.
 
Old 06-14-2011, 09:57 AM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by jerunh View Post
Thanks. This worked for me. Albeit Ugly.
I used to work in a mixture of AIX, HPUX, Solaris in the same pool - with different default login shells and different versions of them. So the approach is a radical solution for all these potential incompatibilities/variations.

...

In an ancient Japanese woman author book (translated into Russian) I once read: "What can be more ugly than how a cat's ear looks inside ?!". I actually like staring into cats' ears ...
 
  


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
How to map or mount the whole developing environment remotely? ggyyree Linux - Networking 1 01-09-2011 09:00 PM
[SOLVED] Xwindow's program will not run when executed on boot or when executed remotely richman1234 Programming 2 10-08-2010 01:32 PM
X window remotely environment alaios Linux - Networking 1 10-16-2006 02:48 PM
Starting program remotely and have them continue to run after the session is ended OJAtkinson Linux - General 2 12-07-2004 12:06 PM
Custom user environment RHrulz Linux - Software 3 09-16-2003 07:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:16 PM.

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