LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   cronjob + feh (https://www.linuxquestions.org/questions/linux-software-2/cronjob-feh-827706/)

alphisb0t 08-21-2010 05:49 PM

cronjob + feh
 
Hey guys. I wrote a small bash script to use feh to change my background every time its called. I use a cron job to have it execute every 5 minutes.

When I run the script manually it works perfectly, however for some reason when its run via cron it fails. I can see that it is executing but it seems to error for some reason.

Any reason why this would happen? Its kind of a drag to have a working script that when run as a cron job suddenly no longer works.

Thanks.

colucix 08-21-2010 05:56 PM

Cron jobs have their own limited environment and their rules. It's difficult to give a clue without seeing the crontab entry and/or the content of the script. By the way, as a normal behavior, the cron daemon sends standard output and standard error of jobs to the crontab owner's mailbox (if they are not explicitly redirected to a file). Have you checked it?

alphisb0t 08-21-2010 06:02 PM

Aug 21 18:04:01 drone_x61 cron[3818]: (alphis) RELOAD (crontabs/rage)
Aug 21 18:05:01 drone_x61 cron[19512]: (alphis) CMD (rotate_wallpapers.sh)
Aug 21 18:05:22 drone_x61 cron[19511]: (alphis) MAIL (mailed 49 bytes of output but got status 0x0001


this is a piece of the output from /var/log/messages

As for mail, I don't even have a mailbox setup.

Any ideas?

Elv13 08-21-2010 06:04 PM

Add DISPLAY=:0 before feh
Code:

DISPLAY=:0 few --bg-scale /path/to/image.png
It might work. Cron is not aware of X, it run in a deeper layer and does not know about higher layers. The DISPLAY variable -may- point feh to the right place, or not.

alphisb0t 08-21-2010 06:05 PM

Ok I redirected output (why didn't I do this before) to a file.

feh ERROR: Can't open X display. It *is* running, yeah?


feh ERROR: Can't open X display. It *is* running, yeah?

So....I take it cron cannot access my X session? Is there anyway to get around this? I'm beginning to think I'll have to just write the script and have it running in the background constantly if cron cannot do this job.

Any thoughts?

colucix 08-21-2010 06:10 PM

You can try the hint given by Elv13 above. Regarding the mailbox, do you mean the mail command does not exist on your system and you cannot receive system mail?

Elv13 08-21-2010 06:22 PM

Quote:

Originally Posted by alphisb0t (Post 4073635)
Ok I redirected output (why didn't I do this before) to a file.

feh ERROR: Can't open X display. It *is* running, yeah?


feh ERROR: Can't open X display. It *is* running, yeah?

So....I take it cron cannot access my X session? Is there anyway to get around this? I'm beginning to think I'll have to just write the script and have it running in the background constantly if cron cannot do this job.

Any thoughts?

Have you added DISPLAY=:0 as I said? If it can not redirect from there, then you will have to run the script from X. All WMs have autorun capabilities in a way or an other.

The script can look like
Code:

#!/bin/bash
{
while true;do
  feh ... <your wallpaper>
  sleep 15m
done
} &

Will will run every 15 minutes

alphisb0t 08-21-2010 06:25 PM

Adding DISPLAY=:0 before feh was just what I needed! Works perfectly now!

Thanks much.

If anyone is interested in the BASH script I made for this here it is:

Quote:

#!/bin/bash

MAX=`ls /home/rage/wallpapers | grep -v \.sh | wc -l`
COUNTER=0

#echo "MAX: $MAX"
INDEX=$RANDOM
let "INDEX %= $MAX"

#echo "INDEX: $INDEX"

for i in `ls /home/rage/wallpapers | grep -v \.sh`;
do
if [ $COUNTER -eq $INDEX ]; then
#echo "switching to $i"
DISPLAY=:0 feh --bg-scale /home/rage/wallpapers/$i &
break
fi
let COUNTER+=1
done

Thanks again for all your help! =]

Edit:

to answer your question regarding mail, yes mail is not in my path. I checked dead.letter and got the output /var/log/messages was referring to


All times are GMT -5. The time now is 05:48 AM.