LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Crontab doesn't execute self-written script (https://www.linuxquestions.org/questions/linux-newbie-8/crontab-doesnt-execute-self-written-script-945567/)

Annielover 05-17-2012 12:43 PM

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

xonogenic 05-17-2012 01:09 PM

Make sure the script is executable. ( chmod a+x <script name> )

lleb 05-17-2012 01:18 PM

what user are you running cron as, and what are the permissions of the script?

toothandnail 05-17-2012 02:54 PM

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.

chrism01 05-17-2012 05:48 PM

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

Annielover 05-18-2012 02:42 AM

Quote:

Originally Posted by xonogenic (Post 4681104)
Make sure the script is executable. ( chmod a+x <script name> )

The script is executable.

Annielover 05-18-2012 02:47 AM

Quote:

Originally Posted by lleb (Post 4681108)
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*


pan64 05-18-2012 02:53 AM

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.

Annielover 05-18-2012 03:22 AM

Quote:

Originally Posted by chrism01 (Post 4681269)
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!

pan64 05-18-2012 03:34 AM

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.

Annielover 05-18-2012 03:52 AM

Quote:

Originally Posted by pan64 (Post 4681531)
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?

pan64 05-18-2012 04:06 AM

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.

Annielover 05-18-2012 06:24 AM

Quote:

Originally Posted by pan64 (Post 4681577)
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!!


All times are GMT -5. The time now is 03:36 PM.