crontab是否有不使用编辑器(crontab -e)创建cron作业的参数?如果是,从Bash脚本创建cron作业的代码是什么?
当前回答
关于crontab的使用已经有很多不错的答案,但是没有提到更简单的方法,比如使用cron。
使用cron将利用位于/etc/crontab、/etc/cron.目录的系统文件和目录Daily、weekly、hour或/etc/cron.d/:
cat > /etc/cron.d/<job> << EOF
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root HOME=/
01 * * * * <user> <command>
EOF
在上面的例子中,我们在/etc/cron.目录中创建了一个文件D /为命令成功执行提供了环境变量,并为命令和命令本身提供了用户。该文件不应该是可执行的,名称应该只包含字母-数字和连字符(详细信息如下)。
为了给出一个彻底的答案,让我们看看crontab和cron/crond之间的区别:
crontab -- maintain tables for driving cron for individual users
对于那些希望在系统上的用户上下文中运行作业的人来说,使用crontab可能非常有意义。
cron -- daemon to execute scheduled commands
对于那些使用配置管理或希望为其他用户管理作业的人,在这种情况下,我们应该使用cron。
从手册中摘录了一些例子,告诉你应该做什么,不应该做什么:
/etc/crontab and the files in /etc/cron.d must be owned by root, and must not be group- or other-writable. In contrast to the spool area, the files under /etc/cron.d or the files under /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly and /etc/cron.monthly may also be symlinks, provided that both the symlink and the file it points to are owned by root. The files under /etc/cron.d do not need to be executable, while the files under /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly and /etc/cron.monthly do, as they are run by run-parts (see run-parts(8) for more information). Source: http://manpages.ubuntu.com/manpages/trusty/man8/cron.8.html
从系统的角度来看,以这种方式管理cron更容易,也更可扩展,但并不总是最好的解决方案。
其他回答
脚本函数添加cronjob。检查重复条目,可用表达式* > "
cronjob_creator () {
# usage: cronjob_creator '<interval>' '<command>'
if [[ -z $1 ]] ;then
printf " no interval specified\n"
elif [[ -z $2 ]] ;then
printf " no command specified\n"
else
CRONIN="/tmp/cti_tmp"
crontab -l | grep -vw "$1 $2" > "$CRONIN"
echo "$1 $2" >> $CRONIN
crontab "$CRONIN"
rm $CRONIN
fi
}
测试:
$ ./cronjob_creator.sh '*/10 * * * *' 'echo "this is a test" > export_file'
$ crontab -l
$ */10 * * * * echo "this is a test" > export_file
来源:我的大脑;)
所以,在Debian、Ubuntu和许多类似的基于Debian的发行版中……
有一个cron任务连接机制,它接受一个配置文件,将它们捆绑起来,并将它们添加到运行中的cron服务中。
你可以把一个文件放在/etc/cron.目录下D /somefilename,其中somefilename是你想要的任何东西。
sudo echo "0,15,30,45 * * * * ntpdate -u time.nist.gov" >> /etc/cron.d/vmclocksync
让我们来分解一下:
Sudo -因为您需要提升权限来更改/etc目录下的cron配置
Echo -在STD输出时创建输出的工具。printf,猫……也会起作用
-在你的字符串开头使用双引号,你是专业的
0,15,30,45 * * * * -标准的cron运行调度,每15分钟运行一次
ntpdate -u time.nist.gov -我想要运行的实际命令
-因为我的第一个双引号需要一个伙伴来关闭正在输出的行
>> -双重定向追加而不是覆盖*
/etc/cron.d/vmclocksync - vmclocksync是我选择的文件名,它在/etc/cron.d/
* if we used the > redirect, we could guarantee we only had one task entry. But, we would be at risk of blowing away any other rules in an existing file. You can decide for yourself if possible destruction with > is right or possible duplicates with >> are for you. Alternatively, you could do something convoluted or involved to check if the file name exists, if there is anything in it, and whether you are adding any kind of duplicate-- but, I have stuff to do and I can't do that for you right now.
您可以向crontab添加如下内容:
#write out current crontab
crontab -l > mycron
#echo new cron into cron file
echo "00 09 * * 1-5 echo hello" >> mycron
#install new cron file
crontab mycron
rm mycron
Cron行解释
* * * * * "command to be executed"
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
nixCraft来源。
编辑(固定覆盖):
cat <(crontab -l) <(echo "1 2 3 4 5 scripty.sh") | crontab -
不,crontab中没有修改cron文件的选项。
您必须:获取当前的cron文件(crontab -l > newfile),更改它并将新文件放置到位(crontab newfile)。
如果你熟悉perl,你可以使用Config::Crontab这个模块。
LLP安德里亚
推荐文章
- RVM不是一个函数,用' RVM use…’不会起作用
- 如何打破一个循环在Bash?
- 如何从终端机发送电子邮件?
- Linux Bash中双&和分号有什么区别?
- 如何合并2 JSON对象从2个文件使用jq?
- 在Bash中模拟do-while循环
- 在Bash中将输出赋给变量
- 如何在Mac OS X 10.6中使硬件发出哔哔声
- 从Docker容器获取环境变量
- 如何重定向标准derr和标准输出到不同的文件在同一行脚本?
- 如何循环通过一个目录递归删除具有某些扩展名的文件
- 在Bash中获取日期(比当前时间早一天)
- Linux: kill后台任务
- 在OSX中永久设置PATH环境变量
- 删除Bash脚本中的重复条目