LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-01-2016, 11:47 AM   #76
linuxnewbie0101
Member
 
Registered: Dec 2015
Posts: 46

Original Poster
Rep: Reputation: Disabled

Gents/Ladies,

a quick question on debugging/error logging.

so for what i have now,

Code:
set -ex

echo -e $timestamp >> $logfile						#timestamp for log file
exec >> $logfile 2>&1 && tail $logfile
it outputs the timestamp and the whole proccess of script cmds to my logfile.

how can i simply output just the timestamp and ONLY errors or not exit 0
?

can i do something like this
Code:
if [ $? -eq 0 ]
then
  echo -e $timestamp >> $logfile
  echo -e "successfully completed for $host $user" >> $logfile 2>&1
else
  echo -e $timestamp >> $logfile
  echo -e "$method failed for $host $user" >> $logfile 2>&1
fi
and should this go before or after my exit 0, or .....?

Thank you

Last edited by linuxnewbie0101; 07-01-2016 at 12:38 PM.
 
Old 07-01-2016, 12:53 PM   #77
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
You could make a log function
Code:
logfile=mylog.log

log () {
  local timestamp=$(date "+%D %H:%M:%S")
  echo "$timestamp $1" >> $logfile
}

log "something is being logged"
log "Error: there was an error"
 
Old 07-01-2016, 01:38 PM   #78
michaelk
Moderator
 
Registered: Aug 2002
Posts: 15,399

Rep: Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654
The command exit terminates your script so anything after is never executed.

1 is standard output (stdout) i.e. the echo command output goes to stdout.
2 is standard error (stderr) i.e. error messages are sent to stdout.

So 2>&1 means redirect both stdout and stderr to your logfile. I used the set -x mode to output the commands to help you debug your code. One way to turn the off the whole process is comment or remove the set command.
 
Old 07-01-2016, 01:52 PM   #79
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
Yes, and redirect stderr to stdout from echo command is not really useful, I mean echo writes all its arguments to stdout by default
 
Old 07-06-2016, 08:55 AM   #80
linuxnewbie0101
Member
 
Registered: Dec 2015
Posts: 46

Original Poster
Rep: Reputation: Disabled
Thanks for all the suggestions.

here is my latest code. now my question is, the script fails if a cronjob exists for a deleted user and exit. how can i check for deleted user and delete the orphan cronjob before it starts the rest of the backups?

Code:
#!/bin/bash -e
# This script by default is designed to backup all users crontab on host
# and must be run as root user.
# Use optional flags to set values other than default.
# default usage: script.sh  
# optional usage: script.sh -m (backup||restore) -p (desire path) -u (user)
# see Help function for more detail on setting optionals.    

cron_source='/var/spool/cron/crontabs'                                  #default crontab location
bkuplocation='/systems/homes/cronjobs_bk'                               #default backup location
host="$(hostname -f)"                                                   #host FQDN
method='backup'                                                         #setting default method to backup
usercheck="$(cut -d: -f1 /etc/passwd)"

#Help function
function show_help {
  echo "script must be run as root. "
  echo "default usage : scriptname.sh -m (backup||restore) -p (location) -u (user)"
  echo "Command line switches are -m , -p & -u "
  echo "-m  --Sets the value for option to backup or restore. Defaults to backup."
  echo "-p  --Sets the path to backup from and restore to. Defaults to $bkuplocation"
  echo "-u  --Sets the user's job for backup. Defaults to all"
  echo "-h  --Displays this help message. No further functions are performed."
  exit 1
}

# Make sure only root can run this script
if [[ "$(id -u)" != "0" ]]; then                                          # must run script as root
  show_help
  echo "user is not root. script must run as root"
  exit 1
else
  umask 0077
fi

#checking for $bkuplocation. 
if [[ ! -d "${bkuplocation}" ]]; then
  mkdir "${bkuplocation}"                                               #create $bkuplocation folder if it does not exist
  echo "${bkuplocation} does not exist. Created ${bkuplocation}"
fi

# Start getopts code
while getopts :m:p:u:h options; do
  case "${options}" in
    m) 
      method="${OPTARG}"
      ;;
    p) 
      bkuplocation="${OPTARG}"
      ;;
    u)
      user="${OPTARG}"
      ;;
    h)
      show_help
      ;;
    \?) 
      echo "Option ${OPTARG} not allowed."/n
      show_help
      ;;
  esac
done

#Error checking for default $bkuplocation. create if not exist.
if [[ ! -d "${bkuplocation}" ]]; then
  echo "ERROR: Specified location ${location} does not exist."
  echo "Please verify location or use default location by removing -p argument."
  exit 1
fi

#Error checking for non-exist user.
if [[ ${method} = 'backup' && ! -z ${user} ]]; then
  if [[ ! -f ${cron_source}/${user} ]]; then
    echo "ERROR: no cronjob found for ${user} on ${host}."
    exit 1
  fi
fi

#Error checking for restore homeless user.
if [[ ${method} = 'restore' ]]; then
  if [[ ! -z ${user} && ! -f "${bkuplocation}/${host}-${user}" ]]; then
    echo "ERROR: no cron backup found for ${user} on ${host}."
    exit 1
  fi
fi

#Start getopts case code
case ${method} in
  backup)
    if [[ -z "${user}" ]]; then                                           #default backup, no user input
      cd "${cron_source}"
      for users in *; do 
        crontab -l -u "${users}" > "${bkuplocation}/${host}-${users}"
      done
    elif [[ ! -z "${user}" ]]; then                                       #backup with user input -u
        crontab -l -u "${user}" > "${bkuplocation}/${host}-${user}"
    else 
      echo "ERROR: Input option(s) not recognized."
      show_help                                                         #show Help function if user input not recognized
    fi
  ;;
  restore)
    if [[ -z "${user}" ]]; then                                           #default restore for all users from default backup location
      for users in "${bkuplocation}/${host}-*"; do                      #select $host files from default $bkuplocation
        username="${users##*-}"                                         #set username from selected files by removing $host-
        crontab -u "${username}" "${users}"                             #crontab restore for all user
      done  
    elif [[ ! -z "${user}" ]]; then                                       #restore with user input -u
        crontab -u "${user}" "${bkuplocation}/${host}-${user}"
    else
      echo "ERROR: Input option(s) not recognized." 
      show_help                                                         #show Help function if option(s) not recognized
    fi
  ;;
esac

#verify completion of script run
if [[ "$?" -eq "0" ]]; then                                                     #use script exit code for verification
  echo "successfully completed for ${host} ${user}"
else
  echo "${method} failed for ${host} ${user}" 
fi

exit 0
 
Old 07-06-2016, 09:02 AM   #81
michaelk
Moderator
 
Registered: Aug 2002
Posts: 15,399

Rep: Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654
Check if the user does not exist in /etc/passwd.
 
Old 07-06-2016, 09:22 AM   #82
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
Slight notices:

[edit] removed wrong notice, you do verify if crontab backup file exists if $user is set

- On both backup) and restore) actions, you test if $user is set and $user is not set (-z empty, null...), so these lines will never be executed (remove them)

Code:
else 
  echo "ERROR: Input option(s) not recognized."
  show_help                                                         #show Help function if user input not recognized
fi

Last edited by keefaz; 07-06-2016 at 09:25 AM.
 
Old 07-06-2016, 09:27 AM   #83
linuxnewbie0101
Member
 
Registered: Dec 2015
Posts: 46

Original Poster
Rep: Reputation: Disabled
will this work


Code:
case ${method} in
  backup)
    if [[ -z "${user}" ]]; then                                           #default backup, no user input
      cd "${cron_source}"
      for users in *; do
        if [[ "{$user}" != $(cut -d: -f1 /etc/passwd | grep ${user}) ]]; then
          echo "${user} is not an active user. Please consider remove the ${user} jobs"
        else
          crontab -l -u "${users}" > "${bkuplocation}/${host}-${users}"
        fi
      done
    elif [[ ! -z "${user}" ]]; then                                       #backup with user input -u
        crontab -l -u "${user}" > "${bkuplocation}/${host}-${user}"
    else 
      echo "ERROR: Input option(s) not recognized."
      show_help                                                         #show Help function if user input not recognized
    fi
  ;;
 
Old 07-06-2016, 09:48 AM   #84
linuxnewbie0101
Member
 
Registered: Dec 2015
Posts: 46

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by keefaz View Post
Slight notices:

[edit] removed wrong notice, you do verify if crontab backup file exists if $user is set

- On both backup) and restore) actions, you test if $user is set and $user is not set (-z empty, null...), so these lines will never be executed (remove them)

Code:
else 
  echo "ERROR: Input option(s) not recognized."
  show_help                                                         #show Help function if user input not recognized
fi

Yes, but when no user is passed, it will backup all users cronjob, which in this case the user is no longer exist and its just an orphan file that does not need to backup.

Thank you

Last edited by linuxnewbie0101; 07-06-2016 at 09:57 AM.
 
Old 07-06-2016, 10:03 AM   #85
michaelk
Moderator
 
Registered: Aug 2002
Posts: 15,399

Rep: Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654
Just checking if $(cut -d: -f1 /etc/passwd | grep ${user}) exists but what you posted should work.
 
1 members found this post helpful.
Old 07-06-2016, 10:57 AM   #86
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
Quote:
Originally Posted by linuxnewbie0101 View Post
Yes, but when no user is passed, it will backup all users cronjob, which in this case the user is no longer exist and its just an orphan file that does not need to backup.

Thank you
I was talking about this block code (reduced)
Code:
if [[ -z "${user}" ]]; then   #default backup, no user input       
    ...
elif [[ ! -z "${user}" ]]; then  #backup with user input -u         
    crontab -l -u "${user}" > "${bkuplocation}/${host}-${user}"  
else     
    echo "ERROR: Input option(s) not recognized."    
    show_help #show Help function if user input not recognized
fi
You test if $user is not set, then elif test if $user is set...
what would be the alternative executed in else block?
 
1 members found this post helpful.
Old 07-14-2016, 01:27 PM   #87
linuxnewbie0101
Member
 
Registered: Dec 2015
Posts: 46

Original Poster
Rep: Reputation: Disabled
I just want to come and say TY for ALL who came to my rescue on this. TY
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to verify Cronjobs smilemukul Linux - Newbie 1 03-10-2011 02:57 PM
[SOLVED] cronjobs in ubuntu dinakumar12 Linux - Server 7 04-02-2010 05:31 AM
CronJobs error archey Linux - Newbie 4 08-22-2009 05:16 PM
Using cronjobs to mysql dump and scp backup to another box daiver Linux - General 4 11-10-2006 07:37 AM
Problem with cronjobs..... freakin'me Linux - General 7 02-11-2006 03:08 PM


All times are GMT -5. The time now is 12:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration