在cronjob中使用linux date命令

时间:2016-08-10 13:40:54

标签: linux date crontab

我使用linux' date' cronjob中的命令。

Linux版本:CentOS Linux 7(核心)

我需要的是9天前的日期作为test.sh脚本的参数。 今天是2016年8月10日,需要执行的工作是:

/scripts/test.sh 2016-08-01

我在crontab中的代码:

DATEVAR=$(date +%F --date="9 days ago")
0 12 * * tue ~/scripts/test.sh $($DATEVAR)

所以linux命令是' date +%F - 日期=" 9天前"'但我需要将其执行并设置为参数。 它现在所做的是使用参数' $(date':

)运行脚本
~/scripts/test.sh $(date

我尝试使用以下内容设置DATEVAR,但没有成功:

DATEVAR='date +%F --date="9 days ago"'   
DATEVAR=date +%F --date="9 days ago"
DATEVAR=$(date +%F --date="9 days ago")
DATEVAR=(shell date +%F --date="9 days ago")

有没有人知道这是否可行以及如何设置我的DATEVAR以及执行' date'命令?

2 个答案:

答案 0 :(得分:0)

我不确定你是否可以在cron工作中做到这一点。

一个简单的解决方法是将您对test.sh的调用包装成另一个没有参数的脚本。在其中,只需写下:

DATEVAR=$(date +%F --date="9 days ago")
~/scripts/test.sh $($DATEVAR)

在你的cronjob中打电话给你。

答案 1 :(得分:0)

这应该有效:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
class Solution
{
    static string ReplacementForSteadiness(string s)
    {   
        var counter = new Dictionary<char,int>() {
            { 'A', 0 }, { 'C', 0 }, { 'G', 0 }, { 'T', 0 }
        };
        for(int i = 0; i < s.Length; ++i)
                counter[s[i]] += 1;

        int div = s.Length / 4;

        var pairs = counter.ToList();
        if(pairs.All(p => p.Value == div))
            return "";

        // If here, that means there is an even count of characters in s. For example, if
        // s = "AAATGTTCTTGCGGGG", then counter = { A -> 3, T -> 5, C -> 2, G -> 6 },
        // div = 4, and we know that we need to increase the number of As by 1, decrease 
        // the number of Ts by 1, increase the number of Cs by 2 and decrease the number
        // of Gs by 2.

        // The smallest strings to replace will have 1 T and 2 Gs, to be replaced with 1 A and
        // 2 Cs (The order of characters in the replacement string doesn't matter).
        // "TGG" --> "ACC" 
        // "GTG" --> "ACC"
        // "GGT" --> "ACC"

        // None of those strings exist in s. The next smallest strings that could be replaced
        // would have 1 T and 3Gs, to be replaced with 1 A and 2 of the Gs to be replaced with
        // Cs. Or, 2 Ts and 2Gs, 1 of the Ts to be replaced by an A and both the Gs to be replaced
        // by Cs.
        // "TGGG" --> "AGCC"
        // "GTGG" --> "AGCC"
        // "GGTG" --> "AGCC"
        // "GGGT" --> "AGCC"
        // "TTGG" --> "ATCC"
        // "TGTG" --> "ATCC"
        // "GTGT" --> "ATCC"
        // "GGTT" --> "ATCC"

        // None of those strings exist in s. Etc.      

        string r;

        // ... 

        return r;
    }

    static void Main(String[] args)
    {
       Console.ReadLine(); // n
       string str = Console.ReadLine();
       string replacement = ReplacementForSteadiness(str);
       Console.WriteLine(replacement.Length);
    }
}

不幸的是,这里有几个问题需要克服。首先,cron现在支持“环境设置”这一事实可能会产生误导。具有shell脚本经验的人可能很容易认为shell的全部功能都可以在这里使用 - 它不能。这些设置和它们一样愚蠢:它们是严格的逐字替换。在我看来,一个更好的绰号将是“占位符分配。”

其次,您希望使用的日期字符串有些复杂。具体来说,它包含一个嵌入的引用字符串(“9天前”)和一个特殊的crontab字符(惊讶!):表达式'+%F'中的'%'。 Cron用换行符替换'%' - 这是一个不错的功能,但如果你不知道或忘记它,那就太惊人了。引号不能在分配阶段存活下来。

知道这一点,使用“占位符分配”的上述条目的替代方法是:

0 12 * * tue  _DV=`date +\%F --date="9 days ago"`; ~/scripts/test.sh $_DV

在这里,我们正在捕捉我们想要直接替换的片段而不进行任何插值。

有 - 两个解决方案的价格一个!

相关问题