crontab: How to run GUI programs with cron
This is an updated Thread from 5.10. It works on 6.06.
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:
you can also use a nicer GUI tool to edit crontab, like gnome-schedule or KCron (both 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:
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 this thread.
Re: crontab: How to run GUI programs with cron
Thanks a lot !
It started to drive me crazy :)
Re: crontab: How to run GUI programs with cron
That is a very neat trick, thank you! (I was looking earlier for something like that)
Re: crontab: How to run GUI programs with cron
To both previous posters, I just like to say that I'm very glad that this HowTo helped you. When I found this, I was going mad trying to accomplish this without success. It's really nice to be able to share knowledge.
Re: crontab: How to run GUI programs with cron
Bravo! Thank you for the info. This helped me a lot!
Re: crontab: How to run GUI programs with cron
Thank you SO much. This is one of the most useful threads I've ever found on this forum. I had been trying to find a fix to this problem for weeks, and none of them worked -- except this one.
Re: crontab: How to run GUI programs with cron
Hi henriquemaia,
Firstly, thanks for this useful post :) the DISPLAY=:0 did the trick for me. Previously I was wondering why my stuff didn't run :)
Actually, I have a question though.
When you say "DISPLAY" here, are we talking about desktop workspaces? Cause I have a teeny little problem.
I'm trying to run the command:
Code:
rhythmbox-client --play-pause"
And it does run. The only problem is, that it launches a new instance of rhythmbox(therefore I have 2 music players). If I directly type the command "rhythmbox-client --play-pause" into the terminal, it just pauses/plays the current instance of rhythmbox, and doesn't create a new instance. Is this caused by the DISPLAY?
Hope you can enlighten me on this :)
Thanks,
Kinson.
Re: crontab: How to run GUI programs with cron
thanks a million - that did the trick. I was just about to give up altogether...
Re: crontab: How to run GUI programs with cron
I was just about to give up when I find this... thaanks
Re: crontab: How to run GUI programs with cron