LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 04-29-2009, 03:15 PM   #1
msegmx
LQ Newbie
 
Registered: Apr 2009
Posts: 9

Rep: Reputation: 0
Question crontab doesn't execute python script right


/etc/crontab :

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#0,5,10,15,20,25,26,30,35,40,45,50,55 * * * * mserkan python /home/mserkan/.myscripts/randomwallpaper.py
* * * * * mserkan python /home/mserkan/.myscripts/randomwallpaper.py >/home/mserkan/.myscripts/myscript.log 2>&1
#


/home/mserkan/.myscripts/randomwallpapers.py :

#!/usr/bin/env python

from os import listdir, system
from random import sample

#get list of files in /mnt/Work/resources/visuals/wallpapers/
fileList = listdir("/mnt/Work/resources/visuals/wallpapers/")
#randomly choose one
theChosenOne = sample(fileList, 1)
#set wallpaper
system('gconftool-2 --type string --set /desktop/gnome/background/picture_filename "/mnt/Work/resources/visuals/wallpapers/' + str(theChosenOne[0]) + '"')
#scale wallpaper
system('gconftool-2 --type string --set /desktop/gnome/background/picture_options "zoom"')

here's my ls -l output :

mserkan@UbuntuPC:~/.myscripts$ ls -l
total 8
-rw-r--r-- 1 mserkan mserkan 0 2009-04-29 18:21 myscript.log
-rwxr-xr-x 1 mserkan mserkan 539 2009-04-29 17:50 randomwallpaper.py
-rw-r--r-- 1 mserkan mserkan 593 2009-04-29 17:49 randomwallpaper.py~
mserkan@UbuntuPC:~/.myscripts$



In a terminal when I run :
mserkan@UbuntuPC:~/.myscripts$ python randomwallpaper.py
it works, the wallpaper changes immediately.
when I delete the myscript.log file, it is created every minute again, hence I guess the python script is being run every minute. the created log file is empty.

this script worked in Ubuntu 8.04 through a crontab entry
but I couldn't get it work through a crontab entry in Ubuntu 9.04.

can anybody help me ?

Last edited by Tinkster; 04-29-2009 at 03:23 PM.
 
Old 04-29-2009, 04:55 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Personal cronjobs should not be run from system-wide /etc/crontab but from what you get when you run 'crontab -e' as unprivileged user "mserkan". Your logfile may be empty because there's no stdout or stderr to log and because you don't append but clobber the log. And with respect to reinventing the wheel, doesn't your Desktop Environment come with wallpaper changing functionality?
 
Old 04-29-2009, 04:56 PM   #3
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
Try executing the exact command line from the prompt:

Code:
python /home/mserkan/.myscripts/randomwallpaper.py >/home/mserkan/.myscripts/myscript.log 2>&1
What is outputting to the log or screen when you manually execute it that way? Secondarily you should have it in your personal crontab (crontab -e) not system wide... my guess is change of python version or loss of a system variable that was set before (/security changes related to x?)

Last edited by rweaver; 04-29-2009 at 04:59 PM.
 
Old 04-29-2009, 05:18 PM   #4
msegmx
LQ Newbie
 
Registered: Apr 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by unSpawn View Post
Personal cronjobs should not be run from system-wide /etc/crontab but from what you get when you run 'crontab -e' as unprivileged user "mserkan".
Consider me as a totally newbie. the blog where I found this script says to use the crontab in /etc.

'crontab -e' -> GNU nano 2.0.9 File : /tmp/crontab.UznAaV/crontab

it produces a tmp file, so I guess you say that a crontab file should be created. but where ? directly in my home folder "/home/mserkan" ?

Quote:
Originally Posted by unSpawn View Post
Your logfile may be empty because there's no stdout or stderr to log and because you don't append but clobber the log.
When an error occurs, it is output in the log file (I made intentionally an error in the script to see if it works).
Quote:
Originally Posted by unSpawn View Post
And with respect to reinventing the wheel, doesn't your Desktop Environment come with wallpaper changing functionality?
Ubuntu 9.04 comes with Gnome 2.26.1, but afaik there's no such tool like in KDE.

Quote:
Originally Posted by rweaver
What is outputting to the log or screen when you manually execute it that way?
absolutely nothing, neither in log nor on screen. but the wallpaper changes.

crontab -e : it produces a tmp file. where do I have to create one ?

I've installed Ubuntu 9.04 yesterday. so I guess it comes with one of the latest versions.

Last edited by msegmx; 04-29-2009 at 05:20 PM.
 
Old 04-29-2009, 05:25 PM   #5
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Edit your crontab by using "crontab -e". Crontab should install the result in the correct place, and you shouldn't need to worry about it.
 
Old 04-29-2009, 05:41 PM   #6
msegmx
LQ Newbie
 
Registered: Apr 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by billymayday View Post
Edit your crontab by using "crontab -e". Crontab should install the result in the correct place, and you shouldn't need to worry about it.
I've removed the lines from /etc/crontab and did as you say.
after (ctrl + x) exiting nano editor it said :

mserkan@UbuntuPC:~$ crontab -e
no crontab for mserkan - using an empty one
crontab: installing new crontab
mserkan@UbuntuPC:~$

I deleted the log file and waited one minute. the log file got created with this error message in it :

/bin/sh: mserkan: not found

when I type crontab -e again nano editor now opens :

# m h dom mon dow command
#0,5,10,15,20,25,26,30,35,40,45,50,55 * * * * mserkan python /home/mserkan/.myscripts/randomwallpaper.py
* * * * * mserkan python /home/mserkan/.myscripts/randomwallpaper.py >/home/mserkan/.myscripts/myscript.log 2>&1
 
Old 04-29-2009, 05:51 PM   #7
msegmx
LQ Newbie
 
Registered: Apr 2009
Posts: 9

Original Poster
Rep: Reputation: 0
I removed mserkan in nano editor, becaus it says there at the top :

# m h dom mon dow command

so no user.

now every minute the log file is created but empty. and the wallpaper doesn't change at all.

I removed the lines in /etc/crontab before. now the crontab of mserkan works.

in terminal, this works and the wallpaper changes.
mserkan@UbuntuPC:~/.myscripts$ python randomwallpaper.py
mserkan@UbuntuPC:~/.myscripts$

but crontab runs the script, gives no errors but doesn'T change the wallpaper.

can you help ?
 
Old 04-29-2009, 06:20 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You don't need 'python' and the path to python in the 1st line of the script. That's redundant.
cron prob can't find the first python because you haven't specified the full path (a typical issue with cron; always specify complete absolute paths to cmds/files).
Anyway, drop the 'python' in the cron cmd and just ensure you have the path in the script (as you have) and also that the script has executable perms on.
 
Old 04-29-2009, 06:33 PM   #9
msegmx
LQ Newbie
 
Registered: Apr 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by chrism01 View Post
You don't need 'python' and the path to python in the 1st line of the script. That's redundant.
cron prob can't find the first python because you haven't specified the full path (a typical issue with cron; always specify complete absolute paths to cmds/files).
Anyway, drop the 'python' in the cron cmd and just ensure you have the path in the script (as you have) and also that the script has executable perms on.
I made it so and the result is the same. then I added print "1" to "4" as debug code between every line in the script. the output in the log file was

h1
h2
h3
h4

but when I open gconf-editor the background value isn't changed. when I change it manually, the wallpaper also changes immediately.

in terminal :
mserkan@UbuntuPC:~/.myscripts$ ls -l
total 12
-rw-r--r-- 1 mserkan mserkan 18 2009-04-30 02:29 myscript.log
-rwxr-xr-x 1 mserkan mserkan 712 2009-04-30 02:26 randomwallpaper.py
-rw-r--r-- 1 mserkan mserkan 712 2009-04-30 02:25 randomwallpaper.py~
mserkan@UbuntuPC:~/.myscripts$



now sorry for asking this (prob.) very simple question, but what is the command for changing it to executable ?

I tried chmod 777. as I said I'm really new to Linux.
 
Old 04-29-2009, 06:51 PM   #10
msegmx
LQ Newbie
 
Registered: Apr 2009
Posts: 9

Original Poster
Rep: Reputation: 0
could it be that python doesn't have the rights to change something in gconf ?
 
Old 04-29-2009, 09:00 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
cron runs detached, ie not attached to any terminal, unlike eg '&' which puts it in the background, but is still attached to the terminal. Its more like 'nohup'.
You should prob just set up the python script as a daemon (ie an infinite lop) and run it in the background.
There may(!) be a way to get cron to talk to an interactive session, but I don't know how.
 
Old 04-30-2009, 06:17 AM   #12
msegmx
LQ Newbie
 
Registered: Apr 2009
Posts: 9

Original Poster
Rep: Reputation: 0
@Chris

this worked in Ubuntu 8.04 flawlessly. I used the /etc/crontab file there (I wasn't even aware of user specific crontab files until this thread).

do you (or anybody else) have any clue why it wouldn't work in 9.04 ?

btw, I got this script from : http://trishankkarthik.blogspot.com/...pers-with.html
 
Old 04-30-2009, 06:35 AM   #13
msegmx
LQ Newbie
 
Registered: Apr 2009
Posts: 9

Original Poster
Rep: Reputation: 0
... and the wallpaper is being changed every time I logout/login/restart.

that would mean that some process is invoking the script, right ?
 
Old 05-04-2009, 09:29 AM   #14
msegmx
LQ Newbie
 
Registered: Apr 2009
Posts: 9

Original Poster
Rep: Reputation: 0
There is a solution for this problem :

http://ubuntuforums.org/showpost.php...8&postcount=12

it worked for me.

I'm copy-pasting the solution, in case the post at 'ubuntuforums.org' got deleted :


Quote:
Apparently this is not a bug in gconftool-2, but with Intrepid it uses different back-end stuff which I won't pretend to understand. However, the following solution works with using gconftool-2 in Intrepid in a script called by cron:

1) Make a file in your home directory that creates a file with environment variables needed by gconftool-2. I called mine .make_Xdbus and here are the contents:

Code:

#!/bin/bash
# Export the dbus session address on startup so it can be used by cron
touch $HOME/.Xdbus
chmod 600 $HOME/.Xdbus
env | grep DBUS_SESSION_BUS_ADDRESS > $HOME/.Xdbus
echo 'export DBUS_SESSION_BUS_ADDRESS' >> $HOME/.Xdbus
# Export XAUTHORITY value on startup so it can be used by cron
env | grep XAUTHORITY >> $HOME/.Xdbus
echo 'export XAUTHORITY' >> $HOME/.Xdbus

2) Make .make_Xdbus run each time you log in by selecting System -> Preferences -> Sessions and add /home/your_name/.make_Xdbus as a Startup program. To test, log out and log back in and check to be sure a file called .Xdbus is there.

3) Modify your cron command so it sources .Xdbus before calling the script which contains the call to gconftool-2. Here's an example from my cronfile:

Code:

00,10,20,30,40,50 * * * * . /home/mike/.Xdbus; /home/mike/chg_background.sh >| OUT_BACKGROUND 2>&1

Don't forget the "." after the last asterisk.
 
  


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
crontab is failed to execute my python script tcyeo Linux - Newbie 4 04-05-2009 11:14 PM
Script doesn't execute in crontab jis0501 Linux - General 2 08-04-2007 07:09 AM
script in crontab do not execute or take proper environment of user mayank_a Linux - Server 1 07-02-2007 05:03 AM
crontab : why root can't execute ?? bluenectar Linux - General 2 03-12-2007 03:21 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:28 PM.

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