LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Problem using crontab (http://www.linuxquestions.org/questions/linux-newbie-8/problem-using-crontab-452502/)

simenzo 06-07-2006 03:07 PM

Problem using crontab
 
Hi, I'm a pretty experienced programmer, but am definitely a Linux newbie... so I hope I'm categorizing this question correctly.

I've written a shell script... when I run it from the command-line, it runs fine (i.e., launches the program it is supposed to launch). However, it doesn't seem to be launching when the script is run as a cron job.

I set up the cron job by typing "crontab -e" and entering the following:
MAILTO=""
HOME=/home/Ethan/workspace/SelfPortrait
* * * * * /home/Ethan/workspace/SelfPortrait/monitor.sh

monitor.sh is as follows:

#!/bin/bash

echo "testing" >> /home/Ethan/workspace/SelfPortrait/log.txt
cd /home/Ethan/workspace/SelfPortrait
/home/Ethan/workspace/SelfPortrait/SelfPortrait -auto
I've check and both the script & the executable (i.e, "SelfPortrait") have the execute permission enabled. The cron job is executing the script (which I determined from the log being outputted)... and as I mentioned, running the script from the command line is also successful.

Any ideas?

Many thanks,
Ethan

MensaWater 06-07-2006 03:26 PM

There are different kinds of crontabs. (See man crontab, man cron).

"crontab -e" is specifically used to edit what I think of as the "real crontab" because its the one UNIX uses.

Your syntax is wrong for the "real crontab". In that crontab you need to setup the time to run then the command to run. That's what you're doing on the last line of your cron edit but the other two lines have no impact on that last line.

To fix it just add the other two lines to your script:


Code:

#!/bin/bash

MAILTO=""
HOME=/home/Ethan/workspace/SelfPortrait
echo "testing" >> /home/Ethan/workspace/SelfPortrait/log.txt
cd /home/Ethan/workspace/SelfPortrait
/home/Ethan/workspace/SelfPortrait/SelfPortrait -auto

Then just put the following line in your edit:
* * * * * /home/Ethan/workspace/SelfPortrait/monitor.sh

By the way the above syntax is saying run ever minute 24 hours a day so might cause you some issues with performance. You may wish to modify the "* * * * *" to a specific time or at least to a less frequent run. man crontab and man cron will let you know exactly what to set.

You can have multiple lines in the "real crontab" but they all have to have a time specification then a command specification.

Also to log commands done in cron you can simply modify the line to have the log:
* * * * * /home/Ethan/workspace/SelfPortrait/monitor.sh >>/home/Ethan/workspace/SelfPortrait/log.txt 2>&1 (This is all one line)

That tells it to append output to your log.

Finally there may be other environment variables besides HOME that you need to add to the script. From command line the script inherits the user's environment. From cron it has a minimal environment that is NOT inherited from the user that made the cron entry.

marozsas 06-07-2006 03:44 PM

Is this SelfPortrait a graphical program ?
If so, as jlightner pointed, there is an environment variable named DISPLAY which is set in graphical desktop.
In moniotr.sh, try to add:
Code:

export DISPLAY=:0.0
before the graphical programs starts.

If SelfPortrait is a complex application, try to replace it by a single one, like xclock, just for test purposes. So, you can make sure a graphical program can be run by your cron job.

simenzo 06-07-2006 04:14 PM

Thanks for the suggestions... I bet it does have something to do with an environmental variable. I added the the Display line and substituted xclock for my app--with the result that xclock doesn't launch either.

I'll play around with adding environment variables to the script... any further thoughts would be most welcome!

Ethan

simenzo 06-07-2006 04:21 PM

Got it working! It was the environment variables (not sure exactly which ones, since I put a bunch in).

Thanks again! :)


All times are GMT -5. The time now is 08:35 PM.