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=""monitor.sh is as follows:
#!/bin/bashI'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.
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:
* * * * * /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.
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:
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.
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!
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 10:10 AM.|