2

I'm attempting to run a cron job as root user, but it will not execute. Here's what I've tried...

  1. Adding the job to my user crontab (crontab -e) and setting the user as root...

    17 10 * * * root /etc/cron.daily/cammedia.sh
    
  2. Adding the job to the root user crontab (sudo crontab -e) and also adding the a few PATH's...

    PATH=/usr/sbin:/usr/bin:/sbin:/bin
    PATH=/usr/sbin:/usr/bin:/sbin:/etc
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/home/scott
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/etc
    #
    0 4 * * * /etc/cron.daily/cammedia.sh
    

Here's the result of running crontab -l (local user account) after a test run:

May 26 10:17:01 WEBCAM CRON[89901]: (scott) CMD (root /etc/cron.daily/cammedia.sh)
May 26 10:17:01 WEBCAM CRON[89902]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May 26 10:17:01 WEBCAM CRON[89900]: (CRON) info (No MTA installed, discarding output)

Suggestions?

8
  • 2
    User crontabs (i.e. those invoked using crontab -e or sudo crontab -e in the case of root) do not support an additional field to specify the user - that's only valid in the system-wide crontab file /etc/crontab. See man 5 crontab for discussion of the differences. May 26, 2018 at 17:26
  • Please tell us when (at what time, at what interval) you want your cammedia.sh to be executed.
    – PerlDuck
    May 26, 2018 at 17:28
  • Thanks @steeldriver. How then, do I get my user script to run as root, assuming it needs to be run as root? Also, I didn't gain anything from the link you presented, sorry.
    – Scott
    May 27, 2018 at 15:37
  • @PerlDuck, I want to run this everyday, though the specific time is not really important; for example-sake, let's say run it @ 10:17 every AM.
    – Scott
    May 27, 2018 at 15:39

1 Answer 1

5

Just remove the line completely. Simply placing a script in /etc/cron.daily will already make it run once a day. You will have to rename it to not contain the .sh suffix to be suitable for the cron.daily directory.

If you do not want to run it at the default time for cron.daily you should store the script elsewhere, e. g. /usr/local/bin/cammedia.sh.

If you plan to do this you should be aware that only root can run cron jobs as another user, for normal users it is not valid.

Do not forget to make the script executable by running chmod +x cammedia.sh in any case.

The output of the script will be e-mailed to you. If that is not possible (and according to the logs in your question it is not), the output will be discarded. There are other ways to get the output. E. g. you could add the following statement to the cammedia script:

logger "cammedia script run"

and you should see something in syslog.

A good read on the subject is https://help.ubuntu.com/community/CronHowto, it will help you get your problem solved.

1
  • Thanks, @Sebastian, though I cannot get it to run simply by having it placed in the cron.daily folder. I removed the .sh extension, and I set & verifed 777 permissions on the file. The script functions perfectly if run independently. When I run 'grep CRON /var/log/syslog`, my script is not shown to have executed.
    – Scott
    May 27, 2018 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.