Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I want to schedule a gui application in cron, but there is a problem. When cron tries to run the scheduled task (amarok in my case) it throws cannot connect to X server error. I assume it is because the application is executed from my root account. Is it possible to run an application from a root but to pretend to be someone else?

Thanks.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Cronjobs run in a very restricted environment, meaning it doesn't know where to find the X server (the $DISPLAY environment variable doesn't exist).

If you want to execute a X application from crontab, you need to first set the $DISPLAY environment variable.

Either call AmaroK like pQd said, or set it for the whole the cron-script:

DISPLAY=:0.0
# ...
* * * * * amarok

Also, I would recommend you to run this as the account you "want to be", rather than root, this is done by running crontab -e as the user.

EDIT:

You should also be able to do it like this:

* * * * * export DISPLAY=:0.0 && amarok
share|improve this answer
    
Thanks, yeah I am using DISPLAY=:0.0 amarok.sh - works perfect. –  Maksim Vi. Nov 22 '09 at 18:08

i think it's bad idea, nevertheless you can execute as root:

su -c 'export DISPLAY=":0.0"; amarok' username

i assume x session is running and you do have user username.

but... you can run from cron scripts as non-root - so why do you insist on invoking it as root? there are also plenty of command-line controlled media players - maybe you do not need amarok after all?

to run script as non-root put in /etc/crontab

25 6    * * *   username /script/you/want/to/execute.sh

depending on location of commands you'll use you might need to add full path to them.

share|improve this answer
    
okay, how would I run cron scripts as non-root? So I won't see cannot connect to X server message? –  Maksim Vi. Nov 22 '09 at 18:05

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.