LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-12-2009, 05:35 AM   #1
dzcharles
LQ Newbie
 
Registered: May 2009
Location: Belgium
Distribution: Debian 5.0
Posts: 14

Rep: Reputation: 0
Strange solution for problem with cron-sheduling


First I want to make a global picture of how my program works

I have different bash-scripts and one script that is called by cron.
The one script that is called by cron, just call all the other scripts so there must be just one entry in cron.

Now when I tested everything I did the following thing, I ran the script with the commando
Code:
/home/karel/scripts/script
and everything worked fine

and then when I wanted to test it with cron, I made the following entry in cron:
Code:
00 8,12,16,22 * * * cd /home/karel/scripts && bash script
the script ran but didn't do his "job" like it should do it.
One of the scripts makes pictures with a webcam. This scripts made a picture-file but is was 0 bytes. Then when I started to troubleshoot everything I wanted to log the output of the commando like this
Code:
vgrabbj -w $WIDTH -H $HEIGHT -q $QUALITY -f /tmp/Trust.jpeg -E $EVALUE -d $i -W $WHITENESS -r $COLOR -x $CONTRAST -b $BRIGHTNESS -u $HUE &> /tmp/webcam.log
Then comes the strange thing, it worked like this...
Also another script that replace text in config file with rpl, didn't worked until I logged the output...

Can someone give a decent explanotion for this?

thx

Karel
 
Old 05-12-2009, 09:56 AM   #2
bhaslinux
Member
 
Registered: Oct 2003
Location: UnitedKingdom
Distribution: Debian Bullseye
Posts: 357

Rep: Reputation: 49
After the cron executes, the output of the script is sent to the person's mail ID.
Did you check the script output to see if there are any issues ?
 
Old 05-12-2009, 11:11 AM   #3
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
One frequent issue with running scripts from cron is down to the fact that cron typically has a different $PATH to the user that you have been testing with.

So, two questions:
  1. Which account did you do your testing as?
  2. Is there any reason that you cannot run the script as that user?

Could you change your cron entry to:
Code:
00 8,12,16,22 * * * /bin/su - myuser -c "/home/karel/scripts/script"
using your user account instead of myuser
 
Old 05-13-2009, 03:37 AM   #4
dzcharles
LQ Newbie
 
Registered: May 2009
Location: Belgium
Distribution: Debian 5.0
Posts: 14

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by bhaslinux View Post
After the cron executes, the output of the script is sent to the person's mail ID.
Did you check the script output to see if there are any issues ?
The output of the scripts is the following:
where I log the output of the replacing text with rpl:
Code:
Replacing "height 1024" with "height 1024" (case sensitive) (partial words matched)
.
A Total of 1 matches replaced in 1 file searched.
But the thing is, then when I log the output it works, I wanted to log the output to see if it works and now it works
but i want to know why

-------------------------------------
Quote:
Originally Posted by Disillusionist View Post
One frequent issue with running scripts from cron is down to the fact that cron typically has a different $PATH to the user that you have been testing with.

So, two questions:
  1. Which account did you do your testing as?
  2. Is there any reason that you cannot run the script as that user?

Could you change your cron entry to:
Code:
00 8,12,16,22 * * * /bin/su - myuser -c "/home/karel/scripts/script"
using your user account instead of myuser
I've tested my scripts as root.
And I wanted to run my script through cron also as root

I've set the environment variables like PATH with export. So the env variables are ok, i've logged it als with "env > logfile" and I've compared with the normal env

I've tested your solution with the /bin/su and the user but then i does nothing...
it's very very strange ...

Last edited by dzcharles; 05-13-2009 at 03:40 AM.
 
Old 05-13-2009, 04:10 AM   #5
bhaslinux
Member
 
Registered: Oct 2003
Location: UnitedKingdom
Distribution: Debian Bullseye
Posts: 357

Rep: Reputation: 49
If I am understanding this right:
you mean to say that the script when the output is re-directed does not work properly whereas
when the output is not-redirected works properly.

Some of the executables use stderr to write the output. Are you sure that
in the normal shell if you do a re-direction the image converter works fine ?
Try to redirect only one stdXXX at a time.
First

script > <logfile> <- this will help just to redir the stdout
script 2> <logfile> <- only stderr
script > <logfile> 2>&1 <- both stdout and stderr

I am sure one of them must be failing and if none of these fail, I do not see any reason
why cron should not work.
 
Old 05-13-2009, 05:01 AM   #6
dzcharles
LQ Newbie
 
Registered: May 2009
Location: Belgium
Distribution: Debian 5.0
Posts: 14

Original Poster
Rep: Reputation: 0
No
When I redirect the ouput with "&> logfile" it works (with cron) and when remove the "&> logfile" it doesn't work properly
there are functions that works, but taking a picture with vgrabbj doens't work.

When I call the script with his absolute path, in de CLI, like this /home/karel/scripts/script then works everything
 
Old 05-13-2009, 05:14 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Have you checked if the standard output and the standard error are sent to the user's mail (as suggested by bhaslinux in post #2)?
 
Old 05-13-2009, 05:32 AM   #8
dzcharles
LQ Newbie
 
Registered: May 2009
Location: Belgium
Distribution: Debian 5.0
Posts: 14

Original Poster
Rep: Reputation: 0
no
how do you do that?
 
Old 05-13-2009, 05:47 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Just use the mail command from a terminal (being logged as the user to whom the crontab belongs).
 
Old 05-13-2009, 10:58 AM   #10
dzcharles
LQ Newbie
 
Registered: May 2009
Location: Belgium
Distribution: Debian 5.0
Posts: 14

Original Poster
Rep: Reputation: 0
when i type in mail it says it isn't installed
it's ubuntu server 8.04 as OSµ

Ouput of command mail:
Quote:
The program 'mail' can be found in the following packages:
* mailx
* mailutils
Try: apt-get install <selected package>
bash: mail: command not found
 
Old 05-13-2009, 11:34 AM   #11
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You can try pine or mutt. Or you can install mailx as suggested:
Code:
apt-get install mailx
Note that you have to test again the crontab, especially in the case that no mail program was previously installed (no mail could be sent).
 
Old 05-13-2009, 12:11 PM   #12
dzcharles
LQ Newbie
 
Registered: May 2009
Location: Belgium
Distribution: Debian 5.0
Posts: 14

Original Poster
Rep: Reputation: 0
ok
i've installed mailx, and i've removed all the output logs
and now it seems also to work
but when i type mailx it's says that there is no mail for root
 
Old 05-13-2009, 12:20 PM   #13
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by dzcharles View Post
but when i type mailx it's says that there is no mail for root
There is no mail if the standard output and the standard error are redirected to a file. Try a simple cron job like this:
Code:
20 19 * * * echo $PATH
set the minutes and hour to run two minutes after the current time, then wait two minutes and check the mail again:
Code:
# mail
Mail version 8.1.2 01/15/2001.  Type ? for help.
"/var/mail/root": 1 message 1 new
>N  1 root@localhost     Wed May 13 19:20   21/721   Cron <root@hostname> echo $PATH
& 1
Message 1:
From root@localhost Wed May 13 19:20:01 2009
Envelope-to: root@localhost
Delivery-date: Wed, 13 May 2009 19:20:01 +0200
From: root@localhost (Cron Daemon)
To: root@localhost
Subject: Cron <root@hostname> echo $PATH
Content-Type: text/plain; charset=ANSI_X3.4-1968
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
Date: Wed, 13 May 2009 19:20:01 +0200

/usr/bin:/bin
& x
#
The part in red is the standard output of the cron job (note that the PATH environment variable in crontab is limited to /usr/bin and /bin).
 
Old 05-13-2009, 12:34 PM   #14
dzcharles
LQ Newbie
 
Registered: May 2009
Location: Belgium
Distribution: Debian 5.0
Posts: 14

Original Poster
Rep: Reputation: 0
this is my entry in /etc/crontab
Quote:
30 19 * * * root echo $PATH
but still i don't get an entry in mail or mailx
Quote:
No mail for root
but when i get back as normal user "webcam" i get al the entry's
even from te first scripts, that is run as root

Quote:
webcam@Webcam:~$ mail
Mail version 8.1.2 01/15/2001. Type ? for help.
"/var/mail/webcam": 3 messages 3 unread
>U 1 root@webcam Wed May 13 19:00 101/3747 Cron <root@Webcam> cd /home/webcam && bash beveiliging/scripts/beveiligi
U 2 root@webcam Wed May 13 19:26 22/900 Cron <root@Webcam> echo $PATH
U 3 root@webcam Wed May 13 19:30 22/900 Cron <root@Webcam> echo $PATH
 
Old 05-13-2009, 12:43 PM   #15
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
From man cron:
Quote:
When executing commands, any output is mailed to the owner of the crontab (or to the user named in the MAILTO environment variable in the crontab, if such exists).
Well. Now that you have found where stdout and stderr are sent, do you notice some error message in case you don't redirect the output to logfile? In post #12 you told that you "removed all the output logs and now it seems also to work". Why? What has changed from the previous attempts?
 
  


Reply



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
cron.d running but not executing tasks - no simple solution. djsheak Linux - Software 1 02-02-2009 04:47 PM
Cron.daily timing info (problem and solution) dannystaple SUSE / openSUSE 5 08-06-2008 07:31 AM
Strange Cron problem (!?) monk5854 Red Hat 1 11-19-2007 03:17 PM
Sheduling a .sh to run every 4 hours rainwoman Linux - Newbie 2 09-28-2005 12:48 PM
about Linux sheduling eshwar_ind Programming 7 02-11-2004 03:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:16 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