Ubuntu Forums ubuntu.com - launchpad.net - ubuntu help  

Go Back   Ubuntu Forums > The Ubuntu Forum Community > Other Community Discussions > Tutorials & Tips
Register Reset Password Forum Help Forum Council Search Today's Posts Mark Forums Read

Tutorials & Tips
The place to find Ubuntu related Tips & Tricks.

 
Thread Tools Display Modes
Old December 18th, 2005   #1
henriquemaia
Fresh Brewed Ubuntu
 
henriquemaia's Avatar
 
Join Date: Apr 2005
Location: Berlin, Germany
Beans: 1,213
Send a message via ICQ to henriquemaia Send a message via AIM to henriquemaia Send a message via Skype™ to henriquemaia
Lightbulb crontab: How to run GUI programs from cron

First of all, please refer to this howto to learn more about crontab.


My motivation to find this out was because there are several radio shows I really like and I was always missing them. As such, I needed something to make amarok play the chosen radio station at specific times. Probably this could be achieved through a more elegant solution, but what is explained here works great. Also, I knew that by learning how to do this, I could use it for many other things.

To accomplish this, you have to edit your crontab:

Code:
crontab -e
you can also use a nicer GUI tool to edit crontab, like Gnome Schedule (not in the repositories) or KCron (available on the repositories).

If you use a GUI tool, please adapt the instructions accordingly. I will explain this using as basis the default crontab editor.

To run a GUI command on cron, you'll have to tell cron what display the program should use. For that you use:

Code:
export DISPLAY=:0
The ':0' is the default. If you like the program to run on other display, please change the number accordingly (e.g. :1, :2, etc).

So, you add to what is explained here:

Code:
01 04 * * * /usr/bin/somedirectory/somecommand
the export variable, like this:

Code:
01 04 * * * export DISPLAY=:0 && /usr/bin/somedirectory/somecommand
You can omit the full path to the application bin. For amarok, you just need to use the 'amarok' command. Works fine both ways.

So, in my case, as I want amarok to play Antena2 (Portuguese classical radio station) at a specific time, I use:

Code:
30 16 * * 7     export DISPLAY=:0 && amarok mms://rdp.oninet.pt/antena2
This will make amarok start the Antena2 url every Sunday, at 16:30. But I want also to shut up amarok after the show is finished. So, I use:

Code:
0 17 * * 7     export DISPLAY=:0 && amarok -s
This will shut up amarok at 17:00 every Sunday.

(Just a side note - to make amarok play mms urls, you have to set it to use xine engine)

You can use this export trick to the limits of your imagination with every kind of application you want.

Another example that occurs to me is when you neet to use an application for a certain period of time, from 3AM to 9AM, for exemple.

Code:
0 3 * * * export DISPLAY=:0 && your_favorite_application
that will start your_favorite_application (change this to the command that runs the application you desire) everyday at 3AM. To shut it down, I use:

Code:
0 9 * * * killall your_favorite_application
This will shut it down at 9AM.

Hope this helps some of you.

If you have more ideas how to use this trick (or how to improve this HowTo), please share with us in the thread.

Last edited by henriquemaia; January 21st, 2006 at 12:06 PM.. Reason: Needed some rephrasing on certain paragraphs and some information was added.
henriquemaia is offline   Reply With Quote
Old January 21st, 2006   #2
henriquemaia
Fresh Brewed Ubuntu
 
henriquemaia's Avatar
 
Join Date: Apr 2005
Location: Berlin, Germany
Beans: 1,213
Send a message via ICQ to henriquemaia Send a message via AIM to henriquemaia Send a message via Skype™ to henriquemaia
Smile Re: HowTo make cron run GUI applications

This howto was updated. Have a good time!
henriquemaia is offline   Reply With Quote
Old January 21st, 2006   #3
HJThis
A Carafe of Ubuntu
 
HJThis's Avatar
 
Join Date: Oct 2005
Beans: 150
Ubuntu Breezy 5.10
Send a message via MSN to HJThis
Talking Re: HowTo make cron run GUI applications

Hi,henriquemaia

Nice work just like to ask is this somthing
that may work with any prog????

Thank you
__________________
Registered Linux user #402077
HJThis is offline   Reply With Quote
Old January 21st, 2006   #4
henriquemaia
Fresh Brewed Ubuntu
 
henriquemaia's Avatar
 
Join Date: Apr 2005
Location: Berlin, Germany
Beans: 1,213
Send a message via ICQ to henriquemaia Send a message via AIM to henriquemaia Send a message via Skype™ to henriquemaia
Thumbs up Re: HowTo make cron run GUI applications

I suppose so. I have tested this with lots of apps and always managed to get them working. As long as you put on you crontab the correct command line caller* to the application you want, there should be no problem at all. It is a good practice to test the command line before introducing it in crontab. Under a terminal, run:

Code:
export DISPLAY=:0 && your_favorite_application
and see if does what you expect it to do. If it does, than you can introduce it to crontab that you know for sure it will work.

Hope this helps you.

*remember that the command line to call an application is not always the application name. For example, calculator's command caller is 'gcalctool' if you use Gnome or 'kcalc' if you use KDE. Just a reminder.
henriquemaia is offline   Reply With Quote
Old April 2nd, 2006   #5
pgmario
Just Give Me the Beans!
 
pgmario's Avatar
 
Join Date: Jul 2005
Beans: 72
Xubuntu 9.04 Jaunty Jackalope
Re: HowTo make cron run GUI applications

Great how-to! Thanks a lot.
Also a good way to start an X-application from tty1.
__________________
www.neue-retro.de
pgmario is offline   Reply With Quote
Old April 3rd, 2006   #6
henriquemaia
Fresh Brewed Ubuntu
 
henriquemaia's Avatar
 
Join Date: Apr 2005
Location: Berlin, Germany
Beans: 1,213
Send a message via ICQ to henriquemaia Send a message via AIM to henriquemaia Send a message via Skype™ to henriquemaia
Re: HowTo make cron run GUI applications

Quote:
Originally Posted by pgmario
Great how-to! Thanks a lot.
Also a good way to start an X-application from tty1.
That's true. Now and then I use it. I really like this.
henriquemaia is offline   Reply With Quote
Old April 10th, 2006   #7
outofnicks
A Carafe of Ubuntu
 
outofnicks's Avatar
 
Join Date: Feb 2006
Location: SW Wisconsin, USA
Beans: 100
Kubuntu 8.04 Hardy Heron
Re: HowTo make cron run GUI applications

Thanks for the tutorial. I did a neat thing with it, scheduling voice announcements of the time every half-hour. Don't ask me why, just thought it was neat. I like to hear them in the morning starting about sunrise, it can help if I have to get up at a certain time.

I went to the AT&T Labs Text-to-Speech demo page at:

http://public.research.att.com/~ttsweb/tts/demo.php

Picking my favorite voice (Lauren), I phonetically spelled out the time like so:

Seven, thirty, ay em.

This sounded better than typing:
7:30 am
The "am" was spoken as am, the word, so AM and PM needed to be spelled phonetically.

I put all the sound files in a folder under my home directory, and added in crontab a line for each of them according to the tutorial here, like so:

Code:
30      07      *       *       *       export DISPLAY=:0 && totem /home/ken/Documents/Sounds/Time/7-30am.wav
That Text-to-speech demo is fun to play around with, very good quality voices
__________________
Kubuntu Hardy Heron
Registered Linux user #422217
outofnicks is offline   Reply With Quote
Old May 20th, 2006   #8
henriquemaia
Fresh Brewed Ubuntu
 
henriquemaia's Avatar
 
Join Date: Apr 2005
Location: Berlin, Germany
Beans: 1,213
Send a message via ICQ to henriquemaia Send a message via AIM to henriquemaia Send a message via Skype™ to henriquemaia
Re: HowTo make cron run GUI applications

Quote:
Originally Posted by outofnicks
Thanks for the tutorial. I did a neat thing with it, scheduling voice announcements of the time every half-hour. Don't ask me why, just thought it was neat. I like to hear them in the morning starting about sunrise, it can help if I have to get up at a certain time.

I went to the AT&T Labs Text-to-Speech demo page at:

http://public.research.att.com/~ttsweb/tts/demo.php

Picking my favorite voice (Lauren), I phonetically spelled out the time like so:

Seven, thirty, ay em.

This sounded better than typing:
7:30 am
The "am" was spoken as am, the word, so AM and PM needed to be spelled phonetically.

I put all the sound files in a folder under my home directory, and added in crontab a line for each of them according to the tutorial here, like so:

Code:
30      07      *       *       *       export DISPLAY=:0 && totem /home/ken/Documents/Sounds/Time/7-30am.wav
That Text-to-speech demo is fun to play around with, very good quality voices
This is nice. Thanks for sharing this idea.

I also set up a reminder using images that pop up every 3 hours. The cool thing is that the possibilities are endless. Cron is a very useful tool.
henriquemaia is offline   Reply With Quote
Old May 20th, 2006   #9
kashms
A Carafe of Ubuntu
 
Join Date: Sep 2005
Beans: 85
Send a message via MSN to kashms
Re: HowTo make cron run GUI applications

This is not working for me! For example I put the following entry in crontab:

Code:
10 * * * * export DISPLAY=:0 && zenity --notification --text "This is a test."
Nothing happens and I get a mail from cron:

Quote:
Xlib: connection to ":0.0" refused by server
Xlib: No protocol specified

Syntax error
Of course the command works from the terminal.
kashms is offline   Reply With Quote
Old May 20th, 2006   #10
henriquemaia
Fresh Brewed Ubuntu
 
henriquemaia's Avatar
 
Join Date: Apr 2005
Location: Berlin, Germany
Beans: 1,213
Send a message via ICQ to henriquemaia Send a message via AIM to henriquemaia Send a message via Skype™ to henriquemaia
Re: HowTo make cron run GUI applications

Quote:
Originally Posted by kashms
This is not working for me! For example I put the following entry in crontab:

Code:
10 * * * * export DISPLAY=:0 && zenity --notification --text "This is a test."
Nothing happens and I get a mail from cron:



Of course the command works from the terminal.
Two questions:
  • What is the number of the X you're running? To find out, use:
Code:
ls /tmp/.X11-unix/
  • Did you try running the command from a terminal emulator like GNOME Terminal or from a tty (console)?
I have tried the same command you shown and worked as expected. Even from the console (tty1).

Last edited by henriquemaia; May 21st, 2006 at 03:04 AM..
henriquemaia is offline   Reply With Quote

Bookmarks

Tags
crontab, gnome schedule

Thread Tools
Display Modes

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 On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 05:31 PM.


vBulletin ©2000 - 2010, Jelsoft Enterprises Ltd. Ubuntu Logo, Ubuntu and Canonical © Canonical Ltd. Tango Icons © Tango Desktop Project. lingonberry