Skip to content

Instantly share code, notes, and snippets.

@stvhwrd
Last active February 17, 2017 21:58
Show Gist options
  • Save stvhwrd/b0195b4fab2b9799ea972b757b324505 to your computer and use it in GitHub Desktop.
Save stvhwrd/b0195b4fab2b9799ea972b757b324505 to your computer and use it in GitHub Desktop.
Compile and Run Code on Remote Server - Credit to Amanda Dash for this
#!/bin/bash
#Usage: ./example.sh <local dir> "<cmd to run remotely>"
# ./example.sh . "./hello.out 5"
#Run the command date and save the output to the variable ts
#this is the Unix epoch seconds, this is the name of the folder
#we'll create
ts=$(date +%s)
#Grab the first argument which is the directory we're copying to the remote server
local_dir="$1"
#Grab the second argument which is the command we're going to run
cmd="$2"
#Address of the remote server
remote_srv="linux.csc.uvic.ca"
#Netlink ID to use
remote_id="stvhwrd" #"<netlink>"
#Path the to the remote directory
remote_dir="/home/stvhwrd/Desktop/csc360" #"<remote_dir>"
echo "Copying $local_dir to $remote_id@$remote_srv:$remote_dir/$ts ..."
#Note: if you don't want to put in your password, you can install and
# user ssh-pass
#Do a secure copy to the remote server and rename our local directory
#to the timestamp (ensuring we never overwrite previous versions)
scp -r $local_dir $remote_id@$remote_srv:$remote_dir/$ts
echo "Compile and run '$cmd' on the server..."
remote_cmd="$cmd 2> err.log > out.log; echo '---ERRORS---'; cat err.log; echo '---OUTPUT---'; cat out.log"
echo "stderr redirected (2>) to err.log and stdout (>) to out.log"
#Do a ssh remote command to compile and run the command
#if you want to run this in interactive mode, use:
# $ ssh -t
ssh $remote_id@$remote_srv "cd $remote_dir/$ts; make all && $remote_cmd"
echo "All done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment