Script runs as root but not as crontab? or
Why does my script run as the root user but won't run from cron.daily? or
What user does cron.daily run as?
I recently ran into a problem with a Subversion update script I wished to execute daily. Unfortunately the svn update failed due to a permissions error. This made little sense to me as I believed if I ran the script as root the script should work. It turns out that on some *nix systems (RH Linux and other variants) the root user has a home directory called /root. Within this directory is where the .subversion directory sits which contains your connection information. Thus solution was to set the HOME variable in my script, as crontab doesn't necessarily pick up environment variables that you get when you log in.
Below you will find the simple fix.
#!/bin/sh
HOME=/root
export HOME=${HOME}
cd /usr/local/svn_workspace/trunk
/opt/CollabNet_Subversion/bin/svn update
Release Engineering is generally defined as the compilation, packaging and delivery of software. The release engineer is usually responsible for much more and has many roles in the software development processes. These roles can include; Supporting and improving tools for Continuous Integration, integrating automated testing, source control, and systems administration. I have collected notes over the past 20+ years and hope to post the best of them here.
Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts
Monday, August 27, 2012
Saturday, August 18, 2012
Shell Script Timestamps
Shell Script Trick No. 3 Timestamps
Over the years I've had the need to use time stamps in my scripts to name log files or directories. I created this function to make the task easier.
Usage is as follows:
timestamp "yyyymmdd"
sets the variable $timestamp to 20120818
timestamp "mm/dd/yy"
sets the varible $timestamp to 08/18/2012
##################
# timestamp: This function generates a timestamp
# Calling Profile: timestamp "format"
# Returns: timestamp
##################
timestamp () {
format=$1
yyyy=`date +%Y`
yy=`date +%y`
month=`date +%m`
day=`date +%d`
hour=`date +%H`
min=`date +%M`
sec=`date +%S`
case $format in
"yesterday yyyy.mm.dd" ) timestamp=`date --date yesterday "+%Y.%m.%d"` ;;
"yesterday yyyy-mm-dd" ) timestamp=`date --date yesterday "+%Y-%m-%d"` ;;
"yesterday yyyymmdd" ) timestamp=`date --date yesterday "+%Y%m%d"` ;;
"yyyy" ) timestamp="$yyyy" ;;
"yy" ) timestamp="$yy" ;;
"dd" ) timestamp="$day" ;;
"mm" ) timestamp="$month" ;;
"hh:mm:ss" ) timestamp="$hour:$min:$sec" ;;
"hhmmss" ) timestamp="$hour$min$sec" ;;
"mm/dd/yy" ) timestamp="$month/$day/$yy" ;;
"mm/dd/yyyy" ) timestamp="$month/$day/$yyyy" ;;
"yyyymmdd" ) timestamp="$yyyy$month$day" ;;
"yyyymmddhhmm" ) timestamp="$yyyy$month$day$hour$min" ;;
"yyyymmddhhmmss" ) timestamp="$yyyy$month$day$hour$min$sec" ;;
"yyyy-mm-dd" ) timestamp="$yyyy-$month-$day" ;;
"yyyy.mm.dd" ) timestamp="$yyyy.$month.$day" ;;
"mmddyyyy" ) timestamp="$month$day$yyyy" ;;
"dd/mm/yy" ) timestamp="$day/$month/$yy" ;;
"dd-mm-yy" ) timestamp="$day-$month-$yy" ;;
"dd-mm-yyyy" ) timestamp="$day-$month-$yyyy" ;;
"mm-dd-yy" ) timestamp="$month-$day-$yy" ;;
"mm-dd-yyyy" ) timestamp="$month-$day-$yyyy" ;;
"mm dd, yyyy" ) timestamp="$month $day, $yyyy" ;;
* ) timestamp="$hour$min$sec";;
esac
}
Over the years I've had the need to use time stamps in my scripts to name log files or directories. I created this function to make the task easier.
Usage is as follows:
timestamp "yyyymmdd"
sets the variable $timestamp to 20120818
timestamp "mm/dd/yy"
sets the varible $timestamp to 08/18/2012
##################
# timestamp: This function generates a timestamp
# Calling Profile: timestamp "format"
# Returns: timestamp
##################
timestamp () {
format=$1
yyyy=`date +%Y`
yy=`date +%y`
month=`date +%m`
day=`date +%d`
hour=`date +%H`
min=`date +%M`
sec=`date +%S`
case $format in
"yesterday yyyy.mm.dd" ) timestamp=`date --date yesterday "+%Y.%m.%d"` ;;
"yesterday yyyy-mm-dd" ) timestamp=`date --date yesterday "+%Y-%m-%d"` ;;
"yesterday yyyymmdd" ) timestamp=`date --date yesterday "+%Y%m%d"` ;;
"yyyy" ) timestamp="$yyyy" ;;
"yy" ) timestamp="$yy" ;;
"dd" ) timestamp="$day" ;;
"mm" ) timestamp="$month" ;;
"hh:mm:ss" ) timestamp="$hour:$min:$sec" ;;
"hhmmss" ) timestamp="$hour$min$sec" ;;
"mm/dd/yy" ) timestamp="$month/$day/$yy" ;;
"mm/dd/yyyy" ) timestamp="$month/$day/$yyyy" ;;
"yyyymmdd" ) timestamp="$yyyy$month$day" ;;
"yyyymmddhhmm" ) timestamp="$yyyy$month$day$hour$min" ;;
"yyyymmddhhmmss" ) timestamp="$yyyy$month$day$hour$min$sec" ;;
"yyyy-mm-dd" ) timestamp="$yyyy-$month-$day" ;;
"yyyy.mm.dd" ) timestamp="$yyyy.$month.$day" ;;
"mmddyyyy" ) timestamp="$month$day$yyyy" ;;
"dd/mm/yy" ) timestamp="$day/$month/$yy" ;;
"dd-mm-yy" ) timestamp="$day-$month-$yy" ;;
"dd-mm-yyyy" ) timestamp="$day-$month-$yyyy" ;;
"mm-dd-yy" ) timestamp="$month-$day-$yy" ;;
"mm-dd-yyyy" ) timestamp="$month-$day-$yyyy" ;;
"mm dd, yyyy" ) timestamp="$month $day, $yyyy" ;;
* ) timestamp="$hour$min$sec";;
esac
}
Shell script reading files
Shell Script Trick No. 2
Reading files. Over the years I have used a couple of different methods for reading configuration files.
I present the two I have used the most with the second being my current favorite.
cat config | while read line
do
LINE=`echo $line | cut -d: -f2`
if [ "$LINE" = "#" ];then
echo
else
echo "$LINE:\c"
fi
done
#The read command eats leading spaces
exec < ${datafile}
while read line
do
# Don't process lines with comments just skip them.
if ( echo $line | grep "^#" > /dev/null 2>&1 );then
continue
fi
done
Friday, August 10, 2012
Remove Carriage Return from string variable
Recently I was tasked with writing some scripts to copy log files from a drop site to a working directory then to unzip them. This was not a very difficult task but I ran into a small issue, how to remove a carriage return from a string variable
How to remove a Carriage Return from a string variable?
#setup
logs=`ls`
#use the tr command and the octal code for CR
logs=`echo ${logs} | tr -d '\15'`
Subscribe to:
Posts (Atom)