树莓派-Debain 自制数据库自动执行python开机脚本

开机自动执行脚本
进入linux的/etc 我的linux版本是debain 9

sudo vim /etc/rc.local

进入后第一次设置发现没有启动,重新设置绝对路径

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.


# Print the IP address

_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi
#-------------------------------------------------------------------------------

  su pi -c "python3 /home/pi/Desktop/autoscript.py &"
exit 0

我的autoscript脚本如下:

# -*- coding: utf-8 -*-
"""
Created on Wed Apr  8 12:55:45 2020

@author: wen

自动执行脚本
当前设置为:

"""

#!/user/bin/env python

import time,os,sched
schedule = sched.scheduler(time.time,time.sleep)
def perform_command(cmd,inc):
  #在inc秒后再次运行自己,即周期运行
  schedule.enter(inc, 0, perform_command, (cmd, inc))
  os.system(cmd)
def timming_exe(cmd,inc=28800):
  schedule.enter(inc,0,perform_command,(cmd,inc))
  schedule.run()#持续运行,直到计划时间队列变成空为止
print('show time after 8hs:')
timming_exe('python spyder_get_weatherV02.py')

在重启后的 ps -a进程管理下,看不到实际运行程序,而且这个程序是延时执行原理,并不符合实际应用,最多执行单个程序,需要改良采用其他方法。在执行过程中cpu会被bash占满100%。
推荐使用linux下的自动定时任务来执行挂机任务。

-> 推荐思路


[
额外参考文献:
有时我们自己在/etc/rc.d/rc.local里面增加的随机器启动的脚本和指令总是不能自动加载和启动,,机器启动后手动执行脚本又能成功,经常被搞得晕头转向的。最近我经过1天的辛苦测试和查找资料,终于解决了这问题,解决方式如下,/etc/rc.d/rc.local文件的文件头是#!/bin/sh ,我们把这修改成#!/bin/sh -x,这样系统启动后就会把/etc/rc.d/rc.local里面的指令或脚本不能执行的日志写入/var/log/messages ,我们查看messages文件内容就知道具体的问题出在哪里了

额外参考文献链接:https://www.zhihu.com/question/21039737/answer/26193092

]


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值