每 60 秒复制一个文件 bash
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21572267/
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
Copy a file every 60 seconds bash
提问by Shannon Hochkins
I have a thecus nas server, and they seem to do some tricky things to their templates to display their files, currently at boot I'm running a shell command to copy one file over another, so that It boots with my custom template, however after a certain amount of time (I'm not sure what this time is) it overwrites it again with the original and my custom template is gone.
我有一个 thecus nas 服务器,他们似乎对他们的模板做了一些棘手的事情来显示他们的文件,目前在启动时我正在运行一个 shell 命令来将一个文件复制到另一个文件上,以便它使用我的自定义模板启动,但是一段时间后(我不确定这次是什么),它再次用原始模板覆盖它,而我的自定义模板消失了。
Here is my current boot script:
这是我当前的启动脚本:
#!/bin/bash
cp /customTemplates/login.tpl /www/img/templates/adm/login.tpl
Is there a way, to perform that copy command, say every 60 seconds? the login.tpl file is only 2kb, so I wouldn't think this could cause any problems.
有没有办法执行该复制命令,例如每 60 秒执行一次?login.tpl 文件只有 2kb,所以我认为这不会导致任何问题。
Is there anything wrong with doing this, this way? Or is there another trick I could use?
这样做有什么问题吗?或者我可以使用其他技巧吗?
回答by Reinstate Monica Please
To answer the general question, two ways to do this, put it in a while/sleep loop or use a crontab
要回答一般问题,有两种方法可以做到这一点,将其放入 while/sleep 循环或使用 crontab
1)while/sleep
1)当/睡觉
#!/bin/bash
while true; do
cp -f /customTemplates/login.tpl /www/img/templates/adm/login.tpl
sleep 60
done
2)crontab (preferred)
2)crontab (首选)
Run crontab -e
and put the following line there
运行crontab -e
并将以下行放在那里
* * * * * cp -f /customTemplates/login.tpl /www/img/templates/adm/login.tpl
This will run the command every minute of every hour of every day of every month of every day of the week. (ergo every 60 seconds)
这将在一周中的每一天的每个月的每一天的每一小时的每一分钟运行命令。(ergo 每 60 秒)
But, as Aaron Digulla saidit would be better to get to where it's pulling the config from and edit it there, rather then overwriting it every 60 seconds.
但是,正如Aaron Digulla 所说,最好到达它从哪里提取配置并在那里编辑它,而不是每 60 秒覆盖一次。
回答by Aaron Digulla
A better solution would be to find out where the NAS takes the original file from.
更好的解决方案是找出 NAS 从何处获取原始文件。
Execute this command to find possible candidates:
执行此命令以查找可能的候选人:
find / -name "login.tpl"
You can also use patterns: "*login*"
您还可以使用模式: "*login*"
I suggest to use the quotes when you use patterns, otherwise it might match files in the local folder and the shell will then replace the name beforeit passes the argument to find
.
我建议在使用模式时使用引号,否则它可能会匹配本地文件夹中的文件,然后 shell 将在将参数传递给find
.
To find out who changes the file, use auditctl
(see this question).
要找出谁更改了文件,请使用auditctl
(请参阅此问题)。
回答by MLSC
try the following solution:
尝试以下解决方案:
chmod +x /tmp/test.sh
your script test.sh:
你的脚本 test.sh:
#!/bin/bash
cp -f /customTemplates/login.tpl /www/img/templates/adm/login.tpl
then do:
然后做:
touch /tmp/cronjob
echo "* * * * * ./test.sh" > /tmp/cronjob
/etc/init.d/crond/start
crontab /tmp/cronjob
回答by alexanderbird
If you're looking for a throw-away, one time sort of solution, you can use the watch utility
如果您正在寻找一次性的、一次性的解决方案,您可以使用 watch 实用程序
watch -n 60 ./test.sh
runs ./test.sh every 60 seconds and echoes the output to stdout
每 60 秒运行 ./test.sh 并将输出回显到 stdout