HowTo: Execute A Script On Remote UNIX / Linux Server

How do I execute a script on remote server called server2.example.com from my PC? How to run a shell/python/perl script on remote Unix-like systems?

You can use the ssh client which is a part of most *BSD/Linux/OS X/Linux distros/Unix-like oses. If you are on MS-Windows operating systems you need to download and use Putty client.[donotprint][/donotprint]

ssh command line syntax

The basic syntax is as follows:

ssh [email protected] /path/to/script
ssh [email protected] /path/to/script arg1 arg2 arg3

OR

ssh [email protected] /path/to/command
ssh [email protected] /path/to/command arg1 arg2

Examples

Open the Terminal. To run a script called /root/scripts/backup.sh on remote UNIX or Linux server called server1.sxi.io, enter:

ssh root@server1.sxi.io /root/scripts/backup.sh

ssh [email protected] /root/scripts/backup.sh

You can pass command line arguments too:

ssh root@server1.sxi.io /scripts/job.init --job=sync --type=aws --force=true

ssh [email protected] /scripts/job.init –job=sync –type=aws –force=true

You can execute the date command as follows on remote server:

ssh user@server2.example.com date

ssh [email protected] date

You may need to specify full script path. In this example, run /root/backup.sh script as follows:

ssh user@server2.example.com /root/backup.sh

ssh [email protected] /root/backup.sh

To avoid password prompt set password less ssh keys or use tool such as keychain – a manager for ssh-agent, typically run from ~/.bash_profile.

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.