Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.4K
Views
How to run bash script from Nginx

1) I have static site and wand to set "autopull" from bitbucket.

2) I have webhook from bitbucket.

3) I have bash script which do "git pull"

How can I run this script when nginx catch request?

server {

    listen   80;
    server_name example.ru;

    root /path/to/root;
    index index.html;

    access_log /path/to/logs/nginx-access.log;
    error_log /path/to/logs/nginx-error.log;

    location /autopull {
        something to run autopull.sh;
    }

    location / {
        auth_basic "Hello, login please";
        auth_basic_user_file /path/to/htpasswd;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_set_header Host $host;
    }
}

I tried lua_block and fastcgi service, but both are failed. lua does not run os.execute("/path/to/script") and does not write the log. fastcgi is more successful, but it has not permissions, because my www-data user doesn't have ssh-key in my bitbuchet repo.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Problem solved.

I didnt want to use any script/process on another port because I have several sites and I need port for each.

My final configuration is:

server {

    listen   80;
    server_name example.ru;

    root /path/to/project;
    index index.html;

    access_log /path/to/logs/nginx-access.log;
    error_log /path/to/logs/nginx-error.log;

    location /autopull {
        content_by_lua_block {
            io.popen("bash /path/to/autopull.sh")
        }
    }

    location / {
        auth_basic "Hello, login please";
        auth_basic_user_file /path/to/htpasswd;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_set_header Host $host;
    }
}

Problem was in permission of www-data user and its ssh-kay in repo.

over 4 years ago · Santiago Trujillo Report

0

Based on this, create py script

#!/usr/bin/python
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
from subprocess import call

PORT_NUMBER = 8080
autopull = '/path/to/autopull.sh'
command = [autopull]

#This class will handles any incoming request from
#the browser 
class myHandler(BaseHTTPRequestHandler):

    #Handler for the GET requests
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type','text/html')
        self.end_headers()
        # Send the html message
        self.wfile.write("runing {}".format(autopull))
        call(command)
        return

try:
    #Create a web server and define the handler to manage the
    #incoming request
    server = HTTPServer(('', PORT_NUMBER), myHandler)
    print 'Started httpserver on port ' , PORT_NUMBER

    #Wait forever for incoming htto requests
    server.serve_forever()

except KeyboardInterrupt:
    print '^C received, shutting down the web server'
    server.socket.close()

Run it and in nginx config add

location /autopull { proxy_pass http://localhost:8080; }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!