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?