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 |
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
12-18-2012, 08:49 AM
|
#1
|
|
LQ Newbie
Registered: May 2010
Posts: 7
Rep:
|
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.
|
|
|
|
12-18-2012, 09:44 AM
|
#2
|
|
Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,644
|
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.
|
|
|
|
12-18-2012, 11:00 AM
|
#3
|
|
LQ Newbie
Registered: May 2010
Posts: 7
Original Poster
Rep:
|
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.
|
|
|
|
12-18-2012, 11:11 AM
|
#4
|
|
Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,644
|
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.
|
|
|
|
12-19-2012, 02:54 AM
|
#5
|
|
LQ Newbie
Registered: May 2010
Posts: 7
Original Poster
Rep:
|
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.
|
|
|
|
12-19-2012, 04:44 AM
|
#6
|
|
LQ Newbie
Registered: May 2010
Posts: 7
Original Poster
Rep:
|
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.
|
|
|
|
12-20-2012, 02:53 AM
|
#7
|
|
LQ Newbie
Registered: May 2010
Posts: 7
Original Poster
Rep:
|
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.
|
|
|
|
01-03-2013, 12:34 PM
|
#8
|
|
Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,644
|
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).
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:55 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
|
|