LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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
  Search this Thread
Old 05-17-2012, 12:43 PM   #1
Annielover
Member
 
Registered: May 2011
Location: Belgium
Distribution: Ubuntu Server Edition, Fedora 16
Posts: 90

Rep: Reputation: 8
Crontab doesn't execute self-written script


Hi everyone,

I've written a script that generates a system report.
I want to run the script (for testing) every minute, however, crontab doesn't execute it.
But the "ntpdate" command and the "service restart" command ARE working fine...

I've set the PATH variable and I'm using the full path name of the script.
When I execute the same command in the shell it works just fine...
Any advice?

Code:
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin:/home/laurent/Scripts
# m h  dom mon dow   command
0 */2 * * * sudo service vsftpd restart >> /home/laurent/logs/FTP_Restart.log 2>> /home/laurent/logs/FTP_$
0 */1 * * * sudo ntpdate time.nist.gov >> /home/laurent/logs/NTPDateLog.log 2>> /home/laurent/logs/NTPDat$
*/1 * * * * /home/laurent/Scripts/systemcaptureCSV 2>> /home/laurent/logs/stats_CSV.err
*/1 * * * * /home/laurent/Scripts/systemreportHTML 2>> /home/laurent/logs/stats_HTML.err
And the *.err files are empty as well...
 
Old 05-17-2012, 01:09 PM   #2
xonogenic
Member
 
Registered: Feb 2006
Posts: 30

Rep: Reputation: 2
Make sure the script is executable. ( chmod a+x <script name> )
 
Old 05-17-2012, 01:18 PM   #3
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
what user are you running cron as, and what are the permissions of the script?
 
Old 05-17-2012, 02:54 PM   #4
toothandnail
Member
 
Registered: Apr 2007
Location: Oxfordshire, UK
Distribution: Arch, Sparky, Salix64
Posts: 119

Rep: Reputation: 25
About a year ago, I spent a good deal of time trying to work out why one cron event was not executed. In the end, it turned out to be the fact that the crontab did not have a blank line at the end of the file (and the event that was no executing was the last event in crontab)! I'm not sure if that problem exists in all cron implementations, but it was a very annoying error. It was only when I looked at a crontab from another machine running the same distro that I spotted the difference....

Paul.
 
Old 05-17-2012, 05:48 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
There was a problem in some of the older version of crontab on Solaris, where a blank last line would cause issues ..
On Linux and *nix systems generally I've found that removing any blank lines at the bottom usually works ..
I guess you just have to try these things; they come with experience ie the books don't usually mention them...
 
Old 05-18-2012, 02:42 AM   #6
Annielover
Member
 
Registered: May 2011
Location: Belgium
Distribution: Ubuntu Server Edition, Fedora 16
Posts: 90

Original Poster
Rep: Reputation: 8
Quote:
Originally Posted by xonogenic View Post
Make sure the script is executable. ( chmod a+x <script name> )
The script is executable.
 
Old 05-18-2012, 02:47 AM   #7
Annielover
Member
 
Registered: May 2011
Location: Belgium
Distribution: Ubuntu Server Edition, Fedora 16
Posts: 90

Original Poster
Rep: Reputation: 8
Quote:
Originally Posted by lleb View Post
what user are you running cron as, and what are the permissions of the script?
I'm running cron as root (sudo crontab -e).
When I run the script at the shell without using sudo, it does work fine... (using sudo of course as well )

Code:
-rwxr-xr-x  1 root    root     880 2012-05-17 18:18 systemcaptureCSV*
-rwxr-xr-x  1 root    root     950 2012-05-17 18:38 systemreportHTML*
 
Old 05-18-2012, 02:53 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,692

Rep: Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275
I would try to catch the output. (I mean next to 2>> you may need >>/home/laurent/logs/stats_HTML.stdout).
Also try to set -xv at the beginning of your script and echo some text to see it it was started.
 
Old 05-18-2012, 03:22 AM   #9
Annielover
Member
 
Registered: May 2011
Location: Belgium
Distribution: Ubuntu Server Edition, Fedora 16
Posts: 90

Original Poster
Rep: Reputation: 8
Quote:
Originally Posted by chrism01 View Post
There was a problem in some of the older version of crontab on Solaris, where a blank last line would cause issues ..
On Linux and *nix systems generally I've found that removing any blank lines at the bottom usually works ..
I guess you just have to try these things; they come with experience ie the books don't usually mention them...
Hmmm, that did not work for me... very strange though: the scripts are executable, I've created them as root, I'm running them as root in cron, but it doesn't work!
 
Old 05-18-2012, 03:34 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,692

Rep: Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275
this usually caused by the special environment set by cron. That's why catching the output of those tasks may give you helpful information.
Also you can try to put an echo "hello world" > /tmp/cronjoblogfile into your scripts to see if they started. You can check the mailbox of the user, sometimes cron sends mails.
 
Old 05-18-2012, 03:52 AM   #11
Annielover
Member
 
Registered: May 2011
Location: Belgium
Distribution: Ubuntu Server Edition, Fedora 16
Posts: 90

Original Poster
Rep: Reputation: 8
Quote:
Originally Posted by pan64 View Post
I would try to catch the output. (I mean next to 2>> you may need >>/home/laurent/logs/stats_HTML.stdout).
Also try to set -xv at the beginning of your script and echo some text to see it it was started.
OK, let's see:

This is how my cron looks now:
Code:
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin:/home/laurent/Scripts
# m h  dom mon dow   command
0 */2 * * * sudo service vsftpd restart >> /home/laurent/logs/FTP_Restart.log 2>> /home/laurent/logs/FTP_Restart.err&
0 */1 * * * sudo ntpdate time.nist.gov >> /home/laurent/logs/NTPDateLog.log 2>> /home/laurent/logs/NTPDateLog.err&
*/1 * * * * /home/laurent/Scripts/systemcaptureCSV >> /home/laurent/stats_CSV.stdout 2>> /home/laurent/logs/stats_CSV.err
*/1 * * * * /home/laurent/Scripts/systemreportHTML >> /home/laurent/stats_HTML.stdout 2>> /home/laurent/logs/stats_HTML.err
My home dir:
Code:
-rwxrwxrwx  1 laurent laurent    180 2012-05-17 19:30 stats.csv*
-rwxrwxrwx  1 root    root         0 2012-05-18 10:30 stats_CSV.stdout*
-rwxrwxrwx  1 laurent laurent    691 2012-05-17 19:31 stats.html*
-rwxrwxrwx  1 root    root         0 2012-05-18 10:30 stats_HTML.stdout*
My scripts:
Code:
#!/bin/bash
#
# This script generates a HTML file form the CSV file
#
##############################################################
# VARIABLES                                                  #
##############################################################
set -xv echo "If you see this, script $0 is executed!"
REPORT_FILE="$HOME/stats.csv"
etc.........
and
Code:
#!/bin/bash
#
# This script captures the system statistiscs and puts them in a CSV file
# We'll use this script later with the report script
#
##############################################################
# VARIABLES                                                  #
##############################################################
set -xv echo "If you see this, script $0 is executed!"
REPORT_FILE="$HOME/stats.csv"
etc..........
But both the stdout files are still empty...

BUT BUT!!

The error files are NOT empty! Let's have a look at them:
Code:
REPORT_FILE="$HOME/stats.csv"
+ REPORT_FILE=/root/stats.csv
DATE1=`date +"%d/%m/%Y"`
date +"%d/%m/%Y"
++ date +%d/%m/%Y
+ DATE1=18/05/2012
TIME1=`date +"%H:%M:%S"`
date +"%H:%M:%S"
++ date +%H:%M:%S
+ TIME1=10:44:01
#
##############################################################
# MAIN                                                       #
##############################################################
USERS=`uptime | sed 's/user.*$//' | gawk '{print $NF}'`
uptime | sed 's/user.*$//' | gawk '{print $NF}'
++ sed 's/user.*$//'
++ uptime
++ gawk '{print $NF}'
+ USERS=1
LOAD=`uptime | gawk '{print $NF}'`
uptime | gawk '{print $NF}'
++ gawk '{print $NF}'
++ uptime
+ LOAD=0.29
FREEMEM=`free -m | sed -n '2p' | gawk '(x=$4+$7) {print x}' | sed 's/$/MB/'`
free -m | sed -n '2p' | gawk '(x=$4+$7) {print x}' | sed 's/$/MB/'
++ free -m
++ sed 's/$/MB/'
++ gawk '(x=$4+$7) {print x}'
++ sed -n 2p
+ FREEMEM=866MB
IDLE=`vmstat 1 2 | sed -n '/[0-9]/p' | sed -n '2p' | gawk '{print $15}'`
vmstat 1 2 | sed -n '/[0-9]/p' | sed -n '2p' | gawk '{print $15}'
++ vmstat 1 2
++ gawk '{print $15}'
++ sed -n 2p
++ sed -n '/[0-9]/p'
+ IDLE=84
#
echo "$DATE1,$TIME1,$USERS,$LOAD,$FREEMEM,$IDLE" >> $REPORT_FILE # create the CSV file
+ echo 18/05/2012,10:44:01,1,0.29,866MB,84
REPORT_FILE="$HOME/stats.csv"
+ REPORT_FILE=/root/stats.csv
DATE1=`date +"%d/%m/%Y"`
date +"%d/%m/%Y"
++ date +%d/%m/%Y
+ DATE1=18/05/2012
TIME1=`date +"%H:%M:%S"`
date +"%H:%M:%S"
++ date +%H:%M:%S
+ TIME1=10:45:01
I think the error is located in bold, don't you think so?

Last edited by Annielover; 05-18-2012 at 03:54 AM.
 
Old 05-18-2012, 04:06 AM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,692

Rep: Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275
yes, probably the report file is taken from root's home instead of yours. So use

REPORT_FILE="~laurent/stats.csv" or absoluthe path. You can look for another occurrences as well.



_________________________________
Happy with solution ... mark as SOLVED
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.
 
Old 05-18-2012, 06:24 AM   #13
Annielover
Member
 
Registered: May 2011
Location: Belgium
Distribution: Ubuntu Server Edition, Fedora 16
Posts: 90

Original Poster
Rep: Reputation: 8
Quote:
Originally Posted by pan64 View Post
yes, probably the report file is taken from root's home instead of yours. So use

REPORT_FILE="~laurent/stats.csv" or absoluthe path. You can look for another occurrences as well.



_________________________________
Happy with solution ... mark as SOLVED
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.
Indeed, that worked for me, thanks!!
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] crontab doesn't execute the jobs at the right time shoutr Linux - General 5 02-16-2012 02:23 AM
I cant get crontab to execute my rsync script.... Theatre Linux - Server 3 09-10-2011 05:19 AM
crontab doesn't execute python script right msegmx Linux - Newbie 13 05-04-2009 09:29 AM
Script doesn't execute in crontab jis0501 Linux - General 2 08-04-2007 07:09 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:07 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration