Tux

...making Linux just a little more fun!

Help with Bash script

Chiew May Cheong [chiew_may at hotmail.com]


Tue, 26 Jun 2007 05:19:29 +0000

I have got a bash script called del_level0.sh that runs every Friday that looks like this:

#!/bin/bash
cd /u1/database/prod/level0
rm *
There's a cron entry for this script that runs every Friday:

linux:/usr/local/bin # crontab -l
 
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.XXXXvi5yHq installed on Tue Jun 26 14:13:04 2007)
# (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)
0 15 * * 5 /usr/local/bin/del_level0.sh > /dev/null 2>&1
Can you help me so that the script runs at 3pm every 2nd and 4th Friday of the month?

Thanks.

Regards,

Chiew May


Top    Back


Ramanathan Muthaiah [rus.cahimb at gmail.com]


Tue, 26 Jun 2007 11:04:05 +0530

> Can you help me so that the script runs at 3pm every 2nd and 4th Friday of
> the month?

Running, "info crontab" or "man crontab", should provide the details.

/Ram


Top    Back


Thomas Adam [thomas.adam22 at gmail.com]


Tue, 26 Jun 2007 06:43:09 +0100

On 26/06/07, Chiew May Cheong <chiew_may@hotmail.com> wrote:

> I have got a bash script called del_level0.sh that runs every Friday that
> looks like this:
> #!/bin/bash
> cd /u1/database/prod/level0
> rm *

Hmm. Presumably there's no directories under there. What if the number of files increases? The globbing will soon fail after that. Better off:

find . -type f -print0 | xargs -0 rm
That would recurse though. If you wanted to restrict to that to just the one directory (which is what the glob would do):

find . -type f -maxdepth 1 -print0 | xargs -0 rm
> There's a cron entry for this script that runs every Friday:
>
> 0 15 * * 5 /usr/local/bin/del_level0.sh > /dev/null 2>&1
>
> Can you help me so that the script runs at 3pm every 2nd and 4th Friday of
> the month?

Vixie Cron won't let you do this, however, you could do something like this:

3 15 7-14, 21-30 * 5 test 'date +\%a' != Fri ||
/usr/local/bin/del_level0.sh > /dev/null 2>&1
The idea here is that we try and restrict the range of dates the 2nd and 4th Friday in any given month can be in by checking for Fri in that range.

-- 
Thomas Adam

Top    Back


Ben Okopnik [ben at linuxgazette.net]


Tue, 26 Jun 2007 16:33:58 -0400

On Tue, Jun 26, 2007 at 06:43:09AM +0100, Thomas Adam wrote:

> On 26/06/07, Chiew May Cheong <chiew_may@hotmail.com> wrote:
> 
> > There's a cron entry for this script that runs every Friday:
> >
> > 0 15 * * 5 /usr/local/bin/del_level0.sh > /dev/null 2>&1
> >
> > Can you help me so that the script runs at 3pm every 2nd and 4th Friday of
> > the month?
> 
> Vixie Cron won't let you do this, however, you could do something like this:
> 
> ``
> 3 15 7-14, 21-30 * 5 test 'date +\%a' != Fri ||
> /usr/local/bin/del_level0.sh > /dev/null 2>&1
> ''
> 
> The idea here is that we try and restrict the range of dates the 2nd
> and 4th Friday in any given month can be in by checking for Fri in
> that range.

You could also split the job between the script and the cronjob instead of trying to make it all happen in one place.

# In the crontab
3 15 7-14, 21-30 * 5 /usr/local/bin/del_level0.sh > /dev/null 2>&1
 
# In the script
[ `/bin/date +%a` == 'Fri' ] || exit
-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * https://LinuxGazette.NET *

Top    Back


Thomas Adam [thomas.adam22 at gmail.com]


Tue, 26 Jun 2007 23:44:53 +0100

On 26/06/07, Ben Okopnik <ben@linuxgazette.net> wrote:

>
> You could also split the job between the script and the cronjob instead
> of trying to make it all happen in one place.

Yes, you could, but so what? It's only one line that doesn't do anything complex, the only real caveat is to remember to escape the percent signs to the date command.

-- 
Thomas Adam

Top    Back


Ben Okopnik [ben at linuxgazette.net]


Tue, 26 Jun 2007 22:54:53 -0400

On Tue, Jun 26, 2007 at 11:44:53PM +0100, Thomas Adam wrote:

> On 26/06/07, Ben Okopnik <ben@linuxgazette.net> wrote:
> >
> > You could also split the job between the script and the cronjob instead
> > of trying to make it all happen in one place.
> 
> Yes, you could, but so what?  It's only one line that doesn't do
> anything complex, the only real caveat is to remember to escape the
> percent signs to the date command.

Which, in my opinion, makes it more complex than it needs to be. Intermixing two different syntaxes when it's unnecesary does not seem like a reasonable approach.

In the case that I demonstrated, 'cron' does its job and interprets its own syntax, and Bash does its part; that makes any problems easier to troubleshoot. Incidentally, the redirect to /dev/null is also most likely unnecessary - 'cron' doesn't run in a terminal.

-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * https://LinuxGazette.NET *

Top    Back