On Fri, 29 Jun 2018 10:54:57 -0400, Lew Pitcher wrote:
Bit Twister wrote:
On Thu, 28 Jun 2018 17:25:39 -0400, Lew Pitcher wrote:
Bit Twister wrote:
On Sat, 23 Jun 2018 22:06:57 -0700, tom wrote:
On Sat, 23 Jun 2018 18:46:10 -0700 (PDT)
[email protected] wrote:
On Tuesday, June 23, 1998 at 3:00:00 AM UTC-4, Rodney Mach wrote:
What is the best way to run a job on the last day of
every month?
<snip>
Yeah but he asked how to do it in Cron. I am curious as well.
Sounds easy to me. Put a script in cron daily and use the calendar
program to get last day of month and compare with current day of month. >>>> If current day is not last day exit. Suggested reading:
man date
man cal
man test
For example:
#!/bin/bash
Current_Day_of_Month=$(date '+%_d')
Last_Day_of_Month=$(set $(cal);shift $(( $# - 1));echo $1)
if [ $Current_Day_of_Month -ne $Last_Day_of_Month ] ; then
exit 0
fi
code for last day of month goes here
Alternatively....
#!/bin/bash
# If tomorrow is the 1st of the month,
# then today is the last day of the month
[ $(date +%-d -d tomorrow) != 1 ] && exit 0
# code for last day of the month goes here
Cute. I like it. I wish I could think like that.
It may be possible (should be possible, but I've never tried it) to incorporate this test directly into the crontab entry, rather than requiring the crontab entry to run a shell script.
Something like
1 0 28-31 * * [ $(date +%-d -d tomorrow) = 1 ] && do_this
where
do_this
is the path to the executable to be executed on the last day of the month.
Personally, I do not want to mess with crontab. I do clean installs on
each new release of my Linux install, so that is one less file to
change. I'll let the scripts decide if it is the date to do something.
To reduce my admin work, I have set users users to have their own cron subsystem and my system /local/,cron setup. That way all anyone has to
do is drop a script into the desired queue.
$ ls -Al /etc/cron* | grep local_cron_job
lrwxrwxrwx 1 root root 25 Jun 14 12:33 _daily -> /local/bin/local_cron_job lrwxrwxrwx 1 root root 25 Jun 14 12:33 _hourly -> /local/bin/local_cron_job lrwxrwxrwx 1 root root 25 Jun 14 12:33 _monthly -> /local/bin/local_cron_job lrwxrwxrwx 1 root root 25 Jun 14 12:33 _weekly -> /local/bin/local_cron_job
]$ cat /local/bin/local_cron_job
#!/bin/bash #************************************************************************
#*
#* local_cron_job - cron job to run /local/cron/(hourly|daily...) jobs
#*
#*
#* Usually called from /etc/cron.hourly, daily, monthly,.....
#*
#* Usage: link _name to /local/bin/local_cron_job
#*
#* Example: cd /etc/cron.hourly
#* ln -s /local/bin/local_cron_job _hourly
#* Leading underscore on _name is important
#* because it is removed to get directory name in /local/cron and
#* causes it to be executed before any other jobs in the directory.
#*
#***********************************************************************
/local/bin/cronlock check
if [ $? -eq 0 ] ; then
exit 0
fi
_exe=${0##*/}
_job=${_exe:1}
if [ ! -d /local/cron/$_job ] ; then
echo "$_exe FATAL error:"
echo "$0 link is incorrect"
echo "or /local/cron/$_job does not exist"
/bin/false
else
nice -n 19 run-parts --report /local/cron/$_job
/bin/true
fi
#*********************** end local_cron_job *******************************
User cron jobs are setup the same way. For example:
# cat /var/spool/cron/bittwister
#*************** start of $HOME/.cron/cron.job ***************************
#
####################################################################
# syntax example #
#minute (0-59), #
#| hour (0-23), #
#| | day of the month (1-31), #
#| | | month of the year (1-12), #
#| | | | day of the week (0-6 with 0=Sunday)#
#| | | | | commands #
#30 01 * * * /home/jim/bin/cleartmp # #################################################################### SHELL=/bin/bash
MAILTO=bittwister
HOME=/home/bittwister
_cron_loc=/home/bittwister
PATH=/usr/local/sbin:/usr/sbin\
:/usr/local/bin:/usr/bin:/local/bin:/usr/games\
:/home/bittwister/local/bin
#
31 0-23 * * * /bin/nice -n 19 /usr/bin/run-parts $_cron_loc/.cron/hourly
25 5 * * * /bin/nice -n 19 /usr/bin/run-parts $_cron_loc/.cron/daily
30 5 * * 0 /bin/nice -n 19 /usr/bin/run-parts $_cron_loc/.cron/weekly
40 5 1 * * /bin/nice -n 19 /usr/bin/run-parts $_cron_loc/.cron/monthly
52 5 * * * /bin/nice -n 19 /local/bin/ck_mail_msg
#*
#* Install: click up a terminal,
#* su - root
#* cp ~bittwister/.cron/cron.job /var/spool/cron/bittwister
#* chown bittwister:bittwsiter /var/spool/cron/bittwister
#* chmod 600 /var/spool/cron/bittwister
#* exit
#* exit
#*
#************* end ~bittwister/.cron/cron.job *************************
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)