Ask a Question related to Debian, Design and Development.

  1. #1

    Default cron bash date

    Hello,

    I am trying to run a bash script in cron. I originally wanted
    it to run at 11:59pm on the last day of every month. February
    will always cause problems because of leap years. Therefore,
    I decided to run cron on the first day of every month using
    the @month parameter.

    The problem is that I want the month to reflect the previous month.

    MON=$((`date +%m`-1))

    This will work for every month except January where instead of getting 12
    I get 0. 2 questions

    1) How can I modify the above line so that if `date *%m`-1 = 0 then MON=12
    2) I want the output to be 01-09 and not 1-9. How can I change the format
    to add a zero? +%m usually adds a zero but subtracting 1 causes the 0
    to be removed.

    Lance






    --
    To UNSUBSCRIBE, email to [email]debian-user-request@lists.debian.org[/email]
    with a subject of "unsubscribe". Trouble? Contact [email]listmaster@lists.debian.org[/email]
    Lance Hoffmeyer Guest

  2. Similar Questions and Discussions

    1. time -l date ==> bash: -l: command not found Bug?
      "time" doesn't seem to accept any options. The first thing on the line after "time" is taken as the utility to execute. I need the -l option. Am I...
    2. problem using bash variables with command-line perl in bash script
      Hi! I have a problem with variables when using command-line perl in a bash script. The script should update a date (in 2003-10-10 form) if the...
    3. Bash quoting
      On Thu, 31 Jul 2003 10:32:42 +1200, Jochen Daum <jochen.daum@cans.co.nz> wrote: http://uk.php.net/manual/en/function.escapeshellarg.php --...
    4. bash script help
      okay, i want a script i want in bash to log into a telnet session and use a file temp.txt to get the user name something like: telnet -l...
    5. bash: let exp ? exp : exp problem
      Hello, I am trying to get a bash script to work which will hibernate till 1:00 am, it appears to work OK # ensure script executes next at 1:00am...
  3. #2

    Default Re: cron bash date

    Lance Hoffmeyer <lance@augustmail.com> writes:
    > I am trying to run a bash script in cron. I originally wanted
    > it to run at 11:59pm on the last day of every month. February
    > will always cause problems because of leap years. Therefore,
    > I decided to run cron on the first day of every month using
    > the @month parameter.
    crontab(5) doesn't suggest a good way to do this, no. So yeah,
    running early (say, 12:03 AM) on the first of the month is easy:

    3 0 1 * * /usr/local/sbin/ourscript
    > The problem is that I want the month to reflect the previous month.
    >
    > MON=$((`date +%m`-1))
    How about:

    MON=`date -d yesterday +%m`

    date(1) seems to think "yesterday" is exactly 24 hours ago, if you're
    looking at other fields. (Or maybe it's exactly a day ago; this makes
    a difference around daylight savings.)

    --
    David Maze [email]dmaze@debian.org[/email] [url]http://people.debian.org/~dmaze/[/url]
    "Theoretical politics is interesting. Politicking should be illegal."
    -- Abra Mitchell


    --
    To UNSUBSCRIBE, email to [email]debian-user-request@lists.debian.org[/email]
    with a subject of "unsubscribe". Trouble? Contact [email]listmaster@lists.debian.org[/email]
    David Z Maze Guest

  4. #3

    Default Re: cron bash date

    * David Z Maze (dmaze@debian.org) [030801 07:13]:
    > Lance Hoffmeyer <lance@augustmail.com> writes:
    >
    > > I am trying to run a bash script in cron. I originally wanted
    > > it to run at 11:59pm on the last day of every month. February
    > > will always cause problems because of leap years. Therefore,
    > > I decided to run cron on the first day of every month using
    > > the @month parameter.
    >
    > crontab(5) doesn't suggest a good way to do this, no. So yeah,
    > running early (say, 12:03 AM) on the first of the month is easy:
    >
    > 3 0 1 * * /usr/local/sbin/ourscript
    >
    > > The problem is that I want the month to reflect the previous month.
    > >
    > > MON=$((`date +%m`-1))
    >
    > How about:
    >
    > MON=`date -d yesterday +%m`
    Oh yeah, I forgot about that, duh! =p That's way cleaner than my daily
    touching a file idea.
    > date(1) seems to think "yesterday" is exactly 24 hours ago, if you're
    > looking at other fields. (Or maybe it's exactly a day ago; this makes
    > a difference around daylight savings.)
    date actually understands a pretty wide array of expressions. So if
    "yesterday" isn't to your liking, you can also use $(date -d "24 hours
    ago"). Also, I think the OP will then easily see how to have a script
    running early on the first of the month can give the date of 23:59
    yesterday, by, say, running at 00:05 and asking date for "6 minutes
    ago".

    good times,
    Vineet
    --
    [url]http://www.doorstop.net/[/url]
    --
    --Nick Moffitt
    A: No.
    Q: Should I include quotations after my reply?

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.2 (GNU/Linux)

    iD8DBQE/KtzW1nIkYEQE+iMRAvQMAJ4iHy3MdCgjsE6fOho9A7UM+zcUpw CeOYB/
    wxjOUc07TVE4Ho/653tn2wI=
    =zGiI
    -----END PGP SIGNATURE-----

    Vineet Kumar Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139