【问题标题】:How to install multiple cron jobs?如何安装多个 cron 作业?
【发布时间】:2015-04-16 13:44:22
【问题描述】:

以下代码用于使用 shellscript 同时安装多个 cron 作业

#!/bin/bash
    file="/home/admin/Desktop/crontab.sh"
    file1="/home/admin/Desktop/crontab1.sh"
    file2="/home/admin/Desktop/crontab2.sh"
    file3="/home/admin/Desktop/crontab3.sh"
    echo "$1 $2 $3 $4 $5 $file" >> cron.new
    echo "$6 $7 $8 $9 $10 $file1" >> cron.new
    echo "$11 $12 $3 $14 $15 $file2" >> cron.new
    echo "$16 $17 $18 $19 $25 $file3" >> cron.new
    cat cron.new
    crontab cron.new

【问题讨论】:

  • 呃,好的,有什么问题吗?是“我可以这样吗?”
  • @David C. Rankin 我需要使用单个 shell 脚本为不同的文件安装多个 cron 作业
  • 好的,这更有意义。感谢上帝,您删除了所有位置参数$1, $2, ...,这看起来像是一场噩梦。如果您不需要创建此on-the-fly,为什么不简单地使用crontab -e 创建一个启动多个脚本的 crontab?虽然使用add, list, or remove cron 条目和crontab 是可以的,但如果您只是使用硬连线 脚本来执行此操作,您不妨使用crontab -e
  • 我正在从 UI 获取位置参数值

标签: linux shell crontab cron-task cronexpression


【解决方案1】:

Bash 位置参数从0 开始,以9 结束。因此,您必须将所有位置参数作为单个参数发送。即将所有参数括在单引号或双引号内。

以下程序将为您提供帮助。

#!/bin/bash

files=("/home/admin/Desktop/crontab.sh" "/home/admin/Desktop/crontab1.sh" "/home/admin/Desktop/crontab2.sh" "/home/admin/Desktop/crontab3.sh")
args=($(echo $1))

for file in ${files[@]}
do
    list=(${args[@]:0:5})
    args=(${args[@]:5})

    for i in $(seq 0 4)
    do
        cmd="${cmd} ${list[$i]}"
    done

    echo "$cmd $file"
    cmd=''
done

输出:

$ ./test.sh '1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20'
 1 2 3 4 5 /home/admin/Desktop/crontab.sh
 6 7 8 9 10 /home/admin/Desktop/crontab1.sh
 11 12 13 14 15 /home/admin/Desktop/crontab2.sh
 16 17 18 19 20 /home/admin/Desktop/crontab3.sh

这里的数字通过将它们括在单引号中作为单个参数传递。文件名被放置在一个数组中以便于访问。

【讨论】:

    猜你喜欢
    • 2016-06-25
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    • 2011-01-03
    • 2012-02-23
    • 2016-10-06
    相关资源
    最近更新 更多