Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Wrote a short shell script "wallch.sh" to randomly change my desktop background:

DIR="/home/user/Wallpapers"
PIC=$(ls $DIR/*.jpg | shuf -n1)
FIL="file://"
gsettings set org.gnome.desktop.background picture-uri $FIL$PIC

And copied it to /bin/, then sudo chmod 755 /bin/wallch.sh to it. This could easily run from terminal smoothly without any error.

However when I add a line in crontab (doing this from terminal as user), like:

* * * * * /bin/wallch.sh

It just wouldn't run. Theoretically it should have run every minute.

/var/log/syslog says:

Feb 25 19:36:01 user-Ubuntu CRON[11762]: (user) CMD (/bin/wallch.sh # JOB_ID_3)
Feb 25 19:36:01 user-Ubuntu CRON[11761]: (CRON) info (No MTA installed, discarding output)
Feb 25 19:37:01 user-Ubuntu CRON[11790]: (user) CMD (/bin/wallch.sh # JOB_ID_3)
Feb 25 19:37:01 user-Ubuntu CRON[11789]: (CRON) info (No MTA installed, discarding output)
Feb 25 19:38:01 user-Ubuntu CRON[11828]: (user) CMD (/bin/wallch.sh # JOB_ID_3)
Feb 25 19:38:01 user-Ubuntu CRON[11827]: (CRON) info (No MTA installed, discarding output)

Any suggestions?

Update:

The script I wrote (please note that I changed the file name, now it is /bin/wallch. Also, I removed the comment lines beginning with # except the shebang):

#!/bin/sh
DIR="/home/user/Wallpapers"
PIC=$(ls $DIR/*.jpg | shuf -n1)
FIL="file://"
gsettings set org.gnome.desktop.background picture-uri $FIL$PIC

The stat /bin/wallch output:

user@user-Ubuntu:~$ stat /bin/wallch
File: `/bin/wallch'
Size: 484           Blocks: 8          IO Block: 4096   regular file
Device: 806h/2054d  Inode: 130389      Links: 1
Access: (4755/-rwsr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-02-26 11:03:01.727198732 -0600
Modify: 2013-02-26 10:37:46.727135926 -0600
Change: 2013-02-26 11:02:06.215196433 -0600
Birth: -
user@user-Ubuntu:~$ 

Thanks for any answers!

share|improve this question

2 Answers 2

Yes, you can install any MTA package. If you have an SMTP server (MTA) anywhere already that you can use, choose nullmailer. Otherwise you could go with postfix (requires some work) or courier-mta which comes with sane defaults, too (and is somewhat easier for beginners, IMO).

The fixed-width names above are package names on Ubuntu. So apt-get install <packagename> will get you the MTA installed. The main issue seems to be that you configured no MTA and therefore cron cannot send mail. The job itself doesn't indicate error from the log output I can see.

Side-note: you could also suppress any output from your jobs by appending (to the job line):

> /dev/null 2>&1

... and even suppress error exit codes with appending (further):

|| /bin/true

... I wouldn't recommend it, however. Also setting MAILTO in your crontab (see man 5 crontab) is another option here. But it's kinda "global" to the respective crontab:

MAILTO=""

What cron mails is the output (stdout/stderr, if it finds an MTA that is) of the job it ran. And it does that by default, yes. Also error exit codes (non-zero) will result in the output being mailed.

However, it also needs to be able to run the job, which in all cases requires that the file be executable (chmod a+x ...) or that, in case of a script, you prepend the path to the script interpreter and that in, case of a script, a proper hashbang line exists at the beginning of the file.

share|improve this answer
    
Thanks for your answering. But I still have a question: does cron have to send mails each time it performs the scheduled task? From my script you can see not even once was mail/MTA mentioned... –  Kazzantra Feb 26 '13 at 5:17
    
Besides, I remember once I added the >/dev/null 2>&1 option with gnome-schedule but it did not work either... –  Kazzantra Feb 26 '13 at 5:18
    
@Kazzantra: in this case your question is worded very very badly. My crystal ball is in repair, so I couldn't see what exactly you were asking before your comment. See my edit to my answer. Feel free to edit your question to reflect your change of mind. –  0xC0000022L Feb 26 '13 at 12:49
    
Thanks for your answer again, but it still does not work. For this script, I did chmod 755 to it, and also tried to prepend /bin/sh to it, however none worked. I actually do not need any output info from it, since it changes my desktop background every minute and I can see it at once if it works. >/dev/null 2>&1 and >/dev/null 2>&1 || /bin/true did not work either. –  Kazzantra Feb 26 '13 at 16:21
    
Then why don't you set MAILTO="" and make sure your script doesn't leak a non-zero error code? It is all in my answer. –  0xC0000022L Feb 26 '13 at 16:28

use this cron line instead

* * * * * DISPLAY=:0 GSETTINGS_BACKEND=dconf /bin/wallch.sh

could also use */2 in the hour column for a background change every two hours (all even hours)

Offtopic advice: this is not a rule just a form to keep the scripts apart, use /usr/local/bin for you admin scripts. :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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