2017存錢計劃(python實現(xiàn))
靈感來源于在微博看到的一條微博桥状,365天存錢法,每天從1到365中任選一個數(shù)字存錢,每一天的數(shù)字都不能重復援制,這樣一年下來就有66795元决瞳,6W多塊錢對于我來說太多货徙,那就從0.1-36.5,根據收入多寡皮胡,每天選擇其中的一個數(shù)字來存痴颊,一年就有6679.5元,是一筆不少的數(shù)字屡贺。那就開始吧蠢棱。
- 考慮到之前租借的vps利用率不高,就打算用vps中安裝的centos的定時任務crontab甩栈,配合上sendmail來發(fā)送郵件告知每日應該存多少錢泻仙。
安裝Xshell,連接上遠端服務器量没。 yum -y install sendmail crontabs 安裝必要的軟件玉转。
touch sendmail.py vim sendmail.py
#!/usr/bin/python
#-*-coding:utf-8 -*- //指定編碼,兼容中文
import random
import os
import time
import datetime
# the remainder days of next China Year
today = datetime.date.today()
other_day = datetime.date(2018,2,15)
remainder = other_day - today
# 每日存錢的數(shù)字
tar = random.randint(1,365)
result = []
with open('/root/saveMoney/saved.txt','r') as f:
data = f.readlines()
for line in data:
tmp = line.split()
for x in tmp:
result.append(x)
num_int = map(float,result)
while str(tar) in result:
tar = random.randint(1,365)// 保證產生的數(shù)不重復
# 保存已經存過的數(shù)字到saved.txt
with open('/root/saveMoney/saved.txt','a') as f:
f.write(' ')
f.write(str(tar))
f.close()
# 要發(fā)送的文本寫入到todo.txt
with open('/root/saveMoney/todo.txt','w') as f:
f.write("今天殴蹄,Centos6建議您存入" + str(tar/10.0) + "元\n")
f.write("距離過年還有" + str(remainder.days) + "天")
f.close()
#調用mail外部命令發(fā)送郵件
os.system(" mail -s 'save money plant' xx@xx.com < /root/saveMoney/todo.txt")
- 新建 crontab.cron vim crontab.cron
24 10 * * * python /root/saveMoney/sendmail.py 達到每天10點24分定時發(fā)送的目的究抓。 - crontab crontab.cron
- 雖然可以不重新啟動服務,但為了確保crond正常工作袭灯,還是加入 service crond restart
- 到此刺下,結束。