Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python编辑远程UNIX服务器中的crontab文件_Python_Unix_Cron_Paramiko - Fatal编程技术网

如何使用Python编辑远程UNIX服务器中的crontab文件

如何使用Python编辑远程UNIX服务器中的crontab文件,python,unix,cron,paramiko,Python,Unix,Cron,Paramiko,我的Python代码中有一个crontab条目,描述在指定时间应该在UNIX远程服务器中调度哪个脚本 我正在编写一个Python脚本,它将使用Paramiko连接到ssh,它将转到远程服务器中指定的crontab文件路径->打开crontab文件->在文件末尾添加Python脚本中指定的crontab条目(在新行)->保存并退出crontab文件 请让我知道我如何才能做到这一点 注意:我已经知道如何使用Paramiko连接到服务器。刚刚在远程服务器的文件处理部分遇到问题。我不知道Paramiko

我的Python代码中有一个crontab条目,描述在指定时间应该在UNIX远程服务器中调度哪个脚本

我正在编写一个Python脚本,它将使用Paramiko连接到ssh,它将转到远程服务器中指定的crontab文件路径->打开crontab文件->在文件末尾添加Python脚本中指定的crontab条目(在新行)->保存并退出crontab文件

请让我知道我如何才能做到这一点


注意:我已经知道如何使用Paramiko连接到服务器。刚刚在远程服务器的文件处理部分遇到问题。

我不知道Paramiko是如何工作的,但如果您可以使用shell,您只需执行:

echo "new line" >> cron_file
此命令将添加一个字符串
新行
,作为文件
cron\u文件
的新行

crontab -l 2>/dev/null| cat - <(echo "your new crontab entry here") | crontab -
将当前crontab输出到stdout

2>/dev/null
[可选]禁止来自crontab-l的错误消息。如果用户没有crontab条目,您将收到一条错误消息。但这不是问题

cat - <(echo "your crontab entry here")
这将crontab条目设置为stdin(由于管道,它是前面命令中的所有stdout)

Edit:看起来您需要使用
bash-c
包装该命令才能使管道正常工作。看这个

或者,您可以向paramiko发送一系列命令。只是要小心并发性

crontab -l > /tmp/current.cron
echo "your crontab entry here" >> /tmp/current.cron
crontab /tmp/current.cron
另一种选择是:

crontab <(cat <(crontab -l 2>/dev/null) <(echo "your new crontab entry"))

crontab我可以在使用子流程Python模块的脚本中使用此命令吗?可以使用shell=True。但这将取决于本地系统。Paramiko用于通过ssh.com在远程系统上执行命令。我只需要通过远程服务器编辑crontab文件。在我的解决方案中发现一个错误。我已经编辑过了。注意第1类之后的QQ。我需要在远程机器上安装Python吗?
crontab -l > /tmp/current.cron
echo "your crontab entry here" >> /tmp/current.cron
crontab /tmp/current.cron
crontab <(cat <(crontab -l 2>/dev/null) <(echo "your new crontab entry"))