#! /usr/bin/perl
use strict;
use warnings;
use utf8;
use File::Basename qw/basename dirname/;
my $DIRNAME = dirname $0;
my $BASENAME = basename $0;
my $CONF_FILE= "$DIRNAME/cgi.json";
if ( ! -e $CONF_FILE ) {
print "Content-type: text/html\n\n ${CONF_FILE} could not be read.\n$!$@";
exit;
}
my $CONFIG = do $CONF_FILE
or die "${CONF_FILE} is could not read.\n$!$@";
my $PARMS = $ENV{'QUERY_STRING'};
my $METHOD = $ENV{'REQUEST_METHOD'};
if ( $METHOD eq "POST" || $METHOD eq "PUT" ) {
read(STDIN, $PARMS, $ENV{'CONTENT_LENGTH'});
}
my ($USER, $DATE);
for my $PARM ( split(/&/, $PARMS) ) {
my ($KEY,$VAL) = split(/=/,$PARM);
$VAL =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr(hex($1))/ego;
if ($KEY eq "user") { $USER=$VAL; }
if ($KEY eq "date") { $DATE=$VAL; }
}
print << "END";
Content-type: text/html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>$CONFIG->{title}</title>
</head>
<body>
<div>Input parameters:$PARMS</div>
<div>User:$USER</div>
<div>Date:$DATE</div>
</body>
</html>
END
Mac OS 10.9.4, FreeBSD 9.1-RELEASE-p15, Ubuntu Server 14.04で動作確認しました。
#! /bin/sh
set -e # Terminate at error
set -u # Error if undefined variable
# System Variables
BASE_DIR=`dirname $0`
BASE_DIR=`cd ${BASE_DIR} && pwd` # Change to full path
BASENAME=`basename $0 | sed -e "s/\.[^.]*$//"` #Except extension
HOST=`hostname | cut -d"." -f 1`
USER=`whoami`
TODAY=`date +%Y%m%d`
# echo with time stamp in front.
log() {
NOW=`date +%Y\/%m/%d\ %H:%M:%S`
echo "$*" | sed -e "s|^|[${NOW}] |"
}
log "Start!"
# User Variables
CONFIG="${BASE_DIR}/${BASENAME}_${HOST}_${USER}.config"
if [ $# -eq 1 ]; then
CONFIG="$1"
fi
if [ ! -f "${CONFIG}" ]; then
echo "${CONFIG} not found"
echo ""
exit 1
fi
. "${CONFIG}"
# Log file
LOGDATE="${TODAY}"
if [ -f "${LOG}" ]; then
LOGDATE=`perl -MPOSIX -le 'print strftime "%Y%m%d", localtime((lstat)[9]) for @ARGV' $LOG`
LOGDATE=`echo ${LOGDATE} | cut -d" " -f 1`
fi
if [ "${LOGDATE}" -lt "${TODAY}" ]; then
echo "move log file from ${LOG} to ${LOG}.${LOGDATE}" | tee -a "${LOG}"
mv "${LOG}" "${LOG}.${LOGDATE}"
fi
log "Config file:${CONFIG}" | tee -a "${LOG}"
NOW=`date +%Y\/%m/%d\ %H:%M:%S`
cat "${CONFIG}" | sed -e "s|^|[${NOW}] Config=|" | tee -a "${LOG}"
# Verify host
if [ `echo ${HOSTS} | grep ${HOST} | wc -l ` -eq 0 ]; then
log "Not allowed on this host." | tee -a "${LOG}"
exit 1
fi
# Prevent multiple instance
GREP_PS="$0 $*"
if [ $# -eq 0 ]; then
GREP_PS="$0"
fi
PROCESSES=`ps -o pgid,pid,tty,time,command | grep "${GREP_PS}" | awk '$1==$2' | wc -l`
log "No. of proceeses ${PROCESSES}" | tee -a "${LOG}"
if [ ${PROCESSES} -gt 1 ]; then
log "Already running this script" | tee -a "${LOG}"
ps -o pgid,pid,tty,time,command | grep "${GREP_PS}" | awk '$1==$2' | tee -a "${LOG}"
log "Abort!" | tee -a "${LOG}"
log "" | tee -a "${LOG}"
exit 1
fi
#do something
log "Complete!" | tee -a "${LOG}"
log "" | tee -a "${LOG}"