GVX
GVX

Reputation: 53

PHP cron job to start/stop AWS - passing parameters

I've this code here:

#!/usr/bin/php
<?php
/*
|
| ec2-power-button -- @dustyfresh
| ./ec2-power-button <start/stop> <instanceID> <region>
|
| this will toggle an instance on or off, and it's good
| for cronjerbs!
|
*/
error_reporting(0);
$cmd = $argv[1] or die("please supply a command(start/stop)...\n");
$instanceID = $argv[2] or die("please supply an instance ID\n");
$region = $argv[3] or die("Please specify a region. for example: us-east-1\n");

require_once "awssdkforphp/vendor/autoload.php";
use Aws\Ec2\Ec2Client;

$client = Ec2Client::factory(array(
 'key' => '', // your auth API key
 'secret' => '', // your secret API key
 'region' => "$region",
 ));

if($cmd == 'start'){
 $result = $client->startInstances(array(
 'InstanceIds' => array($instanceID,),
 'DryRun' => false,
 ));
} elseif($cmd == 'stop'){
 $result = $client->stopInstances(array(
 'InstanceIds' => array($instanceID,),
 'DryRun' => false,
 ));
}
//print_r($result); // uncomment to see results of request
print "OK\n";
?>

As you can see, it needs 3 arguments to work. How do you pass them to it? I tried with

php -q /path/public_html/script.php start i-9999 eu-west-c

But no luck!

Should it be something like...

php -q /path/public_html/script.php?start&i-9999&eu-west-c

?

Upvotes: 1

Views: 233

Answers (1)

sandesh
sandesh

Reputation: 390

To pass command line arguments to PHP Script, May be you need to look at this Command line Arguments to PHP Script.Hope this helps Someone. Thank you

Upvotes: 1

Related Questions