Cron works with user credentials but not root

Ultimately the goal is to set up a monthly cron to run a web server security certificate renewal command. But this is my first dive into cron so I created a simple test example which pops up a message box every minute. It works as expected when I specify my user name but refuses to work under root. I want this command to run no matter who is logged in so I expect root is the proper user account. Here is the working test cron command:

* * * * * geno export DISPLAY=:0 && xterm -e "dialog --msgbox 'Testing cron from geno....' 234 234"

I expected that substituting root for geno would accomplish the same tasks but no such luck. Errors while using root include the following:

Mar 24 20:01:01 safeandtacticalfirearmstraining.com cron[1232]: (root) BAD FILE MODE (/etc/crontab)
Mar 24 20:16:01 safeandtacticalfirearmstraining.com cron[1232]: (root) BAD FILE MODE (/etc/crontab)
Mar 24 20:17:01 safeandtacticalfirearmstraining.com cron[1232]: (root) BAD FILE MODE (/etc/crontab)
Mar 24 20:24:01 safeandtacticalfirearmstraining.com cron[1232]: (root) BAD FILE MODE (/etc/crontab)
Mar 24 20:25:01 safeandtacticalfirearmstraining.com cron[1232]: (root) BAD FILE MODE (/etc/crontab)
Mar 24 20:26:01 safeandtacticalfirearmstraining.com cron[1232]: (root) BAD FILE MODE (/etc/crontab)
Mar 24 20:31:01 safeandtacticalfirearmstraining.com cron[1232]: (root) BAD FILE MODE (/etc/crontab)

Online research reveals that cron is particular about file permissions, so I made sure the file was owned by root with rwx permissions. I also assigned a group that included the cron user as well as my user geno.

A related issue: When I use sudo crontab -e a blank text file pops up. The crontab file I expect to load is at /etc/ which is populated with instructions and comments. I’ve found a crontab file at /usr/bin with the proper date but that does not correspond with any online documentation i’ve read so far. How do I get crontab -e to load the crontab file at /etc/?

You do not. crontab operates on per-user file(s) in /var/spool/cron/crontabs. Just use you favorite editor to manage /etc/crontab.

That was my workaround. Thx.

When you, as user geno11x11 run crontab -e you edit/make a crontab for user geno11x11.

When you, as user root use crontab -e you do the same for user root.

It is as easy as that. So e.g.

henk@boven:~> su -
Password: 
boven:~ # crontab -l
*/5 * * * *     /root/bin/nachtstop

*/5 * * * *     /root/bin/wij
boven:~ #

shows the crontab of root.

And this one shows that henk has no crontab (but he could create one of course).

henk@boven:~> crontab -l
no crontab for henk
henk@boven:~> 

But there is also the system crontab in /etc/crontab.

If you want cron jobs running “as root”, you can choose to use either root’s crontab or the system crontab. Personal preference.

hcvv, I understand your explanation and examples. As I was experimenting I was using a terminal logged in as root. It stands to reason then, that the command credentialed with root should have worked and the one credentialed as geno should have failed - but it worked in reverse. Examples below:

1) This worked: * * * * * geno export DISPLAY=:0 && xterm -e “dialog --msgbox ‘Testing cron from geno…’ 234 234”

2) This failed: * * * * * root export DISPLAY=:0 && xterm -e “dialog --msgbox ‘Testing cron from root…’ 234 234”

Can you diagnose the problem?

User root does not have access to $DISPLAY of your user.

How do I grant access or otherwise solve that problem?

Which problem? You did not show any evidence that your cron job has not run. If your problem is “how to allow other users to access my $DISPLAY” - read man Xsecurity and man xhost or make an Internet search for this phrase. And if you still has questions, start new topic with suitable subject. It is completely unrelated to cron.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.