python 将指定文件添加到 crontab 的脚本
python script to add specified file to crontab
我想创建 python 脚本,自动将指定文件添加到 raspberry pi 上的 crontab。
沿着这些线的东西:
(shurley 我的命令不起作用,这只是我希望它如何工作的提示)
file = "/home/pi/test/python_script.py &"
def add_to_crontab(file):
system("echo @reboot python {} >> crontab -e".format(file))
>>
用于重定向到文件,而不是命令。使用 |
传送到命令。
crontab
-e 用于交互式编辑 crontab。要自动编辑它,您需要将当前的 crontab 提取到一个文件,将新行附加到该文件,然后从该文件更新 crontab。
from tempfile import NamedTemporaryFile
import os
def add_to_crontab(file):
with os.popen("crontab -l", "r") as pipe:
current = pipe.read()
current += f"@reboot python {file}\n"
with NamedTemporaryFile("w") as f:
f.write(current)
f.flush()
system(f"crontab {f.name}")
我想创建 python 脚本,自动将指定文件添加到 raspberry pi 上的 crontab。
沿着这些线的东西:
(shurley 我的命令不起作用,这只是我希望它如何工作的提示)
file = "/home/pi/test/python_script.py &"
def add_to_crontab(file):
system("echo @reboot python {} >> crontab -e".format(file))
>>
用于重定向到文件,而不是命令。使用 |
传送到命令。
crontab
-e 用于交互式编辑 crontab。要自动编辑它,您需要将当前的 crontab 提取到一个文件,将新行附加到该文件,然后从该文件更新 crontab。
from tempfile import NamedTemporaryFile
import os
def add_to_crontab(file):
with os.popen("crontab -l", "r") as pipe:
current = pipe.read()
current += f"@reboot python {file}\n"
with NamedTemporaryFile("w") as f:
f.write(current)
f.flush()
system(f"crontab {f.name}")