From 07023c06342061dddd6c9dcd74c10404e7c50615 Mon Sep 17 00:00:00 2001 From: ligc Date: Sat, 8 Jun 2013 08:23:18 +0000 Subject: [PATCH] add a new tool cronEdit, to edit crontab entries in scripts. Based on the script from John Williams git-svn-id: https://svn.code.sf.net/p/xcat/code/xcat-core/branches/2.8@16582 8638fb3e-16cb-4fca-ae20-7b5d299a9bcd --- xCAT-server/share/xcat/tools/cronEdit | 151 ++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100755 xCAT-server/share/xcat/tools/cronEdit diff --git a/xCAT-server/share/xcat/tools/cronEdit b/xCAT-server/share/xcat/tools/cronEdit new file mode 100755 index 000000000..22d8f87e8 --- /dev/null +++ b/xCAT-server/share/xcat/tools/cronEdit @@ -0,0 +1,151 @@ +#!/bin/sh +# +# NAME: cronEdit +# +# SYNTAX: cronEdit <-a|-d > +# +# DESCRIPTION: Edits crontab for current user to add/change/remove entries +# and re-initialise "cron", it works for both AIX and Linux +# +# FLAGS: -a Add / Change cron entries to root's crontab +# -d Removes cron entries from root's crontab +# +# DEPENDENCIES: None +# +# CHANGE HISTORY: +# +# Date Author Vers. Description +# ----------------------------------------------------------------------------- +# 1996-12-30 JDW 1.0 Original +# 2013-06-06 xCAT 1.1 Updated version to ship with xCAT + +usage() +{ + cat << EOF +Usage: $PROGNAME <-a | -d> + +EOF +} + +############################################################################## + +AddCron() +{ + +crontab -l | awk -v "CronFile=$1" \ + 'BEGIN { FoundCronEntries="False" ; + StartString=sprintf ("### START: Added by cronEdit from file: %s", CronFile) ; + EndString=sprintf ("### END: Added by cronEdit from file: %s", CronFile) +} +{ + # Reading existing roots crontab + + if ( match ($0, StartString) > 0) + { + FoundCronEntries="True" + continue + } + + if ( match ($0, EndString) > 0) + { + FoundCronEntries="False" + continue + } + + if ( FoundCronEntries == "False") + { + print $0 + } +} +END { + printf ("%s\n", StartString) + SystemString = sprintf ("cat %s", CronFile) + system (SystemString) + printf ("%s\n", EndString) + +}' > $CRONTEMP + +} + +############################################################################## + +DeleteCron() +{ + +crontab -l | awk -v "CronFile=$1" \ + 'BEGIN { FoundCronEntries="False" ; + StartString=sprintf ("### START: Added by cronEdit from file: %s", CronFile) ; + EndString=sprintf ("### END: Added by cronEdit from file: %s", CronFile) +} +{ + # Reading existing roots crontab + + if ( match ($0, StartString) > 0) + { + FoundCronEntries="True" + continue + } + + if ( match ($0, EndString) > 0) + { + FoundCronEntries="False" + continue + } + + if ( FoundCronEntries == "False") + { + print $0 + } +}' > $CRONTEMP + +} + +############################################################################## + +PROGNAME=`basename $0` +CRONTEMP="/tmp/cron.tmp$$" +# AIX +if [ -e "/var/spool/cron/crontabs/$USER" ] +then + CRONROOT="/var/spool/cron/crontabs/$USER" +else + CRONROOT="/var/spool/cron" +fi + +if [ $# -ne 2 ] +then + usage + exit 1 +fi + +while getopts ':a:d:' opt +do + case "$opt" in + a) AddCron "$OPTARG" + ;; + d) DeleteCron "$OPTARG" + ;; + *) usage + exit 2 + ;; + esac +done + +# Now respawn cron if necessary...... + +crontab -l | diff - $CRONTEMP 2>&1 > /dev/null + +if [ $? -ne 0 ] +then + # Changes are required...... + if [ `uname` = "AIX" ] + then + cp $CRONTEMP $CRONROOT + CRONPID=`ps -ef | grep "/usr/sbin/cron" | grep -v grep | awk '{ print $2 }'` + kill $CRONPID + else # Linux: common user could not read/write /var/spool/cron + crontab $CRONTEMP + fi +fi + +exit 0