-   Linux - Software (
-   -   crontab execution + log (

argie01 12-18-2012 09:49 AM

crontab execution + log

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


00,10,20,30,40,50 * * * * /root/Scripts/ 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.

MensaWater 12-18-2012 10:44 AM

You need to modify your redirect order to see errors:


00,10,20,30,40,50 * * * * /root/Scripts/ >> /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 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.

argie01 12-18-2012 12:00 PM

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:


traceroute -m 3 | 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).

MensaWater 12-18-2012 12:11 PM

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.

argie01 12-19-2012 03:54 AM


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.


argie01 12-19-2012 05:44 AM

Sorry, I did more test and the problem continue.

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




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

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

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

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

argie01 12-20-2012 03:53 AM


the problem is fixed. The cause was this line


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


ploss=`ping -q -w30 $IPcorreo | grep -o "[0-9]*%" | tr -d %`
everything began to work fine.

MensaWater 01-03-2013 01:34 PM

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).

All times are GMT -5. The time now is 03:38 AM.