3

I coded a python application which was running OK as a cron job. Later I added some libraries (e.g. pynotify and other *) because I wanted to be notified with the message describing what is happening, but it seems that cron can't run such an application.

Do you know some alternative how to run this application every five minutes? I'm using Xubuntu.

  • import gtk, pygtk, os, os.path, pynotify

I can run the application without cron without problems.

Cron seems to run the application but it won't show the notification message. In /var/log/cron.log there are no errors. The application executed every minute without problems.

my crontab: */1 * * * * /home/xralf/pythonsrc/app

thank you

2
  • 1
    Details details. What are the errors you get? May 31, 2011 at 18:20
  • @Nofaul Ibrahim Detail added.
    – xralf
    May 31, 2011 at 18:53

4 Answers 4

1

If your python script runs fine by itself and only fails in cron, then most likely the paths to the libraries are not set in cron. Here's an example from one of my cronjobs where I add the path to cron before executing the file

00 12 * * * LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib && export LD_LIBRARY_PATH && /path/to/my/script

You'll have to replace the paths above with the respective paths to your libraries.

1

If the cron job runs as "you", and if you set the DISPLAY var (export DISPLAY=:0) you should have no issues.

5
  • You gotta be more specific. Do you have any errors? If you don't have any errors, do you have a mail agent delivering cron errors to you? Does it silently fail? Jun 2, 2011 at 17:26
  • Basically it seems that the environment between your running session and cron is somehow different. For example your app may be determining (via the library support) that you can't run X apps or something like that. Drop into shell and do 'echo $DISPLAY' for instance. What does that say? Jun 5, 2011 at 18:57
  • echo $DISPLAY shows :0.0 If I call export DISPLAY=:0 it shows :0
    – xralf
    Jun 5, 2011 at 19:03
  • Then try setting DISPLAY to :0.0 Jun 6, 2011 at 2:24
  • @EightyEight :0.0 did not help.
    – xralf
    Jun 6, 2011 at 7:37
0

I don't see any problem in cron job with pynotify? What is the error you are getting?

Can you run your python code separately to check whether your python code is working really well but only fails with cron?

Celery is distributed job queue & task manager written in Python but it may be too much for your needs.

Supervisord also can do some sort of cron task if you know that your program shall close in 5 minutes. So you can configure supervisord to start the task soon after. None of them are not easier like cron job.

2
  • I added more info to the question.
    – xralf
    May 31, 2011 at 18:46
  • 1
    He's importing GTK, if his cron job tries to run a GTK application, it'll fail because the cron daemon doesn't have access to the X server.
    – nos
    May 31, 2011 at 18:46
0

Your most likely problem is a pathing issue. Cron does not have user paths unless you specifically set it up. So if your libraries are installed in non standard locations or user accounts, cron will need full paths or else you need to set the path variable in your crontab file.

Add the line

PATH=/bin:/usr/bin:/usr/local/bin:/usr/local/sbin:/usr/sbin:/any/other/path

to your crontab file and see if it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.