bash 使用 shell 脚本中的 ssh 登录在服务器上创建 cron 条目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5034333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating cron entry on server using ssh login within shell script
提问by Wettstein Roland
I need to upload a file (bash script) to a remote sever. I use the scp command. After the file has been copied to the remote server I want to create a cron entry in the crontab file on the remote server. However, the file upload and writing the cron entry need to occur within a bash shell script so that I only need to execute the script on my local machine and the script is copied to the remote host and the cron entry is written to the crontab.
我需要将文件(bash 脚本)上传到远程服务器。我使用 scp 命令。将文件复制到远程服务器后,我想在远程服务器上的 crontab 文件中创建一个 cron 条目。但是,文件上传和写入 cron 条目需要在 bash shell 脚本中进行,因此我只需要在本地机器上执行脚本并将脚本复制到远程主机并将 cron 条目写入 crontab。
Is there a way that I can use an ssh command, within the script, that logs me into the remote server, opens the crontab file and writes the cron entry.
有没有一种方法可以在脚本中使用 ssh 命令,将我登录到远程服务器,打开 crontab 文件并写入 cron 条目。
Any help is very welcome
非常欢迎任何帮助
回答by glenn Hymanman
I would:
我会:
- extract the user's crontab with
crontab -l > somefile - modify that file with the desired job
- import the new crontab with
crontab somefile
- 提取用户的 crontab
crontab -l > somefile - 使用所需的作业修改该文件
- 导入新的 crontab
crontab somefile
回答by MobiusTrip
I just did something like this where I needed to create a multiline line crontab on a remote machine. By far the simplest solution was to pipe the content to the remote crontab command through ssh like this:
我只是做了这样的事情,我需要在远程机器上创建一个多行的 crontab。到目前为止,最简单的解决方案是通过 ssh 将内容通过管道传输到远程 crontab 命令,如下所示:
echo "$CRON_CONTENTS" | ssh username@server crontab
回答by Mat Schaffer
mailo seemed almost right, but the command would be the second argument to the ssh command, like this:
mailo 似乎几乎是正确的,但该命令将是 ssh 命令的第二个参数,如下所示:
ssh username@server 'echo "* * * * * /path/to/script/" >> /etc/crontab'
Or if your system doesn't automatically load /etc/crontab you should be able to pipe to the crontab command like this:
或者,如果您的系统没有自动加载 /etc/crontab,您应该能够像这样通过管道连接到 crontab 命令:
ssh username@server 'echo "* * * * * myscript" | /usr/bin/crontab'
回答by tripleee
Say you want to copy $localto $remoteon $hostand add an hourly job there to run at 14 past every hour, using a single SSH session;
假设您想复制$local到$remoteon$host并在那里添加一个每小时工作,以使用单个 SSH 会话在每小时 14 点运行;
ssh "$host" "cat >'$remote' &&
chmod +x '$remote' &&
( crontab -l;
echo '14 * * * * $remote' ) | crontab" <"$local"
This could obviously be much more robust with proper error checking etc, but hopefully it should at least get you started.
通过适当的错误检查等,这显然会更加健壮,但希望它至少能让你开始。
The two keys here are that the sshcommand accepts an arbitrarily complex shell script as the remote command, and gets its standard input from the local host.
这里的两个关键是ssh命令接受任意复杂的shell脚本作为远程命令,并从本地主机获取其标准输入。
(With double quotes around the script, all variables will be interpolated on the local host; so the command executed on the remote host will be something like cat >'/path/to/remote' && chmod +x '/path/to/remote' && ...With the single quotes, you could have whitespace in the file name, but I didn't put them in the crontabentry because it's so weird. If you need single quotes there as well, I believe it should work.)
(在脚本周围使用双引号,所有变量都将在本地主机上插入;因此在远程主机上执行的命令将类似于cat >'/path/to/remote' && chmod +x '/path/to/remote' && ...使用单引号,您可以在文件名中包含空格,但我没有将它们在crontab条目中,因为它太奇怪了。如果你也需要单引号,我相信它应该可以工作。)
回答by mailo
You meant something like
你的意思是这样的
ssh [email protected] && echo "* * * * * /path/to/script/" >> /etc/crontab
?
?

