2017-02-10 25 views
0

繰り返しても謝罪しますが、私の仕事を達成するのに価値のあるものが見つかりませんでした。 インスタンスがあり、boto3を使用して起動と停止を確認していますが、動作しますが、実際の問題はインスタンスが起動しているときにスクリプトを実行していることです。私はスクリプトが終了するのを待って、インスタンスを停止したいと思います。AWS EC2でPythonスクリプトを実行しています

python /home/ubuntu/MyProject/TechInd/EuropeRun.py & 
python /home/ubuntu/FTDataCrawlerEU/EuropeRun.py & 

かなりのポストを読むことは、ラムダとAWS Beanstalkの方向につながりますが、それらは単純には見えません。

ご意見をいただければ幸いです。

よろしく DC

答えて

1

次のコードを使用できます。

import boto3 
import botocore 
import os 
from termcolor import colored 
import paramiko 


def stop_instance(instance_id, region_name): 
    client = boto3.client('ec2', region_name=region_name) 
while True: 
    try: 
     client.stop_instances(
      InstanceIds=[ 
       instance_id, 
      ], 
      Force=False 
     ) 

    except Exception, e: 
     print e 

    else: 
     break 

# Waiter to wait till instance is stopped 
waiter = client.get_waiter('instance_stopped') 
try: 
    waiter.wait(
     InstanceIds=[ 
      instance_id, 
     ] 
    ) 
except Exception, e: 
    print e 


def ssh_connect(public_ip, cmd): 

# Join the paths using directory name and file name, to avoid OS conflicts 
key_path = os.path.join('path_to_aws_pem', 'file_name.pem') 

key = paramiko.RSAKey.from_private_key_file(key_path) 
client = paramiko.SSHClient() 
client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 

# Connect/ssh to an instance 
while True: 
    try: 
     client.connect(hostname=public_ip, username="ubuntu", pkey=key) 

     # Execute a command after connecting/ssh to an instance 
     stdin, stdout, stderr = client.exec_command(cmd) 
     print stdout.read() 

     # close the client connection once the job is done 
     client.close() 
     break 

    except Exception, e: 
     print e 

# Main/Other module where you're doing other jobs: 

# Get the public IP address of EC2 instance, I assume you should have handle to the ec2 instance already 
# You can use any alternate method to fetch/get public ip of your ec2 instance 
public_ip = ec2_instance.public_ip_address 


# Get the instance ID of EC2 instance, I assume you should have handle to the ec2 instance already 
instance_id = ec2_instance.instance_id 

# Command to Run/Execute python scripts 
cmd = "nohup python /home/ubuntu/MyProject/TechInd/EuropeRun.py & python /home/ubuntu/FTDataCrawlerEU/EuropeRun.py &" 
ssh_connect(public_ip, cmd) 
print colored('Script execution finished !!!', 'green') 

# Shut down/Stop the instance 
stop_instance(instance_id, region_name) 
+0

ありがとうございます。ウェイター秒は本当にとても役に立ちます。 – user3341078

+0

それがあなたを助けたことを知ってうれしい。目的に合っていれば、「回答は受け入れ済み」としてください。 – Venkatesh

0

スクリプトが実行されたら、あなたは、Pythonのコードをお使いのshutdownコマンドを実行することができます。

LS

from subprocess import call 
call(["ls", "-l"]) 

しかし、簡単なラムダは、はるかに簡単かつ資源効率的である何かのための使用例。スクリプトをs3にアップロードするだけで、lambda関数をboto3で実行する必要があります。 実際には、依存関係がない場合は、スクリプトコードをラムダコンソールに貼り付けてコピーすることができます。

0

、システムの起動時に自動的にスクリプトを実行するためのいくつかのオプション:

  • はinit.dスクリプト、または@reboot経由でブート時にスクリプトを起動するAMIを設定EC2 User-Data
  • 経由でスクリプトを呼び出しますcronジョブ。

スクリプトの終了後にインスタンスをシャットダウンするには、OSのシャットダウンを開始するか、Botoを介してAWS APIを呼び出してインスタンスをシャットダウンするスクリプトの最後にコードを追加します。

関連する問題