Cronjob setting

Hi there

There's a script I would like to run daily every 5 minutes and this job should restart every 12:03AM so it would append to a new file with the following day date format instead of running and updating continuously into one log. I am not sure of the syntaxing, what I did was to set it to run daily 1-31..any idea on how to set it to run every 5 mins and refresh at 12:03 am?

##Run memory report every 5 mins daily
*/5 * 1-31 * * /usr/local/bin/MEM_Report/MEM_daily_run

Well, it doesn't have to be exactly 12:03 AM, its just that I want a new report with the following day's date format

I was thinking of

 */5 0-23 * * * 

maybe I should wait out till tomorrow so I can see how the reports come out..I started the script just now..cron confuses me every time :confused:

what is the contents of your script?
maybe you can set name of the log as today's date (or yest) in script instead of job reset.

Hi there ygemici

I did the second setting of */5 23 * * * and that gave me a new report this morning at 11 am...so whys that! I want it to start at midnight..

[root@espcrmtestapp 03]# ls -lrt
total 956
-rw-r--r--. 1 root root 512101 Mar 19 23:55 2012-03-19
-rw-r--r--. 1 root root 453702 Mar 20 11:00 2012-03-20

This is the script

#!/bin/bash

##Run Daily MEM report

MEM_REPORT="/var/log/MEM_Report/$(date +'%m')"

MEM_DAILY="/var/log/MEM_Report/$(date +'%m')/$(date +'%F')"

cd ${MEM_REPORT}

ps aux| sort -k4 -r | head -10 >> "${MEM_DAILY}"

because midnight is zero, not 23

So if I were to run the cron every 5 minutes and refresh at midnight, how do I do it?

What timezone are you in when you are at the command prompt?

echo $TZ
date

And can you make a one-off test cron to find out the value of $TZ and the value of date when running from cron.

Please also post what Operating System and version you have.

hi hedkandi,
your script is seem OK.
but as already mentioned by @methly, system date settings and cron may use different timezone information(TZ).
check if your system (redhat derivative )

# cat /etc/crontab

or solaris

# grep ^TZ /etc/TIMEZONE

check your user's profile file that runs this cronjob
both of them are differ?

add the cronjob maybe like this

*/5 * * * * /usr/local/bin/MEM_Report/MEM_daily_run
 # cat /usr/local/bin/MEM_Report/MEM_daily_run
#!/bin/bash
## MEMORY USAGE SCRIPT
LOGF="/var/log/MEM_Report" ; cd "$LOGF" &>/dev/null || exit 1
if [ ! -d "$(date +'%m')" ] ; then
mkdir $(date +'%m');cstat=$?
else cstat=0 ; fi
if [ $cstat -eq 0 ] ; then
MEM_DAILY="$(date +'%m')/$(date +'%F')"
echo -e "\t\t\t [$(date "+%F] -->  %02k:%M:%S")">>"$MEM_DAILY"
ps aux| sort -k4 -r | head -10 >>"$MEM_DAILY"
echo "">>"$MEM_DAILY"
else
echo "Check Permission on the $LOGF folder!!"
exit 1
fi
1 Like

Newbie: Intro to cron

Using cron

Cron and Crontab usage and examples

Minute   Hour   Day of Month       Month          Day of Week        Command  
*/5       0         *                *                *             <command>

@hedkandi

Just realised that your original cron from post #1 must have been running the script every 5 minutes and therefore appending to the output file every 5 minutes. So if you look at 11:00 local time the timestamp changes ... and again at 11:05 etc. . There is nothing obvious wrong with the timestamp on that file unless it wasn't 11:00 when you looked.

What are you trying to do and what is your expected output?

1 Like

Hi methyl, ygemici

Im trying to get memory reading with the ps command every 5 minutes, 24 hours a day unless the system is down (its set up with S'pore time zone). Since the previous 2 cron settings didn't work, so I edited the cron to run 5 mins and all other paramaters set as * :

[root@EP-WCRM1 03]# ls -lrt
total 572
-rw-r--r--. 1 root root 206521 Mar 19 23:55 2012-03-19
-rw-r--r--. 1 root root 186589 Mar 20 12:15 2012-03-20
-rw-r--r--. 1 root root  82800 Mar 21 23:55 2012-03-21--------working!
-rw-r--r--. 1 root root  85050 Mar 22 15:40 2012-03-22

and this is how my cronjob looks like now:

##Run memory report every 5 mins daily
*/5 * * * * /usr/local/bin/MEM_Report/MEM_daily_run

Also, there's another script that creates an empty report file every day at 1min past midnight:

##Create daily memory log
01 00 * * * /usr/local/bin/MEM_Report/MEM_day_log

This is the expected result, which is like totally working now!

[root@EP-WCRM1 03]# more 2012-03-22
  PID USER         TIME COMMAND         %MEM
20110 root     00:00:19 scanner          3.1
 6320 root     00:08:05 nailslogd        0.7
28559 root     00:01:25 httpd            0.5
 6491 root     00:00:05 logepo           0.5
 6416 root     00:21:45 kdm_greet        0.4
25053 root     00:00:30 plasma-desktop   0.4
24783 root     00:00:05 Xvnc             0.3
25027 root     01:43:34 knotify4         0.3
25017 root     00:00:06 kded4            0.2
  PID USER         TIME COMMAND         %MEM

THANK YOU ALL!! :wink: