Cron Job Set to Run as User, Running with User as Part of the Command
Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Cron Job Set to Run as User, Running with User as Part of the Command
I have a crontab file setup like this to run as the root user:
*/10 * * * * root /opt/inmar/dmt/proxyCheck.py
But every time that it runs, it runs once correctly with 'root' as the user and '/opt/inmar/dmt/proxyCheck.py' as the command and it also runs with 'root /opt/inmar/dmt/proxyCheck.py' as the command. This is evidenced in the syslog:
Feb 1 10:30:01 CORP-MSQLPRX1 CRON[16152]: (root) CMD ( /opt/inmar/dmt/proxyCheck.py)
Feb 1 10:30:02 CORP-MSQLPRX1 CRON[16153]: (root) CMD (root /opt/inmar/dmt/proxyCheck.py)
This is not the only cron job that runs like this. There are others where it is mistaking the user as part of the command line. How can I set it up properly and how do I get rid of the bad cron task that keeps running?
You don't need "root" in the crontab entry if the crontab is root's crontab. The crontabs run as the user that owns the crontab. So your entry in root's crontab should just be:
We are running CentOS 6 and 7. We also se this on Some Ubuntu 12 and 14 servers.
The /etc/crontab has an example that looks like this:
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
We have multiple crontab files in /etc/cron.d that are supposed to run as different users, with the crontab files being owned by root.
The really weird thing is that it simultaneously works correctly and doesn't. I would think that it would be one or the other.
Here is another example in the cron log were it ran both ways for a user other than root:
Feb 2 00:00:01 pwslhdpedgecp001 CROND[25828]: (kodoop) CMD (/opt/inmar/kognitio/kognitio_stop.sh) <--- Correct
Feb 2 00:00:01 pwslhdpedgecp001 CROND[25829]: (root) CMD (kodoop /opt/inmar/kognitio/kognitio_stop.sh) <--- Not Correct
No, that is what I don't understand either and why I am asking the question, hoping that someone else has seen this before or can point me in the right direction. There is only one crontab file with this job. I created the crontab file in /etc/cron.d and sudoed as root, I run:
crontab <filename>
crontab -e
(save the crontab with :wq)
it says 'crontab: installing new crontab'
That's it. Pretty simple.
Last edited by avalenti3; 02-03-2017 at 11:34 AM.
Reason: added the location of file created
You posted that you created cron jobs in /etc/cron.d which are system jobs and not the same thing as root's cron jobs which are saved in /var/spool/cron when you use crontab -e. The jobs create using crontab -e do not need a user-name.
If you added the same command when you run crontab -e then you are running it twice.
Last edited by michaelk; 02-03-2017 at 11:47 AM.
Reason: responded to additional information.
Ok, here is a better explanation after a bit of thinking I think. By running crontab <filename> as root you switched its default cron file from /var/spool/cron/root to /etc/cron.d/<filename> so it is running it twice i.e. as a system job and as root.
If you switch root's cron back i.e run crontab /var/spool/cron (as root) that should work although you could run crontab -e and then save a blank file via :wq just to "refresh" crond.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.