Cronjob on ubuntu as su is not working

Get help for specific problems
Posts: 8
Joined: 21 Nov 2019

Homer-S

Hello all,
I am using FFS for years on my Ubuntu server. I am trying for nearly the same time to get a sudo cronjob for a batch sync to work without success.
I tried nearly everything I found in the web but all is stopping with this error message:

Error: Unable to initialize GTK+, is DISPLAY set properly?

Is there a working instruction?

here the latest entry in crontab which is sometimes working for non sudo but never for sudo
in sudoers FreeFileSync is allowed to be run without password
00 00 * * * DISPLAY=:10.0 /usr/local/bin/FreeFileSync /home/homer/Documents/Sicherungen2BackupSicherungen.ffs_batch > /home/homer/ffs-daten.log/Sich2BkpSich_`date +\%Y-\%m-\%d_\%H:\%M:\%S`.log 2>&1
Posts: 306
Joined: 7 Jan 2018

bgstack15

You share information about su and sudo but I don't see that invoked in your command at all. Is this in the user crontab (crontab -e)?
If this is in a file in /etc/cron.d/ then you will probably want to add the user after the time specification:
00 00 * * * homers DISPLAY=:10.0 /usr/local/bin/FreeFileSync ...
If are having trouble with DISPLAY=:10.0, then perhaps you need to adjust which DISPLAY you use. I don't know the defaults for Ubuntu. Perhaps :0 or :0.0 is the default "console" value. Chances are you see the DISPLAY=:10.0 when you use vnc or some other remote-desktop type connection to your server.
If you want to get crazy with it, you could probably install Xvfb which creates a dummy X server (X11 Virtual Frame Buffer). See this post.
Posts: 8
Joined: 21 Nov 2019

Homer-S

Thanks for your reply.
" Is this in the user crontab (crontab -e)?"

Yes this is at the moment in the user crontab. I tried it in sudo crontab -e too but this did not work.

If I do echo $DISPLAY via ssh connection
I receive always the 10.0

I tried it already with 0.0 etc
Posts: 306
Joined: 7 Jan 2018

bgstack15

When you ssh to your server, and you have a $DISPLAY value in that session, then ssh is doing X-forwarding. That :10.0 session will not exist when you do not have that ssh session running. You probably don't want to try to use that.
If you want to get really messy, you can learn which pid (process id) is running for lightdm/gdm/kdm/sddm (your display manager; that is, the login screen on the console). Use ps -ef and find one of those, or your chosen display manager. Call that process's pid $PID for the next step.
tr '\0' '\n' /proc/$PID/environ | grep DISPLAY
If your Ubuntu server does not run any X11 session at all, then you would need to run Xvfb as described in that linked post.
Posts: 8
Joined: 21 Nov 2019

Homer-S

I tried the xvfb-run command and it worked from cli. Next try will be adding it to sudo crontab.
I will report tomorrow :)

I am using lightdm and tried the command (after finding PID) but without success on the first try (missing operator). I did not deep dive because xvfb worked

Big Thanks for your help