简体   繁体   English

Unix,如何将crontab放在shell脚本中

[英]Unix, how to put crontab in a shell script

I am new to Unix and not sure how to use crontab. 我是Unix的新手,不确定如何使用crontab。 I want to write a program that will update a file every midnight. 我想编写一个程序,将在每个午夜更新文件。 0 0 * * * (Midnight every day). 0 0 * * *(每天午夜)。 I want the user to enter a value for in_variable(please look at the following code) only the first time when the program runs, and do the rest Every Midnight (without prompting the user to enter anymore values after the very first time). 我希望用户仅在程序第一次运行时输入in_variable的值(请看下面的代码),其余的工作在“午夜”进行(不提示用户在第一次后再输入任何值)。 depending on the old input(in_variable), the program should execute the if else statement every midnight. 根据旧的输入(in_variable),程序应在每个午夜执行if else语句。 Please let me know if this is possible? 请让我知道是否可行? any help would be greatly appreciated. 任何帮助将不胜感激。

echo "which message would you like to output: "
read in_variable
0 0 * * * 
if [ $in_variable -eq "1" ]; then
   echo "output message 1" >> file1
else
   echo "output message 2" >> file2
fi

Cron jobs are automatic and cannot rely on any human interaction. Cron作业是自动的,不能依赖任何人机交互。

Your script should read its initial input from a file or its initialisation must be done with an interactive script (not from cron ). 您的脚本应该从文件中读取其初始输入,或者必须使用交互式脚本(而不是cron )来完成其初始化。

Also note that you don't specify the schedule in the shell script, but in the crontab itself. 另请注意,您没有在shell脚本中指定调度,而是在crontab本身中指定了调度。 That is to say, your question is back-to-front . 也就是说, 您的问题是从头至尾的 You should ask how to put a shell-script into a crontab ? 您应该问如何将shell脚本放入crontab中 For which the answer is essentially: 为此,答案基本上是:

See man cron . man cron (Linked resource is for BSD, your cron implementation may be different). (链接的资源用于BSD,您的cron实现可能有所不同)。

Make the script to ask which message to use separate of the cron job, putting the message in a file under /var/lib . 使脚本询问独立于cron作业使用哪个消息,并将消息放入/var/lib下的文件中。 Make the cron job check for presence of the file, and then handle appending the contents if present. 使cron作业检查文件是否存在,然后处理是否存在附加内容。

crontab doesn't work like that. crontab不能那样工作。 You don't put the time specification in the program that you want to run; 您没有将时间规范放在要运行的程序中; you put the time specification in a crontab file somewhere (where varies by system) which specifies both when to run the program and what program or command to run. 您将时间指定放在crontab文件中(该位置随系统而异),该文件指定了何时运行程序以及要运行什么程序或命令。

Ignacio has it right regarding what to do with the variable: store the variable in a file in a fixed location, then have your script check for the existence of the file. Ignacio正确处理变量:将变量存储在固定位置的文件中,然后让脚本检查文件是否存在。 Cron will have nothing to do with that part. Cron与这部分无关。

A quick ugly hack (more about why it is a hack after) would be: 一个快速的丑陋的骇客(更多有关为什么以后会成为骇客)将是:

echo "which message would you like to output: "
read in_variable

if [ $in_variable -eq "1" ]; then
   echo '0 0 * * * echo "output message 1" >> file1' | crontab -
else
   echo '0 0 * * * echo "output message 2" >> file2' | crontab -
fi

This is ugly because it erases the current crontab entirely. 这很丑陋,因为它会完全删除当前的crontab。 A more sophisticated answer would provide some sort of 'tag' for the new line being added to the crontab, and overwrite the old line if someone runs the script a second time. 一个更复杂的答案将为添加到crontab的新行提供某种“标签”,并且如果有人第二次运行脚本,则覆盖旧行。 It would also provide a mechanism to remove the line, all while maintaining the preexisting cron table. 它还将提供一种删除该行的机制,同时保持先前存在的cron表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM