Execute bash script on a remote-linux - linux

I have a bash script lets say test.sh. This script contains the following:
#!/bin/bash
echo "khaled"
ads2 svcd&
This script simply prints my name (just for test purposes) and execute ads-service application in the background. When i run the script on my ubuntu, it works correctly. As a test i checked which programs run on the kernel
As you see. ads2 runs and has 12319 process-id.
Now what I'm trying to do is to run the script on the ubuntu, however remotely from a windows pc.
Therefore i opened command-line on windows and executed the following command:
ssh nvidia#ubuntu ip-address ~/test.sh
And i get the following
As you see the scripts run and prints khaled,however on windows command line and what i want is that the script is executed on the ubuntu. this justify why the lineads2 svcd& doe not do anything, neither on windows (which makes sense, since ads2 is installed on ubuntu) nor on linux.
So how can i execute the script on ubuntu ?
thanks in advance

Use the full path to start ads2. When using remote SSH your environment variables may be different than in a local shell.
#!/bin/bash
echo "khaled"
/home/nvidia/ads2 svcd&
Not sure where ads2 is located.
Try the following to locate it on your Ubuntu local shell.
command -v ads2
You may also need nohup to persist the process beyond the life of the SSH session.

If you have the script on the remote server and you want to run this, you would add back ticks,
ssh user#server './test/file.sh'
The script's output would be sent to your local machine, as if you ran the command from your local machine.

Related

Using ssh to login to linux terminal from windows and run command in a logged in shell

First of all, this may seem like a duplicate question but I have searched stack overflow/various other forum sites and still haven't managed to find a solution.
A few example forum posts I have reviewed to prove I've done my research before asking a question:
https://superuser.com/questions/130443/remotely-run-script-on-unix-get-output-locally
https://linuxconfig.org/executing-commands-remotely-with-ssh-and-output-redirection
https://zaiste.net/posts/few-ways-to-execute-commands-remotely-ssh/
https://unix.stackexchange.com/questions/474533/get-output-of-this-command-from-another-server-via-ssh
Run ssh and immediately execute command
https://www.cyberciti.biz/faq/unix-linux-execute-command-using-ssh/
There's hundreds more but I won't include them all.
I essentially need a shell script to open a command prompt on windows, login to a remote linux system and run a command.
I am aware this can be done with the following:
start cmd /k ssh user#host ls
But the problem with the above is that the ssh connection is closed upon completion of the task.
I am also aware I can keep the ssh connection open by adding:
bash -l
in some cases.
For my use case, I need to run a launch file for ROS (robot operating system) and for this I need to see the output from the command.
And when attempting to run roslaunch launchFile.launch (in place of ls above):
start cmd /k ssh user#host "roslaunch launchFile.launch"
the command prompt returns
bash: roslaunch: command not found
I've obviously sanitised the specific name of my launch file but
roslaunch launchFile.launch
runs perfectly if I login to the linux PC first:
ssh user#host
then run the command.
I have achieved this exact use case on MacOS but I now need reimplement the same solution on windows:
osascript -e 'tell app "Terminal"
do script "ssh quantum#172.23.199.1 \n
roslaunch launchFile.launch"
end tell'
Thanks in advance for any help or advice.
Try this :
start cmd /k ssh user#host "/full/path/to/roslaunch launchFile.launch; exec /bin/bash"

Bash Script to start a process on a remote Linux Machine and keep it detached from the starting server

I'm new to Linux (Red Hat) and I'm trying to automate the Eggplant tests I've come up with for our GUI based software. This will be run nightly.
I'll be running the base script on server 001. It will copy over the latest version of our software to a remote PC which is serving as test bench then it's to kick off a Bash script on the test bench which configures the environment then starts up the software.
I've tried:
ssh test#111.111.111.002 'bash -s' < testConfig.sh
ssh test#111.111.111.002 'bash -s testConfig.sh < /dev/nul > testConfig.log 2>&1 &'
ssh -X test#111.111.111.002 'testConfig.sh'
First one just fails, the second tries to start the software but instead of running on the test bench it tries running on the server. Of course with the third one it opens up windows on the server and runs the software; but I need the display on the test bench not the server.
A coworker happened by and saw my difficulty and suggested this, and it worked:
ssh user#1.1.1.1 'setenv DISPLAY :0.0 && testConfig.sh'
Try below and let us know. Using double quotes instead of single quotes.
ssh user#1.1.1.1 "bash -s" < ./testConfig.sh
make sure testConfig.sh is in the directory from where you are ssh'ing else use absolute path name for e.g. /home/qwe/testConfig.sh
Please also mention the error you are getting if any.

Linux: Run a shell command remotely and print result

I want to create a script that runs shell commands on a remote Linux machine and print out the result. Some thing like a "run once" SSH. Or for those familiar with Android development, something like "adb shell". For example, I want to run "ls" on my remote machine and display the results on the local host.
This is supported directly by ssh:
ssh [options] [user#]hostname [command]
eg
ssh user#host ls
If not command is given an interactive shell is usually run by default.

Execute Remote Perl Script on a Windows Box from a Linux Box

We recently got SSH setup on our Windows boxes so we could eliminate the need for disc mounts on our Linux machines. We are using Pentaho and I am writing a shell script that will, from a Linux box, SSH into the Windows box and execute a perl script.
I have able to write in a way to SSH into the windows box and switch to the directory that holds the Perl scripts that I need to execute, I just can't figure out how to actually execute them.
This is what I have:
#!/bin/sh
ssh -t xxxxx#xxxxx "cd /path/to/script/ /path/to/perl.exe HelloWorld.pl"
I have also tried:
#!/bin/sh
ssh -t xxxxx#xxxxx "cd /path/to/directory/with/perl/script" \
"/path/to/perl.exe HelloWorld.pl"
Both attempts result in a short delay and then a "disconnected from xxxxx" and the perl does not run. I can do all of these steps manually through a shell, but can't seem to get them working in script form. As a note, the only way I've been able to execute the perl scripts is if have the shell in the directory the perl script is in.
You need to use either a semi colon to end your statements, or execute with one statement.
try the following:
ssh xxxxx#xxxxx "cd /path/to/script/; /path/to/perl.exe HelloWorld.pl"
or:
ssh xxxxx#xxxxx "/path/to/perl.exe /path/to/script/HelloWorld.pl"
In the Windows command shell, you can use && like in a Unix-shell. If you expect the first command to succeed,
ssh -t xxxxx#xxxxx "cd /path/to/script/ && /path/to/perl.exe HelloWorld.pl"
will work.

Getting environment from .profile while executing a command through ssh

I am new to the linux/unix world. I would like to trigger a make on a remote machine. For this purpose i created a little script on the remote machine which i want to execute via ssh.
The script looks something like this:
echo "loading .profile"
. ~/.profile
echo "profile loaded"
echo "starting gmake"
cd ~/helloWorld/
gmake all
I invoke the script with following ssh command:
ssh user#remotehost "cd ~/helloWorld && ./myscript.sh"
When I execute this command my machine connects to the remote machine. It tells me that the profile is loaded and then i have to press [CTRL]+[D] to continue with the script. So it seems that the . ./myscript.sh command creates something like a new terminal. I dont want this behaviour. I would like to use the ssh command to automate the building process without the need of closing the terminal manually. Is there a way to do this?
Thanking you in anticipation,
John
./source does not invoke a new shell. It runs the commands in the script in the current shell. Something else is going wrong.

Resources