bash script to run commands on remote server

Bash Script to Run Commands on Remote Server

Most Linux systems require you to do SSH login to remote server in order to run commands. Often you may need to run same set of commands on your remote server on a regular basis. It can be tedious to manually login to SSH every time and then run the commands. It is better to simply write a bash script to run commands on remote server. In this article, we will learn how to do this. You can use these steps on all Linux systems.

Bash Script to Run Commands on Remote Server

We will use HERE document feature of shell that allows you to feed command list to interactive programs such as SSH terminal. Here is its syntax.

COMMAND <<LimitString

In the above code block, we specify the initial interactive command or program to run and also mention the LimitString. After that, whatever commands you enter, they will be executed within the interactive program you called, until you enter the LimitString. Once LimitString is encountered, the execution within interactive program stops and control is returned back to your shell terminal.

So you can easily use this code block as shown below to run commands on remote server. Replace remote_host with your remote host IP address or URL.

ssh remote_host << EOF
  #command 1
  #command 2


In the above code, we run the command ‘ssh remote_host’ to login to remote server. We also mention EOF as LimitString. Then we enter the commands we want to execute remotely. In the end we mention EOF to terminate remote execution and return control back to our shell.

Here is an example to run ls and some other commands remotely.

ssh remote_host << EOF
  ls some_folder; 
  ./ 'some params'
  ./some_other_action 'other params'

While running the above code, if you get the following message

Pseudo-terminal will not be allocated because stdin is not a terminal.

then modify the first line of your HERE document as

ssh remote_host /bin/bash << EOF

If you want to variable substitution you can enclose them within quotes but make sure that you don’t mention quotes in your LimitString.

ssh remote_host /bin/bash << EOF
touch "/tmp/${NAME}"

In this case, the shell variable $NAME will be passed to your remote server and used in executing the remote command.

You can even create a cronjob to run the above shell script regularly.

For example, let us say you have saved the above HERE code block in a shell script at /home/ubuntu/ Make it executable with the following command.

$ sudo chmod +x /home/ubuntu/

Next, you can open crontab to create cronjob.

$ crontab -e

Add the following line to run the shell script every day at 10.a.m.

0 10 * * * /home/ubuntu/ >/dev/null 2>&1

In this article, we have learnt how to execute commands on remote server. You can customize them as per your requirement.

Of course, if you just want to run a single command or a few commands, then you can directly include them in your SSH connection call.

$ ssh <username@IP_Address-or-Doman_name> <Command-or-Script>

For example here we are executing ‘uname -a’ command remotely.

$ ssh test@ uname -a
$ ssh test@ "uname -a"

If you want to run multiple commands, you can combine them using && operator, or semicolon (;) operator.

$ ssh test@ "uname -r && lsb_release -a"
$ ssh test@ "uname -r ; lsb_release -a"

But using HERE document is the cleanest way to run multiple remote commands since it leads to shorter code and also supports variable substitution.

Also read:

How to Add Image to Text in Python
How to Find & Copy Files in Linux
How to Rename Downloaded File in Wget
Wget Limit Download Speed Rate
How to Backup & Restore PostgreSQL database

Leave a Reply

Your email address will not be published.