开机自动执行脚本
进入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
]