Reputation: 1036
I have an ec2 instance and created 2 python files, which I want to run whenever the instance is starting. The files are called config.py
and start_elastic.py
and are located in /home/ubuntu/.
I tried several options to make it start (using systemd, locating it in init.d etc.) and nothing works. Now I was told, the way to go is with crontab. So I did the following:
sudo crontab -e
@reboot . /home/ubuntu/config.py
@reboot . /home/ubuntu/start_elastic.py
Unfortunately nothing happens. The files are executable (chmod +x filename
). Does anyone know why it doesn't work?.
The python files look for example like this:
#!/usr/bin/python3
import os
import yaml
from requests import get
import socket
hostname = socket.gethostname()
private = socket.gethostbyname(hostname)
public = get('https://api.ipify.org').text and so on
Upvotes: 0
Views: 315
Reputation: 4642
.
(as in single dot symbol, followed by space) in shell is equivalent of source
command, so basically you are trying to execute every line in you python script as shell command, which is certainly not something you want to do.
The proper way to execute you script, assuming chmod +x
was set is
@reboot /home/ubuntu/config.py
@reboot /home/ubuntu/start_elastic.py
However, for clarity i'd recommend explicitly declaring interpreter in cronjob, like this
@reboot /usr/bin/python3 /home/ubuntu/config.py
@reboot /usr/bin/python3 /home/ubuntu/start_elastic.py
Upvotes: 1