LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Cron Job Set to Run as User, Running with User as Part of the Command (https://www.linuxquestions.org/questions/linux-server-73/cron-job-set-to-run-as-user-running-with-user-as-part-of-the-command-4175598892/)

avalenti3 02-03-2017 08:24 AM

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?

Thanks,
Anthony

MensaWater 02-03-2017 09:05 AM

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:
Code:

*/10 * * * * /opt/inmar/dmt/proxyCheck.py

michaelk 02-03-2017 09:12 AM

In addition what linux distribution/version are you running? Are you using a system cron job i.e you added it to /etc/crontab?

avalenti3 02-03-2017 10:02 AM

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

michaelk 02-03-2017 10:52 AM

At the moment I do not understand why the job is running twice? Do you have multiple cron jobs for the same task?

avalenti3 02-03-2017 11:21 AM

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.

michaelk 02-03-2017 11:34 AM

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.

avalenti3 02-03-2017 11:57 AM

So if I put the file in /etc/cron.d, don't use crontab -e. How do I remove the crontab -e version?

michaelk 02-03-2017 12:36 PM

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.


All times are GMT -5. The time now is 03:55 PM.