Using remote SSH command to run code on other machines under Linux

If you want to run a command on machine B remotely from machine a, such as ls on B, you can use the following command:

[email protected]:~$ ssh [email protected] 'ls'

If you want to run multiple commands on machine B remotely from machine a, you can use semicolon to separate multiple commands, such as:

[email protected]:~$ ssh [email protected] 'cd test; ls'

Of course, you can also run the shell script on machine B. For example, if there is a test.sh script in the cyf user directory on machine B, you can:

[email protected]:~$ ssh [email protected] './test.sh'

or

[email protected]:~$ ssh [email protected] 'bash test.sh'

However, when running the Java program test on machine B remotely, there is a problem:

[email protected]:~$ ssh [email protected] 'java Test'

shows

bash: java: Command not found

And direct ssh [email protected] After that, run java test again, and it will work normally:

If you run

[email protected]:~$ ssh [email protected] ‘env’

You will find that there are no Java environment variables

This is because SSH is a short link when executing the command and does not open the terminal, so it will not load the .bashrc environment variable, resulting in unrecognized Java commands

Solution: modify /etc/environment and add Java path to it

PATH="usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/jdk/bin"

Execute the Java program test again, and you can find it works.

原创文章,作者:ifee,如若转载,请注明出处:https://www.ifee.win/blog/2021/05/17/using-remote-ssh-command-to-run-code-on-other-machines-under-linux/

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注