crontab是否有不使用编辑器(crontab -e)创建cron作业的参数?如果是,从Bash脚本创建cron作业的代码是什么?


当前回答

您可以向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来源。

其他回答

CRON="1 2 3 4 5 /root/bin/backup.sh" 
cat < (crontab -l) |grep -v "${CRON}" < (echo "${CRON}")

给grep exact命令添加-w参数,不带-w参数添加cronjob "testing"会导致删除cronjob "testing123"

脚本函数添加/删除cronjob。无重复条目:

cronjob_editor () {         
# usage: cronjob_editor '<interval>' '<command>' <add|remove>

if [[ -z "$1" ]] ;then printf " no interval specified\n" ;fi
if [[ -z "$2" ]] ;then printf " no command specified\n" ;fi
if [[ -z "$3" ]] ;then printf " no action specified\n" ;fi

if [[ "$3" == add ]] ;then
    # add cronjob, no duplication:
    ( crontab -l | grep -v -F -w "$2" ; echo "$1 $2" ) | crontab -
elif [[ "$3" == remove ]] ;then
    # remove cronjob:
    ( crontab -l | grep -v -F -w "$2" ) | crontab -
fi 
} 
cronjob_editor "$1" "$2" "$3"

测试:

$ ./cronjob_editor.sh '*/10 * * * *' 'echo "this is a test" > export_file' add
$ crontab  -l
$ */10 * * * * echo "this is a test" > export_file

Bash脚本,用于在没有交互式编辑器的情况下添加cron作业。 下面的代码帮助您使用linux文件添加cronjob。

#!/bin/bash

cron_path=/var/spool/cron/crontabs/root

#cron job to run every 10 min.
echo "*/10 * * * * command to be executed" >> $cron_path

#cron job to run every 1 hour.
echo "0 */1 * * * command to be executed" >> $cron_path

所以,在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.

很有可能您正在将此工作自动化,并且您不希望单个工作添加两次。 在这种情况下使用:

__cron="1 2 3 4 5 /root/bin/backup.sh"
cat <(crontab -l) |grep -v "${__cron}" <(echo "${__cron}")

这只在使用BASH时有效。我不知道正确的DASH (sh)语法。

更新:如果用户还没有crontab,这将不起作用。更可靠的方法是:

(crontab -l ; echo "1 2 3 4 5 /root/bin/backup.sh") | sort - | uniq - | crontab - 

或者,如果你的发行版支持它,你也可以使用一个单独的文件:

echo "1 2 3 4 5 /root/bin/backup.sh" |sudo tee /etc/crond.d/backup

在另一个SO问题中找到的。

我想找一个这样的例子,所以可能会有帮助:

COMMAND="/var/lib/postgresql/backup.sh"
CRON="0 0 * * *"
USER="postgres"
CRON_FILE="postgres-backup"
# At CRON times, the USER will run the COMMAND
echo "$CRON $USER $COMMAND" | sudo tee /etc/cron.d/$CRON_FILE
echo "Cron job created. Remove /etc/cron.d/$CRON_FILE to stop it."