0

I am trying to write a python script on an AWS server, which should be started at every boot of the server. The purpose is to change several files, which use the current public IP address. Afterwards four programs should start:

  • Elasticsearch
  • Enterprise Search
  • a Python API file which starts a flask app
  • a react script

I wrote the script called start_elasticsearch.py, which is located in my home directory. Then I created another python file called start.py with the following commands:

$ sudo nano /etc/init.d/start.py
$ sudo chmod 755 /etc/init.d/start.py
$ sudo update-rc.d start.py defaults

The purpose of the start.py file is to execute the start_elasticsearch.py script directed in the home directory. That is, because I wanted the start_elasticsearch.py easily accessible.

The scripts look as follows:

start.py:

import os
os.system('python3 start_elasticsearch.py')

start_elasticsearch.py

import os
import yaml
from requests import get
import socket

os.system('sudo sysctl -w vm.max_map_count=262144')
os.system('rm -f elasticsearch-7.9.2/config/elasticsearch.yml')
os.system('rm -f enterprise-search-7.9.2/config/enterprise-search.yml')

hostname = socket.gethostname()
private = socket.gethostbyname(hostname)
public = get('https://api.ipify.org').text

private_ = private.replace(".", "-")
public_ = public.replace(".", "-")
elasticsearch = {some text}

with open('elasticsearch-7.9.2/config/elasticsearch.yml', 'w') as outfile:
    yaml.dump(elasticsearch, outfile, default_flow_style=False)

enterprise_search = {some other text}

with open('enterprise-search-7.9.2/config/enterprise-search.yml', 'w') as outfile:
    yaml.dump(enterprise_search, outfile, default_flow_style=True)


with open('my-app/src/App.js', 'r') as file:
    # read a list of lines into data
    react_script = file.readlines()

react_script[22] = endpointbase
os.system('rm -f my-app/src/App.js')
with open('my-app/src/App.js', 'w') as file:
    file.writelines(react_script)

with open('my-app/src/request.js', 'r') as file:
    # read a list of lines into data
    request = file.readlines()

request[10] = '''            url: "http://ec2-'''
os.system('rm -f my-app/src/request.js')
with open('my-app/src/request.js', 'w') as file:
    file.writelines(request)

with open('my-app/src/API.py', 'r') as file:
    # read a list of lines into data
    flask = file.readlines()

flask[170] = '''    app.run(host="someip") '''
os.system('rm -f my-app/src/API.py')
with open('my-app/src/API.py', 'w') as file:
    file.writelines(flask)

os.system('python3 my-app/src/API.py')
os.system('elasticsearch-7.9.2/bin/elasticsearch')
os.system('enterprise-search-7.9.2/bin/enterprise-search')
os.chdir('my-app')
os.system('npm start')

Basically, I find the current IP address, modify and delete the old files, create the new files, and start the corresponding programs.

Now to the problem:

  • Whenever I start the server, nothing happens. So apparently the start.py script is not executed. Did I make any mistakes implementing that?
  • When I start the script manually, the logs of the programs starting are written into the files that I create. Therefore the files that I write contain logs of for example Elasticsearch. How can I prevent that?
spadel
  • 998
  • 2
  • 16
  • 40
  • You would probably be better served creating a systemd service for this. – Raman Sailopal Oct 28 '20 at 09:50
  • Unfortunately I have never done something like that. Regarding the time I've put in so far, I prefer to somehow make it work with a python script, as practically it should work too I would expect – spadel Oct 28 '20 at 09:58
  • Take a look at this - https://www.shubhamdipt.com/blog/how-to-create-a-systemd-service-in-linux/ You will still be referencing the Python scripts you have created, just configuring them to run under systemd process management. – Raman Sailopal Oct 28 '20 at 10:19
  • alright got it, so that should provide the automated execution of the script. The problem that still remains is, that the program logs are written into the files, however. – spadel Oct 28 '20 at 10:32

0 Answers0