1、定时执行脚本

http://tech.it168.com/a2011/0707/1214/000001214830_all.shtml

/sbin/service crond start //启动服务
  /sbin/service crond stop //关闭服务
  /sbin/service crond restart //重启服务
  /sbin/service crond reload //重新载入配置
  可以将这个服务在系统启动的时候也自动启动:
  在/etc/rc.d/rc.local这个脚本的末尾加上:
  /sbin/service crond start
  1.直接用crontab命令编辑
  cron服务提供crontab命令来设定cron服务的,以下是这个命令的一些参数与说明:
  crontab -u //设定某个用户的cron服务,一般root用户在执行这个命令的时候需要此参数
  crontab -l //列出某个用户cron服务的详细内容
  crontab -r //删除没个用户的cron服务
  crontab -e //编辑某个用户的cron服务
  比如说root查看自己的cron设置:
  crontab -u root -l
  再例如,root想删除fred的cron设置:
  crontab -u fred -r
  在编辑cron服务时,编辑的内容有一些格式和约定,输入:
  引用:
  crontab -u root -e
  进入vi编辑模式,编辑的内容一定要符合下面的格式:
  */1 * * * * ls >> /tmp/ls.txt
  这个格式的前一部分是对时间的设定,后面一部分是要执行的命令,如果要执行的命令太多,可以把这些命令写到一个脚本里面,然后在这里直接调用这个脚本就可以了,调用的时候记得写出命令的完整路径。时间的设定我们有一定的约定,前面五个*号代表五个数字,数字的取值范围和含义如下:
  引用:
  分钟 (0-59)
  小時 (0-23)
  日期 (1-31)
  月份 (1-12)
  星期 (0-6)//0代表星期天

  除了数字还有几个个特殊的符号就是"*"、"/"和"-"、",",*代表所有的取值范围内的数字,"/"代表每的意思,"*/5"表示每5个单位,"-"代表从某个数字到某个数字,","分开几个离散的数字。以下举几个例子说明问题:
  引用:
  每天早上6点
  0 6 * * * echo "Good morning." >> /tmp/test.txt //注意单纯echo,从屏幕上看不到任何输出,因为cron把任何输出都email到root的信箱了。
  每两个小时
  0 */2 * * * echo "Have a break now." >> /tmp/test.txt
  晚上11点到早上8点之间每两个小时,早上八点
  0 23-7/2,8 * * * echo "Have a good dream:)" >> /tmp/test.txt
  每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
  0 11 4 * 1-3 command line
  1月1日早上4点
  0 4 1 1 * command line
  2.编辑/etc/crontab 文件配置cron
  cron服务每分钟不仅要读一次/var/spool/cron内的所有文件,还需要读一次/etc/crontab,因此我们配置这个文件也能运用cron服务做一些事情。用crontab配置是针对某个用户的,而编辑/etc/crontab是针对系统的任务。此文件的文件格式是:
  SHELL=/bin/bash
  PATH=/sbin:/bin:/usr/sbin:/usr/bin
  MAILTO=root //如果出现错误,或者有数据输出,数据作为邮件发给这个帐号
  HOME=/
  # run-parts
  01 * * * * root run-parts /etc/cron.hourly //每个小时去执行一遍/etc/cron.hourly内的脚本
  02 4 * * * root run-parts /etc/cron.daily //每天去执行一遍/etc/cron.daily内的脚本
  22 4 * * 0 root run-parts /etc/cron.weekly //每星期去执行一遍/etc/cron.weekly内的脚本
  42 4 1 * * root run-parts /etc/cron.monthly //每个月去执行一遍/etc/cron.monthly内的脚本
  使用者 运行的路径
  注意"run-parts"这个参数了,如果去掉这个参数的话,后面就可以写要运行的某个脚本名,而不是文件夹名了。

2、读数据驱动&发邮件

# -*- coding: utf-8 -*-
"""
Created on Tue Jan 28 11:18:00 2014 @author: pi
"""
import smtplib
from email.mime.text import MIMEText
import RPi.GPIO as gpio
import time mail_to_1="====@outlook.com"
mail_to_2="====@qq.com"
mail_to_3='=====@qq.com'
mail_to_4="====@qq.com" def send_mail(to_list,title,content):
mail_host="smtp.126.com"
mail_user="colipso"
mail_pass="====="
mail_postfix="126.com"
me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
msg=MIMEText(content)
msg['Subject']=title
msg['From']=mail_user
msg['To']=to_list mail=smtplib.SMTP()
mail.connect(mail_host)
mail.login(mail_user,mail_pass)
mail.sendmail(me,to_list,msg.as_string())
mail.close() def get_temperature_humidity():
gpio.setwarnings(False)
gpio.setmode(gpio.BOARD)
time.sleep(1)
data=[]
def delay(i): #20*i usdelay
a=0
for j in range(i):
a+1
j=0
#start work
gpio.setup(12,gpio.OUT)
#gpio.output(12,gpio.HIGH)
#delay(10)
gpio.output(12,gpio.LOW)
time.sleep(0.02)
gpio.output(12,gpio.HIGH)
i=1
i=1 #wait to response
gpio.setup(12,gpio.IN) while gpio.input(12)==1:
continue while gpio.input(12)==0:
continue while gpio.input(12)==1:
continue
#get data while j<40:
k=0
while gpio.input(12)==0:
continue while gpio.input(12)==1:
k+=1
if k>100:break
if k<3:
data.append(0)
else:
data.append(1)
j+=1 #print "Sensor is working"
#get temperature
humidity_bit=data[0:8]
humidity_point_bit=data[8:16]
temperature_bit=data[16:24]
temperature_point_bit=data[24:32]
check_bit=data[32:40] humidity=0
humidity_point=0
temperature=0
temperature_point=0
check=0 for i in range(8):
humidity+=humidity_bit[i]*2**(7-i)
humidity_point+=humidity_point_bit[i]*2**(7-i)
temperature+=temperature_bit[i]*2**(7-i)
temperature_point+=temperature_point_bit[i]*2**(7-i)
check+=check_bit[i]*2**(7-i) tmp=humidity+humidity_point+temperature+temperature_point if check==tmp:
return temperature,humidity
else:
return 0,0
#current_time="%d" % time.localtime().tm_year+"."+"%d" % time.localtime().tm_mon+"."+ "%d" % time.localtime().tm_mday+" "+ "%d" % time.localtime().tm_hour+":"+ "%d" % time.localtime().tm_hour current_time=" "
tem_humi=get_temperature_humidity()
#print tem_humi
if tem_humi[0]!=0:
content1=current_time+" The home temperature is %d" % tem_humi[0]
content2=current_time+" The home humidity is %d" % tem_humi[1]
content=content1+"C. "+content2+"%"
send_mail(mail_to_1,'My home',content)
send_mail(mail_to_2,'My home',content)
send_mail(mail_to_3,'My home',content)
send_mail(mail_to_4,'My home',content)
#print content

【Raspberry Pi】定时运行python程序读温湿度传感器数据&发邮件的更多相关文章

  1. 设置PATH 环境变量、pyw格式、命令行运行python程序与多重剪贴板

    pyw格式简介: 与py类似,我认为他们俩卫衣的不同就是前者运行时候不显示终端窗口,后者显示 命令行运行python程序: 在我学习python的过程中我通常使用IDLE来运行程序,这一步骤太过繁琐( ...

  2. 编程语言类别;运行Python程序的方式;变量和常量;Python程序的垃圾回收机制;

    目录 编程语言分类 运行Python程序的两种方式 1.交互式 变量与常量 1.变量 2.常量 3.小整数池 垃圾回收机制 编程语言分类 编程语言分为: 1.机器语言:直接用二进制的0和1和计算机(C ...

  3. Linux-Centos 用crontab定时运行python脚本详细步骤

    服务器总是要定时运行某个程序,而我在解决这个问题的时候遇到很多困难, 特此记录下来. 1.编辑crontab配置 crontab -e 服务器一般会安装好crontab,若没有安装请按命令安装 yum ...

  4. 运行python程序

    1 在windows下运行python程序 1)从DOS命令行运行python脚本 用python解释器来执行python脚本,在windows下面python解释器是python.exe,我的pyt ...

  5. editplus3运行Python程序

    editplus3是一款不错的编辑器,他可以编译,运行java,php等各种程序,现把他运行Python程序的方法贴出来,首先得安装python,然后打开editplug3,工具——配置用户工具——组 ...

  6. 如何使用sublime编辑器运行python程序

    现在越发喜欢sublime编辑器了,不仅界面友好美观.文艺,可扩展性还特别强. sublime本身是不具备运行python程序的能力的,需要做些设置才可以.以下是安装好sublime后设置的步骤: 点 ...

  7. Linux(9)后台运行python程序并输出到日志文件

    后台运行python程序并标准输出到文件 现在有test.py程序要后台部署, 里面有输出内容 使用命令: nohup python -u test.py > test.log 2>&am ...

  8. 周一02.3运行python程序的两种方式

    一.运行python程序的两种方式 方法一:交互式:                     优点:输入一行代码立刻返回结果                      缺点:无法永久保存代码 方法二: ...

  9. 解释器、环境变量、如何运行python程序、变量先定义后引用

    python解释器的介绍.解释器的安装.环境变量的添加为什么加环境变量.如何调取不同的解释器版本实现多版本共存.python程序如何运行的.python的变量定义 一.python解释器: 用来翻译语 ...

随机推荐

  1. STL源码剖析(算法)

    STL中算法是基于迭代器来实现的. 有了容器中迭代器的实现(对operator*.operator++等的重载),STL中大部分算法实现就显得很简单了. 先看一例关于find算法的实现: templa ...

  2. monogdb windows环境下 安装及使用简单示例

    1,下载地址:http://www.mongodb.org/downloads,选择windows平台,当前最新的版本是:2.6.4,本机是64位win7系统,我选择的是windows 64位msi, ...

  3. 动态规划初级 入门理解 C#代码

      using System; using System.Collections.Generic; using System.Linq; using System.Text; using Micros ...

  4. css定位positon

    值 描述 absolute 生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位. 元素的位置通过 "left", "top", " ...

  5. 【Android】Architecture Components最佳实践--Lifecycles

    UI controllers (activities and fragments) 中代码越少越好,不应该自己去请求数据,而是用ViewModel来更新数据,并且监听LiveData来更新UI UI ...

  6. android获取系统应用大小的方法

    <span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-s ...

  7. ExtJS学习------Ext.define的继承extend,用javascript实现相似Ext的继承

    (1)Ext.define的继承extend 详细实例: Ext.onReady(function(){ //Sup Class 父类 Ext.define('Person',{ config:{ n ...

  8. Jetty - Container源码分析

    1. 描述 Container提供管理bean的能力. 基于Jetty-9.4.8.v20171121. 1.1 API public interface Container { // 增加一个bea ...

  9. 分页技术框架(Pager-taglib)学习三(pager-taglib中传递参数时中文乱码问题)

    一.问题描述 问题: 使用<pg:param name="key" />标签传递中文参数时,会有乱码. 原因: 因为它默认是用gb2312来对添加的参数进行编码,如果你 ...

  10. poj 3017 Cut the Sequence(单调队列优化 )

    题目链接:http://poj.org/problem?id=3017 题意:给你一个长度为n的数列,要求把这个数列划分为任意块,每块的元素和小于m,使得所有块的最大值的和最小 分析:这题很快就能想到 ...