LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 12-18-2012, 08:49 AM   #1
argie01
LQ Newbie
 
Registered: May 2010
Posts: 7

Rep: Reputation: 0
Question crontab execution + log


Hi,

I'm using Debian 6.0.6.
I edited root crontab a few days ago to add this line:

Code:
00,10,20,30,40,50 * * * * /root/Scripts/con_switch6.sh 2>&1 >> /var/log/con_switch_diario.log
As you can see, it's a simple bash script execution. As the script has some "echo" commands inside it, I want to log these output on a log file.

I'm connecting to this linux server using Putty. My problem is I realized that meanwhile Putty console is opened and connected a line is generated inside the file "con_switch_diario.log" every 10 minutes (i.e., the script is running).
But when I closed that Putty console (i.e., the ssh connection is finished) and then I reopened it, I found that there aren't entries on that log file during that lapse of time.

So, I don't know if the script is not running during the time that the Putty console is closed, or it's a problem with the log file.
I deleted the crontab and re-created a new one, but the problem persist.

Thank you for your help.
 
Old 12-18-2012, 09:44 AM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,007
Blog Entries: 5

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
You need to modify your redirect order to see errors:

Code:
00,10,20,30,40,50 * * * * /root/Scripts/con_switch6.sh >> /var/log/con_switch_diario.log 2>&1
When you do 2>&1 first you're telling the script to send errors to whatever the stdout was BEFORE you defined it as /var/log/con_switch_diario.log. By doing 2>&1 second you're telling it to use the stdout you just defined.

Since we don't know what is in con_switch6.sh we can't really comment on the rest of it.

Note that jobs run in cron have minimal environments as opposed to what they have when you run them from a command line. At command line they inherit variables setup by things like /etc/profile, /etc/bashrc, $HOME/.bashrc, $HOME/.bash_profile etc... but they don't in cron. This means you often have to include the variables that you need in the script itself.

Chief among these is the PATH variable that tells it where to find the commands it is using.
 
Old 12-18-2012, 11:00 AM   #3
argie01
LQ Newbie
 
Registered: May 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Question

Thank you for your answer, but the problem is not the "2>&1" before the ">> /var/log/con_switch_diario.log".
I changed it and I continue with the same problem. And I have other server linux with a similar script programmed by crontab in the same way that is working fine with the console closed.

I'm aware of the problem with the variables, but inside the script I just using tipical linux-bash commands as "if", "traceroute", "ping", "echo", "mail", etc.
The only odd thing is that from inside this script is called an "expect" script. But in this case I'm using full paths.
And besides it currently the script doesn't have to execute it because it has an "if/else" that run just this command:

Code:
traceroute -m 3 www.domain.com | grep $IP2
The output of this command above should be recorded inside the log file, and it isn't (when the ssh console is closed).

Last edited by argie01; 12-18-2012 at 11:01 AM.
 
Old 12-18-2012, 11:11 AM   #4
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,007
Blog Entries: 5

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
I don't see full path on either the traceroute or grep command despite you're having indicated you had all the commands with full paths on them. (You don't actually need full path on the commands themselves so long as your PATH variable includes the directories where the commands live but I'm mentioning it because of what you said.)

Also if you're using things such as if, for, while etc... that are internal commands to the shell you need to make sure your script includes the interpreter line (e.g. #!/bin/bash) so it is definitely using the shell you expect to get those internals.

expect does extra things so may have other things you need to do.
 
Old 12-19-2012, 02:54 AM   #5
argie01
LQ Newbie
 
Registered: May 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Question

Hi,

yes, you are right regarding to the full path.
I thought that I should be write them just in case of commands that are not linux-native.

But now the problem is fixed. The cause was using a domain name for a traceroute inside the script, instead of the IP.

Regards.

Last edited by argie01; 12-19-2012 at 03:35 AM.
 
Old 12-19-2012, 04:44 AM   #6
argie01
LQ Newbie
 
Registered: May 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Sorry, I did more test and the problem continue.

Here is the code of the script... perhaps you can find something wrong there...

Quote:
#!/bin/sh

IPcorreo="83.25.10.84"
IP1="192.168.10.125"
IP2="192.168.10.154"
maxPloss="15"


IPfirewall=`traceroute -m 3 $IPcorreo | grep $IP1`

if [ $? -eq 0 ]
then
echo "$IPfirewall: `date`"

ploss=$(ping -q -w30 $IPcorreo | grep -o "[0-9]*%" | tr -d %) > /dev/null 2>&1

if [ "$ploss" -gt "$maxPloss" ]
then
/usr/bin/expect /root/Scripts/delete.exp
sleep 60
echo "Switch Email: `date`" | mail -s System system@domain.com
fi
else
traceroute -m 3 $IPcorreo | grep $IP2
fi
Thank you.

Last edited by argie01; 12-19-2012 at 04:48 AM.
 
Old 12-20-2012, 02:53 AM   #7
argie01
LQ Newbie
 
Registered: May 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Smile

Hi,

the problem is fixed. The cause was this line

Quote:
ploss=$(ping -q -w30 $IPcorreo | grep -o "[0-9]*%" | tr -d %) > /dev/null 2>&1
once I changed it to

Quote:
ploss=`ping -q -w30 $IPcorreo | grep -o "[0-9]*%" | tr -d %`
everything began to work fine.
 
Old 01-03-2013, 12:34 PM   #8
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,007
Blog Entries: 5

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Sorry have been out for awhile.

Glad to see you resolved it.

However wanted to note a possible reason why using backticks `` rather than the variable/parentheses $() worked:
Your script has interpreter line as "#/bin/sh". On Linux that is typically linked to another shell usually /bin/bash but on more than one system I've found it linked to /bin/dash or something else and dash does NOT run the same as bash.

When writing scripts rather than relying on links it is best to put in the shell you really want to use so instead of having "#/bin/sh" you should use "#/bin/bash" (or "#/bin/ksh" or "#/bin/dash" or whatever the path is to the shell you really want).
 
  


Reply

Tags
console, crontab, putty, ssh


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 capture or log outputs/message during script execution VijayaRaghavanLakshman Linux - Newbie 7 01-25-2012 08:00 AM
crontab log does not exist sridhar_dct3 Programming 6 09-19-2011 03:56 AM
[SOLVED] rsync execution issue with crontab - Have given full path to rsync too!! Prabagaran Linux - Server 6 04-15-2011 01:39 AM
Crontab log Tekken Linux - Server 1 04-27-2010 09:34 AM
crontab script execution - different behaviour user-cronfile <-> system-wide cronfile resigned Linux - Software 4 03-01-2010 08:55 PM


All times are GMT -5. The time now is 05:21 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration