- user : root
- host : grodoudou.seizam.com
- date : 15/06/12 19:37
- curpath : /home/yannouk
[email protected]# cd /root/
- Edit /root/exec-php-send-report.sh, see [1]
[email protected]# chmod 550 exec-php-send-report.sh
- Edit /root/exec-php-send-daily-report.sh, see [2]
[email protected]# chmod 550 exec-php-send-daily-report.sh
[email protected]# mv /var/log/wikizam-cron /var/log/wikizam-cron.old
- Edit /etc/crontab, see [3]
- "updateUsages.php execution report of 20120614 > no report found" email received
[email protected]# cat /var/log/wikizam-cron/updateUsages.php-20120615-report.log
updateUsages.php execution report of 20120615
Log file is /var/log/wikizam-cron/updateUsages.php-20120615-report.log
Starts 2012/06/15 19:55:01
Current directory is /var/wikizam/deployed
Going to execute 'php extensions/Wikiplaces/updateUsages.php --lifespan=60'
Output log:
[2012-06-15 17:55:01: Updating usages...]
OK: 2 record(s) updated
[2012-06-15 17:55:01: Archiving and resetting monthly usages...]
OK: 0 record(s) archived and reset
[2012-06-15 17:55:01: END]
Ends 2012/06/15 19:55:01
- Correct email sent and report is correctly stored, so exec-php-send-daily-report.sh works fine
- exec-php-send-report.sh will be executed later with updateSubscriptions.php and checkNextRenewals.php
- Move old report file to new folder
[email protected]# mv /var/log/wikizam-cron.old /var/log/wikizam-cron/wikizam-cron.old
- [1] Edit /root/exec-php-send-report.sh
--- old 2012-06-15 19:39:56.000000000 +0200
+++ new 2012-06-15 19:44:39.000000000 +0200
@@ -0,0 +1,108 @@
+#!/bin/bash
+
+# folders and files names date formats
+short_date_format='%Y%m%d';
+full_date_format='%Y%m%d-%H%M%S';
+
+# Where to store reports
+reports_dir='/var/log/wikizam-cron';
+
+# Who will receive report and where to store localy
+mail='root';
+
+
+# ######
+# Script
+# ######
+
+start_date=`date +'%Y/%m/%d %H:%M:%S'`;
+
+# Read php filename (and optional arguments) argument
+if [ $# -lt 1 ]
+then
+ echo "missing php filename to execute"
+ echo "if the php file require args, use \"\" to specify the php file and its args"
+ echo "usage: $0 \"php_file.php <optional_arg> <optional_arg>\" <optional_directory_to_change_to_before_exec>"
+ exit 1
+fi
+php_file_args=$1
+php_file=`echo $1 | awk '{ print $1 }'`
+php_file_name=`echo $php_file | awk 'BEGIN { FS = "/" } { print $(NF) }'`
+
+# Read optional change directory argument
+if [ $# -gt 1 ]
+then
+ if [ ! -d "$2" ]
+ then
+ echo "cannot change current directory to $2: target is not a directory"
+ exit 1
+ fi
+ if [ ! -r "$2" ]
+ then
+ echo "cannot change current directory to $2: target is not readable"
+ exit 1
+ fi
+ cd $2
+fi
+
+# Check the Php file is available
+if [ ! -e "${php_file}" ]
+then
+ echo "cannot execute ${php_file}: file doesn't exist in ${curr_dir}"
+ exit 1
+else
+ if [ ! -r "${php_file}" ]
+ then
+ echo "cannot execute ${php_file}: file is not readable"
+ exit 1
+ fi
+fi
+
+# Create report
+# -p = no error if existing, make parent directories as needed
+mkdir -p ${reports_dir};
+if [ ! -d "${reports_dir}" ]
+then
+ echo "${reports_dir} reports directory doesn't exist"
+ exit 1
+fi
+if [ ! -w "${reports_dir}" ]
+then
+ echo "${reports_dir} reports directory is not writable"
+ exit 1
+fi
+report_date=`date +${short_date_format}`;
+report_file=${reports_dir}'/'${php_file_name}'-'${report_date}'-report.log';
+if [ -e "${report_file}" ]
+then
+ # use full date format if there is already a report with the short format
+ report_date=`date +${full_date_format}`;
+ report_file=${reports_dir}'/'${php_file_name}'-'${report_date}'-report.log';
+fi
+echo ${php_file_name}' execution report of '${report_date} > ${report_file};
+# ensure previous command didn't failed
+if [ ! -e "${report_file}" ]
+then
+ echo "cannot log to ${report_file}: log file doesn't exist"
+ exit 1
+fi
+if [ ! -w "${report_file}" ]
+then
+ echo "cannot log to ${report_file}: log file not writeable"
+ exit 1
+fi
+echo "Log file is ${report_file}" >> ${report_file};
+echo 'Starts '${start_date} >> ${report_file};
+
+# Execute php file
+curr_dir=`pwd`
+echo "Current directory is ${curr_dir}" >> ${report_file};
+echo "Going to execute 'php $php_file_args'" >> ${report_file};
+echo 'Output log:' >> ${report_file};
+php $php_file_args >> ${report_file};
+
+# Log end date, finalize report, set accessible in readonly only for current user and send by email
+end_date=`date +'%Y/%m/%d %H:%M:%S'`;
+echo 'Ends '${end_date} >> ${report_file};
+chmod 400 ${report_file}
+mail -s "${php_file_name} execution report of ${report_date}" ${mail} < ${report_file};
- [2] Edit /root/exec-php-send-daily-report.sh
--- old 2012-06-15 19:45:24.000000000 +0200
+++ new 2012-06-15 19:46:06.000000000 +0200
@@ -0,0 +1,119 @@
+#!/bin/bash
+
+# report file name date format
+short_date_format='%Y%m%d';
+
+# Where to store reports
+reports_dir='/var/log/wikizam-cron';
+
+# Who will receive report and where to store localy
+mail='root';
+
+
+# ######
+# Script
+# ######
+
+start_date=`date +'%Y/%m/%d %H:%M:%S'`;
+
+# Read php filename (and optional arguments) argument
+if [ $# -lt 1 ]
+then
+ echo "missing php filename to execute"
+ echo "if the php file require args, use \"\" to specify the php file and its args"
+ echo "usage: $0 \"php_file.php <optional_arg> <optional_arg>\" <optional_directory_to_change_to_before_exec>"
+ exit 1
+fi
+php_file_args=$1
+php_file=`echo $1 | awk '{ print $1 }'`
+php_file_name=`echo $php_file | awk 'BEGIN { FS = "/" } { print $(NF) }'`
+
+# Read optional change directory argument
+if [ $# -gt 1 ]
+then
+ if [ ! -d "$2" ]
+ then
+ echo "cannot change current directory to $2: target is not a directory"
+ exit 1
+ fi
+ if [ ! -r "$2" ]
+ then
+ echo "cannot change current directory to $2: target is not readable"
+ exit 1
+ fi
+ cd $2
+fi
+
+# Check the Php file is available
+if [ ! -e "${php_file}" ]
+then
+ echo "cannot execute ${php_file}: file doesn't exist in ${curr_dir}"
+ exit 1
+else
+ if [ ! -r "${php_file}" ]
+ then
+ echo "cannot execute ${php_file}: file is not readable"
+ exit 1
+ fi
+fi
+
+# Create report
+# -p = no error if existing, make parent directories as needed
+mkdir -p ${reports_dir};
+if [ ! -d "${reports_dir}" ]
+then
+ echo "${reports_dir} reports directory doesn't exist"
+ exit 1
+fi
+if [ ! -w "${reports_dir}" ]
+then
+ echo "${reports_dir} reports directory is not writable"
+ exit 1
+fi
+report_date=`date +${short_date_format}`;
+report_file=${reports_dir}'/'${php_file_name}'-'${report_date}'-report.log';
+if [ ! -e "${report_file}" ]
+then
+ # send previous report
+ previous_report_date=`date --date="yesterday" +${short_date_format}`;
+ previous_report_file=${reports_dir}'/'${php_file_name}'-'${previous_report_date}'-report.log';
+ if [ ! -e "${previous_report_file}" ]
+ then
+ echo 'no report found' | mail -s "${php_file_name} execution report of ${previous_report_date}" ${mail};
+ else
+ chmod 400 ${previous_report_file}
+ mail -s "${php_file_name} execution report of ${previous_report_date}" ${mail} < ${previous_report_file};
+ fi
+
+ # create new report
+ echo ${php_file_name}' execution report of '${report_date} > ${report_file};
+ echo "Log file is ${report_file}" >> ${report_file};
+else
+ chmod 700 ${report_file}
+fi
+# ensure previous commands didn't failed
+if [ ! -e "${report_file}" ]
+then
+ echo "cannot log to ${report_file}: log file doesn't exist"
+ exit 1
+fi
+if [ ! -w "${report_file}" ]
+then
+ echo "cannot log to ${report_file}: log file not writeable"
+ exit 1
+fi
+
+echo " " >> ${report_file};
+echo 'Starts '${start_date} >> ${report_file};
+
+# Execute php file
+curr_dir=`pwd`
+echo "Current directory is ${curr_dir}" >> ${report_file};
+echo "Going to execute 'php $php_file_args'" >> ${report_file};
+echo 'Output log:' >> ${report_file};
+php $php_file_args >> ${report_file};
+
+# Log end date, finalize report, set accessible in readonly only for current user and send by email
+end_date=`date +'%Y/%m/%d %H:%M:%S'`;
+echo 'Ends '${end_date} >> ${report_file};
+chmod 400 ${report_file}
--- old 2012-06-15 19:47:53.000000000 +0200
+++ new 2012-06-15 19:54:56.000000000 +0200
@@ -16,10 +16,10 @@
-# each days at 9 am (fr timezone)
-0 7 * * * root ( test -x /var/wikizam/deployed/extensions/Wikiplaces/updateSubscriptions.php && cd /var/wikizam/deployed/ && ( php /var/wikizam/deployed/extensions/Wikiplaces/updateSubscriptions.php >> /var/log/wikizam-cron ) && ( echo "updateSubscriptions.php has run" | mail -s "Wikizam cron" root ) ) || ( echo "updateSubscriptions.php cannot be located" | mail -s "Wikizam cron" root )
+# each day at 9 am (debian is fr timezone)
+0 9 * * * root /root/exec-php-send-report.sh "extensions/Wikiplaces/updateSubscriptions.php" /var/wikizam/deployed
-# each days at 3 pm (fr timezone)
-0 11 * * * root ( test -x /var/wikizam/deployed/extensions/Wikiplaces/checkNextRenewals.php && cd /var/wikizam/deployed/ && ( php /var/wikizam/deployed/extensions/Wikiplaces/checkNextRenewals.php --deadline=7 >> /var/log/wikizam-cron ) && ( echo "checkNextRenewals.php has run" | mail -s "Wikizam cron" root ) ) || ( echo "checkNextRenewals.php cannot be located" | mail -s "Wikizam cron" root )
+# each day at 3 pm (debian is fr timezone)
+0 15 * * * root /root/exec-php-send-report.sh "extensions/Wikiplaces/checkNextRenewals.php --deadline=7" /var/wikizam/deployed
# each 5 minutes
-*/5 * * * * root ( test -x /var/wikizam/deployed/extensions/Wikiplaces/updateUsages.php && cd /var/wikizam/deployed/ && ( php /var/wikizam/deployed/extensions/Wikiplaces/updateUsages.php --lifespan=60 >> /var/log/wikizam-cron ) ) || ( echo "updateUsages.php cannot be located" | mail -s "Wikizam cron" root )
+*/5 * * * * root /root/exec-php-send-daily-report.sh "extensions/Wikiplaces/updateUsages.php --lifespan=60" /var/wikizam/deployed
-# each day at 7 am (fr timezone)
-0 5 * * * root /root/mysql-daily-backup.sh
+# each day at 7 am (debian is fr timezone)
+0 7 * * * root /root/mysql-daily-backup.sh