LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-03-2012, 07:24 AM   #1
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,992

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
help with a crontab entry for debugging


Code:
0       1       *       *       *       bash -xvv /usr/bin/usbbak auto >> ${HOME}/logs/USBBAK_test.log
What I am trying to do is watch as the usbbak script runs to find out why when it runs as a cron job it fails to perform correctly, yet when run manually it works just fine.

put that in my crontab last night, but it didnt do anything. no log file and no backup ran last night. what do i need to do to get the debug -xvv to work from cron?
 
Old 07-03-2012, 08:03 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
When a user logs in his environment (variables including $HOME, $PATH, $TERM) are set for him and commands run by that user inherit that environment.

cron on the other hand does not actually "login" so it runs a minimal environment. The most common issue is that the PATH variable of a logged in user often has far more in it than the one cron runs for the same user.

Login as the user and run "env" to see the environment. Run "echo $PATH" to see what the PATH variable contains. Then run a simply cron job to output those two commands to a file and you'll see lots of differences.

Do NOT use $HOME in your cron directory specification - use the actual path to the user's $HOME (e.g. /home/username/log instead of $HOME/log).

cron itself does have a log in /var/log/cron usually. This won't show the output of the actual command but will show you whether cron tried to run the command. My guess is that it did.

To solve issues with a script not running properly in cron you typically just need to add full paths to commands OR explicitly set the PATH variable in the script so it includes the directories that contain the commands you want to use.

That is to say if your script was doing something like:
ps -ef |grep myprocess
You could run "which ps" and "which grep" to find out location of both of those commands. Typically they're both in /bin so you could modify your script to instead do:
/bin/ps -ef |/bin/grep myprocess
--OR--
PATH=$PATH:/bin
ps -ef |grep myprocess

The PATH=$PATH:/bin tells it to append /bin to whatever is already in the PATH variable. The above is a simple example - you can add other paths. For example say you needed things from /usr/local/bin and myuser's $HOME/scripts (where $HOME = /home/myuser) directory you could put in your script:
PATH=$PATH:/usr/local/bin:/home/myuser/scripts

As noted above PATH is the most common issue but there could be others which is why it is important to know what the environment of the logged in user is and compare it to what the script will have when run as cron. One way around this is to simply run the script with su to the user with the "-" flag as that will setup the environment the same as it would be if the user was logged in (mostly - it won't set $TERM because it won't be running on a terminal). This can make your log output ugly but otherwise make your script run.
 
1 members found this post helpful.
Old 07-03-2012, 08:26 AM   #3
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,992

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Code:
PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/rx30/bin:/usr/local/java/bin:.:/usr/rx30
HOME=/usr/rx30
that is the other part of my crontab -l is that what you are referring to for "setting"

the full crontab -l

Code:
crontab -l
PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/rx30/bin:/usr/local/java/bin:.:/usr/rx30
HOME=/usr/rx30
0       6-20    *       *       *       ${HOME}/rsync.sh -n
0       5,21-22 *       *       *       ${HOME}/rsync.sh
0       1       *       *       *       bash -xvv /usr/bin/usbbak auto >> ${HOME}/logs/USBBAK_test.log
not sure if that helps.
 
Old 07-03-2012, 08:42 AM   #4
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,992

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Code:
[Live_Server_103884_rx30@rx30 ~]$ cat TEST.sh
#!/bin/bash

### created to test why usbbak is not creating a full encrypted tarball when run from cron

DOW=`date +%A`
HOMEDIR="$HOME"
LOG=${HOMEDIR}/logs/${DOW}-USBBAK_test.log


BASH()

{

        bash -xvv /usr/bin/usbbak auto >> ${LOG} 2>&1

}

RSYNCPID=`ps -ef | grep "rsync" | grep -v grep | awk '{ print $2 }'`

kill ${RSYNCPID}
sleep 5
kill ${RSYNCPID}
sleep 5
ps -ef | grep ${RSYNCPID} | grep -v grep
ret=$?

if [ "${ret}" != "0" ]
then
        echo "rsync terminated." >> ${LOG}
else
        sleep 5
        kill ${RSYNCPID}
        sleep 5
        ps -ef | grep ${RSYNCPID} | grep -v grep
        ret2=$?
                if [ "${ret2}" != "0" ]
                then
                        echo "Second try to terminate rsync successful." >> ${LOG}
                fi
fi


BASH

exit;
created that to run in place of the cron entry i did before, now my crontab looks like this:

Code:
$ crontab -l
PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/rx30/bin:/usr/local/java/bin:.:/usr/rx30
HOME=/usr/rx30
0       6-20    *       *       *       ${HOME}/rsync.sh -n
0       5,21-22 *       *       *       ${HOME}/rsync.sh
#0      1       *       *       *       bash -xvv /usr/bin/usbbak auto >> ${HOME}/logs/USBBAK_test.log
0       1       *       *       *       ${HOME}/TEST.sh
Does the above script look good?
 
Old 07-03-2012, 09:41 AM   #5
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Again I would NOT use "$HOME" in the crontab entries. I'd make the directory explicit.
 
Old 07-03-2012, 10:00 AM   #6
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,992

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
ok, other then that does the script look good?

ill make the adjustments to remove $HOME and replace with /usr/rx30/

---------- Post added 07-03-12 at 11:01 AM ----------

Code:
]$ crontab -l
PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/rx30/bin:/usr/local/java/bin:.:/usr/rx30
HOME=/usr/rx30
0       6-20    *       *       *       /usr/rx30/rsync.sh -n
0       5,21-22 *       *       *       /usr/rx30/rsync.sh
#0      1       *       *       *       bash -xvv /usr/bin/usbbak auto >> ${HOME}/logs/USBBAK_test.log
0       1       *       *       *       /usr/rx30/TEST.sh
 
Old 07-03-2012, 07:04 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,434

Rep: Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790
In addition to what MensaWater said, check the /var/log/cron and also note that if cron has an issue, it will normally email the job owner or root user. Try mailx as each of those to see.
 
  


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
about crontab entry Ravi kumar Linux - Software 2 09-16-2008 01:41 PM
Somehow not getting crontab entry right Jykke Linux - Desktop 4 02-29-2008 01:50 PM
crontab entry? Master Fox Linux - Software 4 08-23-2005 01:20 PM
crontab job entry notolerance Linux - Software 2 02-20-2005 02:40 PM
crontab entry pilipk01 Linux - General 4 11-19-2003 07:53 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:20 AM.

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
Open Source Consulting | Domain Registration