Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Im using Linux Mint.

I have written a python script that is designed to not be accessible to the logged in standard user but is still be executed while they are logged in. It is to run and log them out if a certain field is set in a mysql database and show a dialog saying "you're being logged out blah".

If I run the python script as the standard user all is well and they get logged out.

If I run the python script as the admin user I get:

glib.GError: Unable to connect to server

when it tries to show the dialog box

If I re-run the script with the admin user and sudo, it works.

The idea is for this to run from cron, and unless I can get cron to run the script as the admin user with sudo it's not going to work.

Any help?

EDIT

this is the two gui parts of the script

this part is called every time the script runs and will show a notification with the amount of time the user has left

    u.get_time_left()
    n = pynotify.Notification("Time Left", str(u.timeLeft / 60)+" Minutes remaining")

    n.set_urgency(pynotify.URGENCY_CRITICAL)
    n.set_timeout(10000) # 10 seconds
    n.set_category("device")
    helper = gtk.Button()
    icon = helper.render_icon(gtk.STOCK_DIALOG_WARNING, gtk.ICON_SIZE_DIALOG)
    #n.set_icon_from_pixbuf(icon)
    try :
        n.show()

This part only runs if certain conditions are met ie the time they have left is under 5 mins.

def getText():
    #base this on a message dialog
    dialog = gtk.MessageDialog(
        None,
        gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
        gtk.MESSAGE_QUESTION,
        gtk.BUTTONS_OK,
        None)
    dialog.set_markup('Your time limit is nearing the <b>end</b>')
    entryUser = gtk.Entry()
    entryPass =gtk.Entry()
    entryUser.connect("activate", responseToDialog, dialog, gtk.RESPONSE_OK)
    hbox = gtk.HBox()
    hbox.pack_start(gtk.Label("Username:"), False, 5, 5)
    hbox.pack_start(entryUser)
    hbox.pack_start(gtk.Label("Password:"), False, 5, 5)
    hbox.pack_start(entryPass)
    dialog.format_secondary_markup("Please enter a new voucher <i>Username</i> and <i>password</i> to carry on your current session")
    dialog.vbox.pack_end(hbox, True, True, 0)
    dialog.show_all()
    dialog.run()
    username = entryUser.get_text()
    password = entryPass.get_text()
    dialog.destroy()
    voucher = {'username':username,'password':password}
    return voucher
share|improve this question

2 Answers 2

Why don't you add the cronjob to the standard user's crontab?

...

If it has to go in the admin user's crontab, and you have to run it with sudo for it to run properly, then you one solution might be to modify your sudoers file to allow the admin user to run the python script without a password.

admin hostname = NOPASSWD: /path/to/python/script.py

Then in the admin user's crontab file:

0 0 * * * sudo /path/to/python/script.py > /tmp/script.py.log 2>&1

To run the script at midnight and log output to /tmp/script.py.log (so you can see where/if errors occur).

This assumes that your use of admin user indicates that you have a non-root user with the username admin. Otherwise, by default, the root user is allowed to run sudo commands without authenticating.

share|improve this answer
    
Why not use the normal user's crontab: because then the normal user could remove or kill the job. I think that Steve means root by admin; the problem is that the Python script is trying to contact some notification server running as the normal user. –  Gilles Nov 16 '11 at 0:54
    
thanks @Gilles. that makes sense. i guess i think about things from a "server" point of view, where we don't usually use things like notifyd. :) –  Tim Kennedy Nov 16 '11 at 2:24
    
@Gilles yes thats correct I dont want the user to be able to kill it, the user I refer to as admin is the 1st user I setup on mint I think mint follows the same policy as ubuntu the first user is a member of the admin group and the admin group has all SUDO rights –  Shutupsquare Nov 16 '11 at 10:34

You can just add it to root's crontab: sudo crontab -e

It will run as root at the specified schedule. See man 5 crontab for the crontab file format.

EDIT

DISPLAY=:0
*/5 * * * * /path/to/your/script
share|improve this answer
    
Hi thanks but the script runs but it wont display the dialog box im guessing its getting the same glib.GError: Unable to connect to server error –  Shutupsquare Nov 15 '11 at 16:15
    
Is this application an X Application (with a GUI)? In this case you need to set the DISPLAY variable to export :0 in your crontab. –  Matteo Nov 15 '11 at 16:25
    
@Matteo Glib is the part of Gnome that doesn't require X (mostly data structures and communication facilities). I think it's looking for a notifyd, not for an X server. –  Gilles Nov 16 '11 at 0:56
    
@Gilles and mateo, yes this does have a gui part to it, its to show a notification and a dialog box, does cron need something else exporting to hook into the current users session? Ive tried the DISPLAY=:0 in roots crontab same thing happens glib error –  Shutupsquare Nov 16 '11 at 9:37

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.