Viewed   73 times

When you describe your EC2 instances, you get an XML node of:-

[launchTime] => 2011-10-14T09:22:37.000Z

I'd like to use this command with PHP, to measure the number of seconds the instance has been on and take actions.

It seems to me there's a number of ways to break this down, including explodes and string searches and regex. But, what is the best way?




Okay, after checking out this thread, I've decided on this approach as the only one that seems to return an accurate measure:

$dt = DateTime::createFromFormat('Y-m-dTH:i:s.uZ', $date);
$now = new DateTime();
echo ($now->getTimestamp() - $dt->getTimestamp())."n";


In PHP 5.3 using classes (no 'U' format):

$dt = DateTime::createFromFormat('Y-m-dTH:i:s.uZ', $arr['launchTime']);
echo (new DateTime())->format('U');

In PHP 5.3 using procedural calls (also works like final solution):

$dt = date_create_from_format('Y-m-dTH:i:s.uZ', $arr['launchTime']);
$now = date_create();
echo ($now->getTimestamp() - $dt->getTimestamp());

In any version using strtotime (return wrong time):

echo time() - strtotime($arr['launchTime']);
Tuesday, October 25, 2022

Needed to run it in passive mode

ftp_pasv($conn_id, true);
Wednesday, October 5, 2022

After a lot of "scratching my head", I finally figured it out.

First of all you one need to figure out the current user who is executing the php. One can either check out file or use

$processUser = posix_getpwuid(posix_geteuid());
print $processUser['name'];

This will give you the user who is executing the code. In my case it was apache rather than www-data (I shouldn't have assumed so in first place).

After that you will need to edit the sudoers file (etc/sudoers)

Add the lines over there.

apache ALL=NOPASSWD:/var/www/
apache ALL=NOPASSWD:/usr/bin/pythondestination

or you can simply add

apache ALL=(ALL)        NOPASSWD:ALL

(You probably should just specify the path).

Then execute the script through php.

Friday, September 23, 2022


be sure, that you have the files in the right place, usually its in the plugins folder for php! maybe you cann add a absolute path to your php.ini! be sure, you edit the correct ini file!

php completly independent to your operatingsystem!

just be sure to doublecheck everything. because its not saying, it has trouble loading your extention, its just saying, the function your trying to call, is not there. so i assume your extention ist not loading at all! :)

Tuesday, October 25, 2022

Anything that is not stored on an EBS volume that is mounted to the instance will be lost.

For example, if you mount your EBS volume at /mystuff, then anything not in /mystuff will be lost. If you don't mount an ebs volume and save stuff on it, then I believe everything will be lost.

You can create an AMI from your current machine state, which will contain everything in your ephemeral storage. Then, when you launch a new instance based on that AMI it will contain everything as it is now.

Update: to clarify based on comments by mattgmg1990 and glenn bech:

Note that there is a difference between "stop" and "terminate". If you "stop" an instance that is backed by EBS then the information on the root volume will still be in the same state when you "start" the machine again. According to the documentation, "By default, the root device volume and the other Amazon EBS volumes attached when you launch an Amazon EBS-backed instance are automatically deleted when the instance terminates" but you can modify that via configuration.

Monday, August 22, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :