Sysadmin papilusion wikizam cron mail report - Seizam/seizamcore GitHub Wiki

Table of Contents

Session

  • user : root
  • host : papilusion.reverse.seizam.com
  • date : 06/15/12 18:47
  • curpath : /root

Commands

  • Edit /root/exec-php-send-report.sh, see [1]
 [email protected]# chmod 500 exec-php-send-report.sh
  • Edit /etc/crontab, see [2]
 [email protected]# cd /mnt/backups/
 [email protected]# ls
 [email protected]# cd cron-reports/
 [email protected]# ls
 [email protected]# cat updateUsages.php-20120615-report.log
  • Edit /mnt/backups/cron-reports/exec-php-send-daily-report.sh, see [3]
 [email protected]# mv exec-php-send-daily-report.sh /root/
 [email protected]# cd /root/
 [email protected]# chmod 500 exec-php-send-daily-report.sh
  • Edit /etc/crontab, see [4]
 [email protected]# cd /mnt/backups/cron-reports/
 [email protected]# ls -lsa
 [email protected]# rm updateUsages.php-20120615-185601-report.log
 [email protected]# mv updateUsages.php-20120615-report.log updateUsages.php-20120614-report.log
  • The exec-php-send-daily-report.sh sends an email about 20120614 report, so it's working properly
 [email protected]# rm updateUsages.php-20120614-report.log

Files

[1]\* Edit /root/exec-php-send-report.sh

--- old	2012-06-15 18:47:43.000000000 +0200
+++ new	2012-06-15 18:49: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='/mnt/backups/cron-reports';
+
+# 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 /etc/crontab

--- old	2012-06-15 18:50:04.000000000 +0200
+++ new	2012-06-15 18:54:01.000000000 +0200
@@ -20,8 +20,8 @@
 # each 2 hours at x.00
-0 */2	* * *	root	( test -x /var/seizam/w/extensions/Wikiplaces/updateSubscriptions.php && cd /var/seizam/w/ && ( php /var/seizam/w/extensions/Wikiplaces/updateSubscriptions.php >> /var/log/wikizam-cron ) ) || ( echo "updateSubscriptions.php cannot be located" | mail -s "Wikizam cron" root )
+0 */2	* * *	root	/root/exec-php-send-report.sh extensions/Wikiplaces/updateSubscriptions.php /var/seizam/w
 
 # each hours at x.00
-0 *	* * *	root	( test -x /var/seizam/w/extensions/Wikiplaces/checkNextRenewals.php && cd /var/seizam/w/ && ( php /var/seizam/w/extensions/Wikiplaces/checkNextRenewals.php --deadline=7 >> /var/log/wikizam-cron ) ) || ( echo "checkNextRenewals.php cannot be located" | mail -s "Wikizam cron" root )
+0 *	* * *	root	/root/exec-php-send-report.sh "extensions/Wikiplaces/checkNextRenewals.php --deadline=7" /var/seizam/w
 
-# each 20 minutes
-*/20 *	* * *	root	( test -x /var/seizam/w/extensions/Wikiplaces/updateUsages.php && cd /var/seizam/w/ && ( php /var/seizam/w/extensions/Wikiplaces/updateUsages.php >> /var/log/wikizam-cron ) ) || ( echo "updateUsages.php cannot be located" | mail -s "Wikizam cron" root )
+# each 2 minutes
+*/2 *	* * *	root	/root/exec-php-send-report.sh "extensions/Wikiplaces/updateUsages.php --lifespan=30" /var/seizam/w

[3]\* Edit /mnt/backups/cron-reports/exec-php-send-daily-report.sh

--- old	2012-06-15 18:55:17.000000000 +0200
+++ new	2012-06-15 18:55:34.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='/mnt/backups/cron-reports';
+
+# 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}

[4]\* Edit /etc/crontab

--- old	2012-06-15 18:56:13.000000000 +0200
+++ new	2012-06-15 18:56:25.000000000 +0200
@@ -26,2 +26,2 @@
 # each 2 minutes
-*/2 *	* * *	root	/root/exec-php-send-report.sh "extensions/Wikiplaces/updateUsages.php --lifespan=30" /var/seizam/w
+*/2 *	* * *	root	/root/exec-php-send-daily-report.sh "extensions/Wikiplaces/updateUsages.php --lifespan=30" /var/seizam/w

Update: send only 1 report per day

Session

  • user : root
  • host : papilusion.reverse.seizam.com
  • date : 06/15/12 20:20
  • curpath : /home/yannouk

Commands

  • Edit /etc/crontab, see [1]
  • Edit /root/exec-php-send-daily-report.sh, see [2]
  • Edit /root/exec-php-send-report.sh, see [3]
 [email protected]# rm -R /mnt/backups/cron-reports
 [email protected]# mv /var/log/wikizam-cron /var/log/wikizam-cron.old
  • wait that folder wikizam-cron is created by the script
 [email protected]# mv /var/log/wikizam-cron.old /var/log/wikizam-cron/
 [email protected]# ls -lsah /var/log/
 [email protected]# ls -lsah /var/log/wikizam-cron/

Files

[1]\* Edit /etc/crontab

--- old	2012-06-15 20:21:05.000000000 +0200
+++ new	2012-06-15 20:21:30.000000000 +0200
@@ -20,6 +20,6 @@
 # each 2 hours at x.00
-0 */2	* * *	root	/root/exec-php-send-report.sh extensions/Wikiplaces/updateSubscriptions.php /var/seizam/w
+0 */2	* * *	root	/root/exec-php-send-daily-report.sh extensions/Wikiplaces/updateSubscriptions.php /var/seizam/w
 
 # each hours at x.00
-0 *	* * *	root	/root/exec-php-send-report.sh "extensions/Wikiplaces/checkNextRenewals.php --deadline=7" /var/seizam/w
+0 *	* * *	root	/root/exec-php-send-daily-report.sh "extensions/Wikiplaces/checkNextRenewals.php --deadline=7" /var/seizam/w
 

[2]\* Edit /root/exec-php-send-daily-report.sh

--- old	2012-06-15 20:22:53.000000000 +0200
+++ new	2012-06-15 20:23:10.000000000 +0200
@@ -6,3 +6,3 @@
 # Where to store reports
-reports_dir='/mnt/backups/cron-reports';
+reports_dir='/var/log/wikizam-cron';
 

[3]\* Edit /root/exec-php-send-report.sh

--- old	2012-06-15 20:23:14.000000000 +0200
+++ new	2012-06-15 20:23:33.000000000 +0200
@@ -7,3 +7,3 @@
 # Where to store reports
-reports_dir='/mnt/backups/cron-reports';
+reports_dir='/var/log/wikizam-cron';
 
⚠️ **GitHub.com Fallback** ⚠️