PHP cron作业启动/停止AWS - 传递参数

时间:2016-02-23 09:49:58

标签: php amazon-web-services amazon-ec2 cron

我在这里有这段代码:

#!/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";
?>

如您所见,它需要3个参数才能工作。你怎么把它们传递给它? 我试过

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

但没有运气!

应该是......

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

1 个答案:

答案 0 :(得分:1)

要将命令行参数传递给PHP脚本,您可能需要查看此Command line Arguments to PHP Script。希望这有助于某人。谢谢