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 |
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
05-17-2012, 12:43 PM
|
#1
|
Member
Registered: May 2011
Location: Belgium
Distribution: Ubuntu Server Edition, Fedora 16
Posts: 90
Rep:
|
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...
|
|
|
05-17-2012, 01:09 PM
|
#2
|
Member
Registered: Feb 2006
Posts: 30
Rep:
|
Make sure the script is executable. ( chmod a+x <script name> )
|
|
|
05-17-2012, 01:18 PM
|
#3
|
Senior Member
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,992
|
what user are you running cron as, and what are the permissions of the script?
|
|
|
05-17-2012, 02:54 PM
|
#4
|
Member
Registered: Apr 2007
Location: Oxfordshire, UK
Distribution: Arch, Sparky, Salix64
Posts: 122
Rep:
|
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.
|
|
|
05-17-2012, 05:48 PM
|
#5
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,434
|
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...
|
|
|
05-18-2012, 02:42 AM
|
#6
|
Member
Registered: May 2011
Location: Belgium
Distribution: Ubuntu Server Edition, Fedora 16
Posts: 90
Original Poster
Rep:
|
Quote:
Originally Posted by xonogenic
Make sure the script is executable. ( chmod a+x <script name> )
|
The script is executable.
|
|
|
05-18-2012, 02:47 AM
|
#7
|
Member
Registered: May 2011
Location: Belgium
Distribution: Ubuntu Server Edition, Fedora 16
Posts: 90
Original Poster
Rep:
|
Quote:
Originally Posted by lleb
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*
|
|
|
05-18-2012, 02:53 AM
|
#8
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,220
|
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.
|
|
|
05-18-2012, 03:22 AM
|
#9
|
Member
Registered: May 2011
Location: Belgium
Distribution: Ubuntu Server Edition, Fedora 16
Posts: 90
Original Poster
Rep:
|
Quote:
Originally Posted by chrism01
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!
|
|
|
05-18-2012, 03:34 AM
|
#10
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,220
|
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.
|
|
|
05-18-2012, 03:52 AM
|
#11
|
Member
Registered: May 2011
Location: Belgium
Distribution: Ubuntu Server Edition, Fedora 16
Posts: 90
Original Poster
Rep:
|
Quote:
Originally Posted by pan64
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.
|
|
|
05-18-2012, 04:06 AM
|
#12
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,220
|
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.
|
|
|
05-18-2012, 06:24 AM
|
#13
|
Member
Registered: May 2011
Location: Belgium
Distribution: Ubuntu Server Edition, Fedora 16
Posts: 90
Original Poster
Rep:
|
Quote:
Originally Posted by pan64
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 07:21 PM.
|
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
|
|