DB-hub Technology Linux how to create a cron expression for every 2 weeks

how to create a cron expression for every 2 weeks

1)Cron Table Format

2)Shell command: date

NAME
       date - print or set the system date and time

SYNOPSIS
       date [OPTION]... [+FORMAT]
       date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

DESCRIPTION
       Display the current time in the given FORMAT, or set the system date.

       Mandatory arguments to long options are mandatory for short options too.

       -d, --date=STRING
              display time described by STRING, not 'now'
       %w     day of week (0..6); 0 is Sunday
       %W     week number of year, with Monday as first day of week (00..53)

3)Crontab

# run this job every two weeks

# This one did not work, if you know why, please let me know
#57 21 * * 0 [ (expr #(date +%W) % 2) -eq 0 ] && /home/oracle/run_job.x >/dev/null 2>&1

# Tested on Linux
01 22 * * 0 test((#(date +\%W)\%2)) -eq 1 && /home/oracle/run_job.x >/dev/null 2>&1

run_job.x

#!/bin/sh

echo `date` > /home/oracle/run_job.log

Leave a Reply

您的邮箱地址不会被公开。 必填项已用 * 标注

Related Post