Skip to content

Instantly share code, notes, and snippets.

@MBoaretto25
Last active February 14, 2019 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MBoaretto25/ff23c3b2a2de28b102c427b61fafd5bc to your computer and use it in GitHub Desktop.
Save MBoaretto25/ff23c3b2a2de28b102c427b61fafd5bc to your computer and use it in GitHub Desktop.
fabfile template for pushing local files to a remote folder, and running script in background.
"""
It is a good practice to usually run fabric directly from is
directory folder
to install:
pip install fabric3
to run (in fabfile current directory):
fab test
@author: Marco Boaretto
"""
import time
from fabric.api import env
from fabric.api import puts
from fabric.api import put
from fabric.api import cd
from fabric.api import sudo
from fabric.api import hide
from fabric.api import execute
from fabric.api import task
import warnings
warnings.filterwarnings("ignore")
env.hosts = ['user@0.0.0.0']
remote_folder = 'remote/foler/path'
def push():
# Push script in remote folder
put('teste_1.py', remote_folder)
def runbg():
# Run script in background
with cd(remote_folder):
command = 'your command here'
"""
the use of 'nohup' and '&> /dev/null < /dev/null &) && /bin/true'
allows the script to run in background
"""
sudo('(nohup %s &> /dev/null < /dev/null &) && /bin/true' % command)
def deploy():
start = time.time()
#hide all info to run smoothly
with hide('running', 'stdout', 'stderr'):
push()
runbg()
puts(' ________')
puts('< execution finished in %.2fs >' % (time.time() - start))
puts(' ________')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment