当前位置:网站首页>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 :

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 :

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 :
- Initialization environment
- Drag down the project that needs to be run
- Run script
- 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 ~
边栏推荐
- Postman assertion
- Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading
- 全上链哈希游戏dapp系统定制(方案设计)
- 刚购买了一个MYSQL数据库,提示已有实例,控制台登录实例要提供数据库账号,我如何知道数据库账号。
- Axi DMA IP core operation process
- Shrimp skin test surface treated
- Haitai Advanced Technology | application of privacy computing technology in medical data protection
- Analysis of errors in JSON conversion using objectmapper
- Memo mode - game archiving
- 海泰前沿技术|隐私计算技术在医疗数据保护中的应用
猜你喜欢
浅谈MySql update会锁定哪些范围的数据

Wechat applet custom tabbar

伯克利、MIT、劍橋、DeepMind等業內大佬線上講座:邁向安全可靠可控的AI

微信小程序自定义tabBar

Network security review office starts network security review on HowNet

How to apply agile development ideas to other work

在Dialog中使用透明的【X】叉叉按钮图片

Visitor model -- generation gap between young and middle-aged people

The JS method parameter passed a number beginning with 0. A magical problem occurred and bothered me for a long time

微信小程序中使用vant组件
随机推荐
C language to realize mine sweeping (simple version)
Memo mode - game archiving
IDEA Dashboard
regular expression
刚购买了一个MYSQL数据库,提示已有实例,控制台登录实例要提供数据库账号,我如何知道数据库账号。
Comprehensive comparison of the most popular packet capturing tools in the whole network
What will you do if you have been ignored by your leaders at work?
What does virtualization mean? What technologies are included? What is the difference with private cloud?
Physical layer introduction
Smooth live broadcast | analysis of key technologies for live broadcast pain points
Appium desktop introduction
database/sql
虚拟化是什么意思?包含哪些技术?与私有云有什么区别?
C langage pour le déminage (version simplifiée)
在Dialog中使用透明的【X】叉叉按钮图片
Format method and parse method of dateformat class
JUnit unit test
Jar package operation
Learn together and make progress together. Welcome to exchange
Why do we always "give up halfway"?