this is the full code of my script which I use on linux RHEL5 and it used to have no error. Recently, I copied the same script file for use on another linux RHEL6 server and I get the following error:
[zenoss]$ bash zen-remote-bkp.sh
: command not foundline 14:
: command not foundline 23:
Started at : Wed-11/05/14-#-09:42:13 AM-EST
: command not foundline 26:
find: `/opt/zenoss/backups/\r': No such file or directory
zen-remote-bkp.sh: line 51: syntax error: unexpected end of file
Code:
SERVER_HOSTNAME="server"
ZENOSS_BACKUP_DIR="/opt/zenoss/backups"
BACKUP_COUNT=0
RSYNC_HOST="client"
SSH_RSYNC_USER="zenoss"
SSH_RSYNC_DEST_DIR="/var/backups_dir/zenoss/$SERVER_HOSTNAME"
MESSAGE_FILE="/home/zenoss/message.txt"
# Initialize the message file with the starting date and time
echo "Started at : " `date +%a-%D-#-%X-%Z`
# Check to see if there are any backup archives
BACKUP_COUNT=`find $ZENOSS_BACKUP_DIR -name "zenbackup*" | wc -l`
if [ "$BACKUP_COUNT" -eq 0 ] ; then
echo "No backup archive to synchronize!"
echo "Ended at : " `date +%a-%D-#-%X-%Z`
exit 1
fi
# Transfer most recent backup via rsync over ssh and (later) delete all
# backups older than 3 days
RECENT_BACKUP=`ls -1 $ZENOSS_BACKUP_DIR/zen* | tail -n 1`
echo "Most recent backup : " $RECENT_BACKUP
rsync -az --stats --partial --ignore-existing -e ssh $RECENT_BACKUP $SSH_RSYNC_USER#$RSYNC_HOST:$SSH_RSYNC_DEST_DIR 2>&1
if [ "$?" -ne 0 ] ; then
echo "Backup archive replication failed!"
echo "Ended at : " `date +%a-%D-#-%X-%Z`
exit 1
fi
echo "Backup archive replication successful!"
echo "Ended at : " `date +%a-%D-#-%X-%Z`
exit 0
Does anyone know what is the cause of it? Thank you.
Related
I have the following script file that writes files to s3 from a local file system:
#!/bin/bash
CURR_DIR=`dirname $0`
SCRIPT_NAME="$(basename $0)"
LOG_FILE=$(echo $SCRIPT_NAME | cut -f 1 -d '.')
TODAY=$(date '+%Y-%m-%d')
NOW=$(date -d "$(date +%Y-%m-%d)" +%Y"-"%m"-"%d)
LOG_PATH="$CURR_DIR"/logs/"$LOG_FILE"-$TODAY.log
LOG="[$(date '+%Y-%m-%d %H:%M:%S,%3N')] INFO {$LOG_FILE} -"
ERROR_LOG="[$(date '+%Y-%m-%d %H:%M:%S,%3N')] ERROR {$LOG_FILE} -"
BUCKET="s3.bucket.example"
OUT_FOLDER="path/to/folderA"
S3_PUSH="s3://$BUCKET/$OUT_FOLDER"
exec &>> $LOG_PATH
echo "$LOG Copying files to local out folder..." >> $LOG_PATH
cp /path/to/folderA/*.* /path/to/folderB
echo "$LOG Command returned code:" $?
if [ "$(ls -A path/to/folderA/)" ]; then
FILES="$(ls path/to/folderA/*)"
for file in $FILES ; do
echo "$LOG File $file found for sync" >> $LOG_PATH
echo "$LOG Pushing $file to S3 /Folder..." >> $LOG_PATH
echo -n "$LOG " ; s3cmd put -c /home/config/.s3cfg "$file" "$S3_PUSH"/
echo "$LOG Command returned code:" $?
echo "$LOG Copying $file to local backup..." >> $LOG_PATH
mv "$file" /path/to/folderA/backup/
echo "$LOG Command returned code:" $? >> $LOG_PATH
RCC=$?
if [ $? -eq 0 ]
then
echo "$LOG Command returned code:" $?
else
echo "$ERROR_LOG Command returned code:" $?
fi
done
else
echo "$LOG No files found for sync." >> $LOG_PATH
fi
And the output is coming out in a specific grok pattern needed for me to parse this output as logs into Elastic Search, however the line 27 output is as follows:
[2021-09-02 08:15:25,629] INFO {TestGrokScriptPattern} - upload: '/path/to/folderA/File.txt' -> 's3://s3.bucket.example/Path/To/Bucket/File.txt' [1 of 1]
0 of 0 0% in 0s 0.00 B/s done
that upload and 0 of 0 0%... Line is created by the exec & command executed on line 16.
How can I get that output to not go to the next line without the date, time and script name preceeding it in order to not break the log pattern I am trying to create?
Rather than redirect output on each line, you can wrap the body of the script in a single block and then handle the output of the entire block in one place. You can then process that output with the stream editor sed. For example:
if true; then # Always true. Just simplifies redirection.
echo "Doing something..."
command_with_output
command_with_more_output
echo "Done."
fi | sed "s/^/${LOG}/" > ${LOG_PATH} 2>&1
The sed expression means: Substitute (s) the beginning of each line (^) with the contents of the LOG variable.
Using 2>&1 at the end also eliminates the need for the exec &>> $LOG_PATH command.
I am running following script to copy files from CIFS share mounted on the system to another destination system. The absolute path of CIFS share contains few spaces and so it fails for that path, I tried running it on another path which doesn't contains spaces and it works fine. It seems some issues with the way I have declared absolute path for CIFS share:
#!/bin/bash
set -x
BASEPATH="/mnt/smbdisks/IT_linux/IT Linux Systems Dev & Support/Testing/Operation/Hello"
ADVICES="World Country State"
make_folder()
{
if [ ! -d "${1}" ]
then
echo "Warning: [${1}] Folder does not exist, trying to create..."
mkdir "$1"
if [ $? != 0 ]
then
echo "Unable to create folder "${1}" - exiting"
exit 1
fi
fi
}
sync_to_apj()
{
FROM=$1
TO=$2
TUNNEL='ssh -A -i /home/linux/.ssh/id_rsa_hostname root#hostname01.exampple.com ssh -q'
EXCLUDE='--exclude Completed --exclude Failed'
echo in folder [${BASEPATH}]
echo "Now running copying from ${FROM}/tmp/ to root#hostname01:/common/shared/test/${TO}/"
rsync -av -e "${TUNNEL}" "${BASEPATH}/${FROM}/tmp/" root#hostname01:/common/shared/test/${TO}/ ${EXCLUDE}
if [ $? != 0 ]
then
echo "Issue with rsync of $1 advices - exiting"
exit 3
fi
# Set perms to JBOSS.JBOSS on our newly copied files
echo " .. and adjusting permssions to jboss.jboss on root#hostname01:/common/shared/test"
ssh -A -i ~/.ssh/id_rsa_hostname root#hostname01 "ssh -q hostname01.example.com 'chown -R jboss.jboss /common/shared/test'"
}
# Main
echo --- START `date` - $0
echo BASEPATH = ["${BASEPATH}"]
for each_advice in ${ADVICES}
do
echo " Syncing ${each_advice}"
#DEST_ADVICE=`echo ${each_advice} | sed -e 's:$:_advices:g'`
DEST_ADVICE=`echo ${each_advice}`
make_folder "${BASEPATH}/${each_advice}/tmp"
make_folder "${BASEPATH}/${each_advice}/New"
echo "Moving pdf files from ${each_advice} to ${each_advice}/tmp"
cd "${BASEPATH}"
mv ${each_advice}/*.{PDF,pdf} ${each_advice}/tmp 2>/dev/null
sync_to_apj "${each_advice}" "${DEST_ADVICE}"
echo "Moving pdf files from ${each_advice}/tmp to ${each_advice}/New"
cd "${BASEPATH}"
mv ${each_advice}/tmp/*.{PDF,pdf} ${each_advice}/New 2>/dev/null
done
echo --- DONE `date` - $0
It fails with following error:
+ '[' '!' -d '/mnt/smbdisks/IT_linux/IT Linux Systems Dev & Support/Testing/Operation/Hello/World/tmp' ']'
+ echo 'Warning: [/mnt/smbdisks/IT_linux/IT Linux Systems Dev & Support/Testing/Operation/Hello/World/tmp] Folder does not exist, trying to create...'
+ mkdir '/mnt/smbdisks/IT_linux/IT Linux Systems Dev & Support/Testing/Operation/Hello/World/tmp'
mkdir: cannot create directory `/mnt/smbdisks/IT_linux/IT Linux Systems Dev & Support/Testing/Operation/Hello/World/tmp': No such file or directory
+ '[' 1 '!=' 0 ']'
+ echo 'Unable to create folder /mnt/smbdisks/IT_linux/IT' Linux Systems Dev '&' Support/Testing/Operation/Hello/World/tmp - exiting'
+ exit 1
I need to create an email notification linux script which can be used in the server patching activity. I have created one script for the same but getting some errors. Can anyone please check what's the issue.
Script -
#email constants
export to_addresses=xyz#abc.com
auth_1_app_path=/export/home/apps/test1/dev1;
function sendServerPatchingSuccessMail()
{
printf "HI Team,\r\n\r\nServer patching activity has been done successfully.\r\n\r\nRequest eText DEV team to validate the server patching changes.\r\n\r\nTotal time taken = $(( ($end_date-$start_date) / ( 60) )) minutes.\r\n\r\nRegards,\r\nAdmin\r\n\r\nNote: This is an auto generated mail, please do not reply. " | mail -s "server patching activity is done sucessfully." $to_addresses -c $cc_addresses
}
function sendServerPatchingStartMail()
{
printf "Hi Team,\r\n\r\nServer patching activity has started at $start_date.\r\n\r\nRequest eText DEV team to validate the server patching changes once they receive the server patching success mail.\r\n\r\nRegards,\r\nAdmin\r\n\r\nNote: This is an auto generated mail, please do not reply. " | mail -s "server patching activity has started." $to_addresses -c $cc_addresses
}
#define list of hosts as array
servers_array=( bookvm04 #DEV1
);
function doWork() {
start_date=$(date +%Y%m%d%H%M%S);
sendServerPatchingStartMail
#stop tomcat on all hosts
echo "`date "+%Y-%m-%d %H:%M:%S"` Stop tomcat stop."
#start for each host
for i in "${servers_array[#]}"
do
export ssh_to_remote_host="ssh -l bookweb -i /export/home/apps/bookplus/.ssh/id_dsa "
case $i in
#DEV1 Book Server
b3bookvm05)
app_path=$view_1_app_path;
;;
#DEV1 Book Server
b3bookvm04)
app_path=$auth_1_app_path;
;;
*) echo "`date "+%Y-%m-%d %H:%M:%S"` $i is not a valid option exiting"
exit 0;;
esac #end case
$ssh_to_remote_host $i $app_path/scripts/stopremote
if [ "$?" -ne 0 ]
then
echo "`date "+%Y-%m-%d %H:%M:%S"` node ${i} could not be stopped"
exit 0
fi
end_date=$(date +%Y%m%d%H%M%S);
echo "`date "+%Y-%m-%d %H:%M:%S"` Total time taken = $(( ($end_date- $start_date) / ( 60) )) minutes";
sendServerPatchingSuccessMail
}
Error while executing above script
-bash-4.1$ sh server_patching_alert.sh
: command not foundrt.sh: line 4:
: command not foundrt.sh: line 5:
: command not foundrt.sh: line 9:
: command not foundrt.sh: line 13:
'erver_patching_alert.sh: line 14: syntax error near unexpected token `
'erver_patching_alert.sh: line 14: `function sendServerPatchingSuccessMail()
This script is working fine.
server_array=$(hostname)
function sendServerPatchingSuccessMail()
{
# Set email constants
to_addresses=
cc_addresses=
# Send server patching completion mail
printf "mail body - server patching activity for hostname '$server_array' has been completed successfully" | mail -s "mail subject" -c $cc_addresses $to_addresses
}
sendServerPatchingSuccessMail
I am writing a script to fix a missing 'F' letter in a mail log file. The mail log file is continuously updating. I am getting a file name, after that I am doing 'sudo su' to get superuser access. Inside sudo, I am fixing a that missing 'F'. However, I am unable to use that file name inside sudo block. Please can anyone help me how I can export these shell variables inside sudo? I tried using export but its not working. the code block I have created is as follows-
#Script to solve F issue
#----------------------------------------
#By Kapil Shirsath
#----------------------------------------
cd /var/spool/mail #mail files reside in mail folder
echo "Entered in mail folder"
filename=`ls -lrt 99999*| sort -k 5 -rn | head -1 | tr -s " " "," | cut -d "," -f "8"` # this will list the file with maximum size`
echo "File with maximum size is $filename"
echo "----------------------------------------------------"
echo "Is it the file expected?(y/n)"
read choice
if test $choice == "n"
then
echo "Exiting...."
exit;
fi;
c=1
while [ $c -le 5 ]
do
ls -lrt $filename
echo $filename
sleep 3
c=`expr $c + 1`
done
echo "---------------------------------------------------"
sudo su<<'HERE' #this will give you super user permissions
echo "Got root access"
echo "First line of the file is as below :"
head -1 $filename
echo "---------------------------------------"
firstline=`head -1 $filename`
echo "Repeat : $firstline"
echo $firstline | grep ^"rom" >/dev/null
if test $? -eq 0
then
ex -s $filename <<'EOF'
1s/^/F/
:wq
EOF
echo "F issue fixed!"
HERE
c=1
while [ $c -le 5 ]
do
ls -lrt $filename
sleep 3
c=`expr $c + 1`
done
echo "---------------------------------------------------"
else
echo "Not finding the missing 'F' ! !! Kindly check with your system "
exit;
fi;
i wrote alert log script and some how this is not working and not throwing any error when i execute the script
i am suspecting sed part is not working properly. could you please advice where i am doing wrong?
here is the piece of code
#!/bin/sh
## Heading #########################################################################################
#---------------------------------------------------------------------------------------#
# script usage #
#---------------------------------------------------------------------------------------#
_usage() {
echo "Usage: $0 ORACLE_SID "
} # _usage
ORACLE_SID="$1"
setenv ()
{
eval "$1=$2"
export "$1"
} # setenv
unsetenv ()
{
while [ $# -gt 0 ]
do
unset "$1"
shift
done
} # unsetenv
if [ $# -ne 1 ]; then
_usage
exit 1
fi
Env=/u01/app/oracle/config
HN=`uname -n`
ERROR_FILE=/tmp/${ORACLE_SID}_error.log
HN=`hostname`
DBA_MAIL="oracle.mail#company"
DBA_PAGE=""
#+--------------------------------------------------------------------------------------+
#| get oracle environment variables from our common env dir |
#+--------------------------------------------------------------------------------------+
if [ -r $Env/${ORACLE_SID}.env ]
then
. $Env/${ORACLE_SID}.env
else
ORACLE_SID=""
fi
#+--------------------------------------------------------------------------------------+
#| just checking for Oracle Env variables for connecting database |
#+--------------------------------------------------------------------------------------+
if [ "$ORACLE_SID" = "" ]
then
echo "ORACLE_SID is invalid"
exit 1
fi
if [ "$ORACLE_HOME" = "" ]
then
echo "The environment variable ORACLE_HOME must be set"
exit 1
fi
if [ "$ORACLE_BASE" = "" ]
then
echo "The environment variable ORACLE_BASE must be set"
exit 1
fi
_AlertLogLoc ()
{
ALERTLOG=`$ORACLE_HOME/bin/sqlplus -s "/as sysdba" << EOF
set head off pause off term on feed off timing off
select value from v\\$parameter where name like 'background_dump_dest';
exit;
EOF`
}
_AlertLogLoc
echo $ALERTLOG
export ALERTLOG
if [ -f $ALERTLOG/alert_${ORACLE_SID}.log ]; then
echo "Found Database Alert log"
else
echo "Alert log not found .. exit from script"
fi
if [ -f $ALERTLOG/alert_${ORACLE_SID}.skip ]; then
echo " ORACLE_SID skip error file found"
SKIP_ERR=`cat $ALERTLOG/alert_$ORACLE_SID.skip|xargs|sed -e 's/ /|/g'`
echo $SKIP_ERR
else
echo "No errors will be excluded"
fi
REC_CUR_ALSIZE=/oraworkspace/OSE/logs/alert_${ORACLE_SID}.size # file to record current alert log lines
#---------------------------------------------------------------------------------------------------------#
# let Capture ORA- error from the alert log #
#---------------------------------------------------------------------------------------------------------#
if [ -f $REC_CUR_ALSIZE ]; then
ALSIZE=`cat $REC_CUR_ALSIZE|sed -e 's/^[ \t]*//'`
ALSIZE=`expr $ALSIZE + 1`
else
ALSIZE=0
fi
if [ $ALSIZE -eq 0 ]; then
echo "PROBABLY RUNNUNG THE SCRIPT FIRST TIME"
sed -n $ALSIZE',$p' $ALERTLOG/alert_${ORACLE_SID}.log |egrep -v "$SKIP_ERR"|grep -i 'ORA-' > /tmp/${ORACLE_SID}_error.log
#`wc -l $ALERTLOG/alert_${ORACLE_SID}.log > $REC_CUR_ALSIZE
cat $ALERTLOG/alert_${ORACLE_SID}.log|wc -l > /oraworkspace/OSE/logs/alert_${ORACLE_SID}.size
#ALSIZE=`cat $ALERTLOG/alert_${ORACLE_SID}.log |wc -l`
else
sed -n ${ALSIZE}',$p' $ALERTLOG/alert_${ORACLE_SID}.log |egrep -v "$SKIP_ERR"|grep -i 'ORA-' > /tmp/${ORACLE_SID}_error.log
#wc -l $ALERTLOG/alert_${ORACLE_SID}.log >> $REC_CUR_ALSIZE
cat $ALERTLOG/alert_${ORACLE_SID}.log |wc -l > /oraworkspace/OSE/logs/alert_${ORACLE_SID}.size
fi
#---------------------------------------------------------------------------------------------------------#
# Notify if any errors are found #
#---------------------------------------------------------------------------------------------------------#
ERR_CNT=`cat /tmp/${ORACLE_SID}_error.log |wc -l`
if [ $ERR_CNT -ne 0 ]; then
echo "Errors found in the alert log. send email notification"
mailx -s "${HN}:${ORACLE_SID} ORA error Found in the alert log" ${DBA_MAIL} < $ERROR_FILE
#mailx -s "${HN}:${ORACLE_SID} ORA error Found in the alert log" ${DBA_MAIL} < $ERROR_FILE
else
echo " No errors found in the alert log"
fi
If $ALERTLOG/alert_$ORACLE_SID.skip doesn't exist or is empty (around line 94), the egrep -v "$SKIP_ERR" will exclude all lines from the sed output, so it will not have a chance to see any remaining ORA- errors.
ALSIZE=1
SKIP_ERR=""
sed -n $ALSIZE',$p' $ALERTLOG/alert_${ORACLE_SID}.log |\
egrep -v "$SKIP_ERR"|wc -l
0
SKIP_ERR="dummy"
sed -n $ALSIZE',$p' $ALERTLOG/alert_${ORACLE_SID}.log |\
egrep -v "$SKIP_ERR"|wc -l
15165
So you need to set your SKIP_ERR to something when it's not set or empty (if your skip file is empty). You haven't said if that is the case or shown the script output, but it seems to work apart from that.
Also not that if ALSIZE is zero sed isn't happy, at least in RHEL 5:
ALSIZE=0
SKIP_ERR="dummy"
sed -n $ALSIZE',$p' $ALERTLOG/alert_${ORACLE_SID}.log |\
egrep -v "$SKIP_ERR"|wc -l
sed: -e expression #1, char 4: invalid usage of line address 0
0
And when you test that the file exists at line 91, you show a message stating that and suggesting you'll stop there, but there is no exit after line 94; doesn't seem to be relevant here but seems like an oversight?