LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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
 
LinkBack Search this Thread
Old 09-15-2010, 08:11 AM   #1
vijith.pa@gmail.com
LQ Newbie
 
Registered: Feb 2009
Posts: 26

Rep: Reputation: 0
Shell Script not Working through Crontab in Ubuntu 10.04


Hai Guyz,
i created one shell script file and give execute permission and copy yo /usr/bin/chatenablesall(in that shell scrpit iptables commands only)
in root user i can execute that shell scrpit file (#/bin/sh /usr/bin/chatenabledall)
i added crontab job for root user(#crontab -e)
20 18 * * * /bin/sh /usr/bin/chatenabledall
and i restart the service cron but it;s not executing through crontab
log file :Sep 15 18:20:01 KITES-SERVER CRON[25399]: (root) CMD (/bin/sh /usr/bin/chatenabledall)
Sep 15 18:35:58 KITES-SERVER named[14679]: success resolving 'www.lineclear.com/A' (in 'lineclear.com'?) after reducing the advertised EDNS UDP packet size to 512 octets
Sep 15 18:37:29 KITES-SERVER crontab[25832]: (root) BEGIN EDIT (root)
Sep 15 18:39:01 KITES-SERVER CRON[25850]: (root) CMD ( [ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/lib/php5/ -type f -cmin +$(/usr/lib/php5/maxlifetime) -print0 | xargs -n 200 -r -0 rm)
Sep 15 18:39:25 KITES-SERVER crontab[25832]: (root) END EDIT (root)
pls help me how to solve it this issue?
with regards
Vijith P A
 
Old 09-15-2010, 08:21 AM   #2
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
Hi,

What is the content of /usr/bin/chatenabledall file

Code:
cat /usr/bin/chatenabledall
 
Old 09-15-2010, 08:25 AM   #3
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
Hi,

As per cron log file /usr/bin/chatenabledall file is getting executed as expected.

Code:
20 18 * * * /bin/sh /usr/bin/chatenabledall > ~/temp
How does the above entry behave ? And the contents of ~/temp ?
 
Old 09-15-2010, 08:30 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

Certain variables are not present when you execute a file from crontab (these are available when running from a shell).

As already suggested by vinaytp: Post the /usr/bin/chatenabledall script or check yourself if any of those variables are used.

Hope this helps.
 
Old 09-15-2010, 09:45 AM   #5
vijith.pa@gmail.com
LQ Newbie
 
Registered: Feb 2009
Posts: 26

Original Poster
Rep: Reputation: 0
Thanks for ur valuable information
Content of chatenabledall file
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
modprobe ip_conntrack
modprobe ip_conntrack_ftp
echo 1 >/proc/sys/net/ipv4/ip_forward
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A INPUT -i eth0 -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables --table nat --append POSTROUTING --out-interface eth0 -j MASQUERADE
iptables --append FORWARD --in-interface eth1 -j ACCEPT
iptables -A INPUT -i eth1 -j ACCEPT
iptables -A OUTPUT -o eth1 -j ACCEPT
iptables -t nat -A PREROUTING -i eth1 -p tcp --dport 80 -j DNAT --to 192.168.1.100:3128
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3128

and the temp file is empty
 
Old 09-15-2010, 10:00 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

Although this should work: 20 18 * * * /bin/sh /usr/bin/chatenabledall you might want to try a slightly different approach.

Change the crontab line to: 20 18 * * * /usr/bin/chatenabledall (remove the /bin/sh part, possibly add > /root/chatenabledall.log to catch any output generated)

And add this as first line in your chatenabledall script: #!/bin/bash

A few other things that come to mind:
- Is the script executable? (chmod 750 /usr/bin/chatenabledall if it is not),
- Did root receive any mail from cron? (could hold valuable information that might point to the specific problem).

Hope this helps.
 
Old 09-15-2010, 10:37 AM   #7
vijith.pa@gmail.com
LQ Newbie
 
Registered: Feb 2009
Posts: 26

Original Poster
Rep: Reputation: 0
Hi,
It will not generate any output of chatenabledall.log it's Empty and i given the permission of chatenabledall file is 755 ,and root didn't get any mail and also i add #!/bin/bash first line of my shell script.
log file information is Sep 15 21:03:01 KITES-SERVER CRON[29243]: (root) CMD (/usr/bin/chatenabledall > /root/chatenabledall.log)

then also the rules not added?
 
Old 09-15-2010, 10:52 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

Just notice something else: You might need to use the full path to the commands in the chatenabledall file. It could be that no PATH environment is set (or one that does not include a path to iptables/modprobe).

I'm not an Ubuntu user, but I do believe iptables should be /usr/sbin/iptables and modprobe shouls be /sbin/modprobe. If that is correct (you can check with: which iptables and which modprobe) your chatenabledall would look like this:
Code:
#!/bin/bash
/usr/sbin/iptables -F
/usr/sbin/iptables -X
/usr/sbin/iptables -t nat -F
/usr/sbin/iptables -t nat -X
/usr/sbin/iptables -t mangle -F
/usr/sbin/iptables -t mangle -X
/sbin/modprobe ip_conntrack
/sbin/modprobe ip_conntrack_ftp
echo 1 >/proc/sys/net/ipv4/ip_forward
/usr/sbin/iptables -P INPUT DROP
/usr/sbin/iptables -P OUTPUT ACCEPT
/usr/sbin/iptables -A INPUT -i lo -j ACCEPT
/usr/sbin/iptables -A OUTPUT -o lo -j ACCEPT
/usr/sbin/iptables -A INPUT -i eth0 -m state --state ESTABLISHED,RELATED -j ACCEPT
/usr/sbin/iptables --table nat --append POSTROUTING --out-interface eth0 -j MASQUERADE
/usr/sbin/iptables --append FORWARD --in-interface eth1 -j ACCEPT
/usr/sbin/iptables -A INPUT -i eth1 -j ACCEPT
/usr/sbin/iptables -A OUTPUT -o eth1 -j ACCEPT
/usr/sbin/iptables -t nat -A PREROUTING -i eth1 -p tcp --dport 80 -j DNAT --to 192.168.1.100:3128
/usr/sbin/iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3128
Hope this helps.
 
1 members found this post helpful.
Old 09-15-2010, 11:10 PM   #9
vijith.pa@gmail.com
LQ Newbie
 
Registered: Feb 2009
Posts: 26

Original Poster
Rep: Reputation: 0
Now It's Work....

Thanks for valuable Information Mr.druuna & Mr.vinaytp............
 
Old 09-16-2010, 01:00 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
You're welcome
 
Old 11-02-2010, 09:18 AM   #11
edino
LQ Newbie
 
Registered: Nov 2010
Posts: 4

Rep: Reputation: 0
Good Day:
I am having a problem with my crontab. in ubuntu 10.04
10 2 * * * /home/peter/bin/touch_file

It does Not run at 2 10; Yet if i edit my crontab -e at 8:01 am and set my crontab file to run at 8:05 IT WILL RUN PERFECTLY.
5 8 * * * /home/peter/bin/touch_file


the content of touch_files is

---------
touch /home/peter/bin/"test.txt";
---------
Thanks so much In advance for u help.

Last edited by edino; 11-02-2010 at 09:26 AM. Reason: clarify information
 
Old 11-02-2010, 09:41 AM   #12
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

First: Next time start a new thread. This one is kinda related, but the original issue is already solved.

At what time did you edit the crontab file (the one that needs to run at 02:10)? If you did this between 02:09 and 02:10 there is a big change that is was not yet picked up by the cron daemon. As a result the first run will be the next time it is 02:10.

Normally the cron daemon checks every 30 seconds for changes and to see if anything needs to be started.

Hope this helps.
 
Old 11-02-2010, 11:06 AM   #13
edino
LQ Newbie
 
Registered: Nov 2010
Posts: 4

Rep: Reputation: 0
Red face

Sorry for adding it in this thread

At you question
At what time did you edit the crontab file (the one that needs to run at 02:10)?

i edited friday (11.00 am) morning to be run saturday, sunday and monday morning at 2:10am. But the file was not executed by cron.

Thanks in advance for your your kindly help


Pd. something that just happened. i edited this morning (before 8am)to run at eleven every min ( * 11 * * * ...) and IT DID NOT RUN.
I removed it (crontab -r ) and copy back the script (* 11 * * * ... )and start to work :-(.

Last edited by edino; 11-02-2010 at 11:08 AM.
 
Old 11-02-2010, 11:20 AM   #14
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

This I don't understand:
Quote:
I removed it (crontab -r ) and copy back the script (* 11 * * * ... )and start to work :-(.
Try the following steps (I assume you are a normal user):

crontab -r (which removes the entire crontab for that user)
crontab -e (enter the 10 2 * * * /home/peter/bin/touch_file. Do this manually, no copy-paste. Safe and quit when you are done)
crontab -l (this shows which crontab jobs are actually present).

If the entry is still not executed, have a look at the cron log files (and/or your users mail).

If you want to test and do not want to wait until 02:10, make sure your start time is 2 to 3 minutes later then your edit time (crontab -e is done at say 09:11, set the job at 09:13). This will make sure that the cron daemon will pick up the changes.

Hope this helps.
 
Old 11-02-2010, 11:38 AM   #15
edino
LQ Newbie
 
Registered: Nov 2010
Posts: 4

Rep: Reputation: 0
Unhappy

About My Pd.

It means that when i edit the crontab file to run few min later it always work, YET when i edit the file to RUN many hours later (eg.12 hours later)it does NOT run.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script not Running from Crontab venkatramanj Suse/Novell 3 01-13-2010 05:15 AM
Runs Multiple shell script inside a main script using crontab srimal Linux - Newbie 4 10-22-2009 06:19 PM
Shell script not working through crontab paragkalra *BSD 1 04-15-2009 12:36 PM
how to run crontab in shell script panselva Linux - General 3 05-08-2008 08:33 PM
adding a job to crontab via shell script tazio Linux - General 7 10-23-2003 02:44 PM


All times are GMT -5. The time now is 02:31 AM.

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