当前位置:网站首页>Alibaba cloud schedules tasks and automatically releases them

Alibaba cloud schedules tasks and automatically releases them

2022-06-24 21:12:00 The smell of tobacco

Preface

Recently wrote a crawler script , The script runs on a Beijing ecs On . But somehow some unknown force , A connection broker is required to access the target web site .

I was thinking of acting for myself , But it's too expensive , For the time being . Until I found this :

image-20210926231548381

Alibaba cloud servers in Hong Kong , It took an hour to 5 Cents . If the script runs directly on the Hong Kong server, it's OK , At this price , I use it for an hour every day , Less than two yuan a month ( Additional payment for flow ).

Prying into

My basic idea is like this :

  • Beijing ecs Timing task , Create and launch Hong Kong ecs
  • In Hong Kong ecs On a mission
  • When the task is completed , Hong Kong ecs Delete

Do as you say , Check the information provided by Alibaba cloud API, Cloud service providers are really convenient , Basically, all operations can be performed through API To execute . API Document address : https://next.api.aliyun.com/api/Ecs

Its API Every interface for every language on the document , There are corresponding demo, I like that .

start-up ecs

start-up ecs Method : runInstances, You can customize parameters , But I recommend creating a startup template , It's more convenient :

image-20210926232534600

Among them UserData Parameters In fact, it is a small script , But not more than 16kb. Custom data document

We'll go through him , stay ecs Automatically execute tasks after creation .

By the following command , You can view the incoming ecs Script content for ( In fact, it is very clear in the document ):

curl http://100.100.100.200/latest/user-data

good , The script we started needs to do the following :

  1. Initialization environment
  2. Drag down the project that needs to be run
  3. Run script
  4. Delete yourself when you are finished

My script goes something like this , For reference :

#!/bin/sh
#  Download dependency first 
apt update
apt install -y git php curl 
#  install  composer
export HOME=/root
curl -sS https://getcomposer.org/installer -o /root/composer-setup.php
/usr/bin/php /root/composer-setup.php --install-dir=/usr/local/bin --filename=composer
#  Download project 
git clone xxx /var/workspace
#  install vendor
cd /var/workspace
printf 'yes\n' | composer install
#  Import profile 
echo '$confJson' > /var/workspace/cron/conf.json
#  Run the command ...
#  Run the script to delete yourself 
php /var/worspace/cron/delete_self.php

Be careful , Here, the script for deleting itself is taken out separately , Why not delete yourself directly after the task script is executed ? If for some unknown reason , The task script hangs up , The instance will not be deleted normally , This is all white silver .

If you meet UserData There is a problem , But open a new one ecs after , It's OK to run again . here , You can see /var/log/cloud-init-output.log Check the files , The running results of the initialization script are recorded .

Delete ecs

Now the mission has started , But you still need to delete yourself after running . After checking, I found deleteInstances Method , But the parameter needs instanceId, How to get the current running instance instanceId Well ? To find the It's also described in the document

tip , Look up the syntax : instance-Id site:https://help.aliyun.com/ Directly locate the target page .

Access address :

curl http://100.100.100.200/latest/meta-data/instance-id

Get current instance ID after , And then it's easy , Just delete yourself .


Execute the script in this way , It is really economical and affordable . You may as well try it if you need it ~

原网站

版权声明
本文为[The smell of tobacco]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211318416215.html