centos7 开机执行Python文件

下面提供两种方法:
1.利用linux 定时任务
2./etc/rc.d/rc.local 在该文件内末尾,写入要执行的文件

这里只做演示

1.crontab -e 进入到编辑模式,该演示用的是Python3,

* * * * * python3 /xx/xx.py         此命令代表每分钟执行一次该目录下的该文件,
* * * * * sleep 2;python3 /xx/xx.py  该命令代表每隔两秒钟执行一次该文件

2.vi /etc/rc.d/rc.local 进入到编辑模式,

#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

# 这里是开机执行某文件夹下的xx.py文件
python3 /xx/xx.py

# 开机启动定时任务
systemctl start crond.service

你可能感兴趣的:(centos)