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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
07-03-2012, 07:24 AM
|
#1
|
Senior Member
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,992
|
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?
|
|
|
07-03-2012, 08:03 AM
|
#2
|
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
|
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.
|
07-03-2012, 08:26 AM
|
#3
|
Senior Member
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,992
Original Poster
|
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.
|
|
|
07-03-2012, 08:42 AM
|
#4
|
Senior Member
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,992
Original Poster
|
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?
|
|
|
07-03-2012, 09:41 AM
|
#5
|
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
|
Again I would NOT use "$HOME" in the crontab entries. I'd make the directory explicit.
|
|
|
07-03-2012, 10:00 AM
|
#6
|
Senior Member
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,992
Original Poster
|
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
|
|
|
07-03-2012, 07:04 PM
|
#7
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,434
|
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.
|
|
|
All times are GMT -5. The time now is 12:20 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|