0

I'm setting up a new Webserver and having trouble getting cron to execute as expected. I am using a script to create cron jobs in .tmp/crontab.txt, which is then moved to /var/spool/cron under the user "Apache". These Apache entries don't execute as far as I can tell.

/var/spool/cron: has 3 user files:

  • Root 0 bytes

  • Apache 344 bytes (looks good, does not execute).

  • JLee 3514 bytes(this is where all my manual entries went).

The manual jobs I schedule are being executed normally. However, the manual jobs create a "sub-cron" and that is the one going to Apache. What am I doing wrong?

I would prefer all cron jobs be created under one User, as I am the admin and sole user of this server.

  • 1
    It seems like you have multiple questions? 1. Why is the crontab of apache not executed? 2. Which UID should be used to run cron jobs as? -- To the first one: do not edit crontabs directly, use crontab. These files are not ment to be changed manually (at least for some implementations of cron). To the second one: running all jobs as one user is likely a security risk, as only root can do everything technically possible. If you explain your intentions (what should the server do? Is it accessible from the internet?) you might get better answers. – dst Sep 17 '15 at 1:15
  • Yes I use schedule tasks in Plesk to enter cron. That is what I mean by manual. Schedule tasks runs fine. The cron job execute a php script which then, attempts to create the sub-cron, which fails. – Jack Lee Sep 17 '15 at 1:41
2

The cron daemon doesn't read the new files if you just copy them. You should use the crontab command to install the new crontab file. It also signals the cron daemon to read the files.

From the crontab man page:

...and though these are files in /var, they are not  intended  to  be
   edited directly.
  • Hi Keith, yes I am trying to avoid editing them directly. So I'm placing crontab.txt in /tmp and the system has assigned that file to "apache". Can I change that "apache" to a different user when writing this file? Thank you. – Jack Lee Sep 17 '15 at 5:12
  • Is the script running as root? – Keith Sep 17 '15 at 5:13
  • I should also mention the "apache user" is not authorized to run cron, therefore the jobs just sit there. Yes I run a single script as root. – Jack Lee Sep 17 '15 at 5:13
1

As has already been pointed out in the answer from Keith, you should not put files directly into /var/spool/cron as they will not be seen by crond.

You should maintain your own instance of a user's crontab file, and use the crontab command to signal the changes to crond:

echo '* * * * * date >>/tmp/output' | crontab

If you can create multiple entries you could write each one to its own file and then cat the results into a single crontab.

mkdir .crondir
...
echo '* * * * * echo job1 >> /tmp/output.1' > .crondir/job1
echo '* * * * * echo job2 >> /tmp/output.2' > .crondir/job2
echo '* * * * * echo job3 >> /tmp/output.3' > .crondir/job3
cat .crondir/job* | crontab

Obviously it's necessary that the user running the crontab command has rights to use it. Depending on how you have set up your permissions scheme for your application it could be its own user account. Otherwise it might have to be your web server user (apache) or possibly even root.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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