Sometimes, you might want to execute asynchronous commands remotely in scripts. In this case, you need to automate what you would do manually through screen.

Although you could do this through an SSH Subsystem, I chose to do it through screen.

First, you need to make sure screen is installed on your remote server.

Here is an example script to run a remote command asynchronously :

#! /usr/bin/env bash

SCRIPT_FOLDER=${0%/*}
SCRIPT_NAME=${0##*/}

if [[ $1 != "--async" ]]; then
  cd $SCRIPT_FOLDER
  screen -dm ./$SCRIPT_NAME --async
  # -dm Start a new screen session without attaching to it
else
  # Commands to run asynchronously
  git -C ./src pull
fi

Make sure you make the script executable through chmod u+x <script>

Finally, you can execute the following :

$ ssh user@host /path/to/script

You could add the script to $PATH if you don’t want to remember where it is located or make the ssh command shorter if you run it manually.

Inspirations :

See also :