Learn how to a build a cloud-first strategyRegister Now


schedule cron job with php

Posted on 2012-08-22
Medium Priority
Last Modified: 2012-08-22
i have a user forum that  the user fills out and creates the crontab format. Then i have it run

Open in new window

shell_exec ("crontab $minutes $hours $days $months $weekdays ".$_POST["command"] );

on line 52 of work.php

but in my cpanel of my test site it never creates the job.
Question by:armasmike
  • 2
  • 2
LVL 35

Expert Comment

ID: 38321376
1. What is the output of $cronout ?

2. Try changing "crontab" to "/usr/bin/crontab"

3. If #2 doesn't work, then it may be a permissions issue. If you're running this from a web browser or something, then the user that PHP is running as may not have permissions to run/modify crontab.

Author Comment

ID: 38321394
1. the echo of the $cronout is

* * * * 0 mike.php

2. That didn't work.

3. how can i test this? See some output showing the user doesn't have permissions ?

on side note

echo shell_exec ("crontab -l");

does show the cron jobs i set up using the cpanel of the test site.
LVL 35

Accepted Solution

gr8gonzo earned 2000 total points
ID: 38321512
Your cron jobs are per-user and are stored in /var/spool/cron. If you're running something like mod_php through Apache, then PHP is going to be run using the user that Apache is set up with (which is usually something very non-privileged, like "nobody" or "daemon" or "www").

At that point, it just becomes a simple Linux filesystem permissions issue - "nobody" cannot write to a file that can only be modified by "root", for example.

If you need the resulting crontab entry for mike.php to be put into root's crontab list, then you'll  either need to go down the unpleasant road of trying to use sudo with Apache, or else have your script write out $cronout to a file somewhere like "cronjobs_to_be_added.txt", and then have a separate cron job running as root that reads in that file and "imports" them into the root user's crontab.

To better explain, think of work.php as a delivery man who needs to get a package into a very secure building (the root crontab). Since the delivery man doesn't have permissions to go all the way into the building, he just goes to some more-accessible location like the building lobby and he gives the package to a receptionist (cronjobs_to_be_added.txt) and his job is then done. Then, every minute, someone from inside the secure building (a script that is executed from the root crontab) comes out to check the lobby/receptionist for packages to bring inside.

You'll need to be careful, because this could open up a security issue if someone else is allowed to write to that cronjobs_to_be_added.txt file. In other words, if someone was able to pose as a delivery man and give a bomb to the receptionist, then the guy from inside brings it into the building... you can see where that could be an issue.

Now, if you don't need to run mike.php or other scripts as root, then you just need to set up a crontab for the user that Apache is running as. Then you should be able to add cron jobs for that specific user.

If you want to do this, then I would open up a new question in the Linux area of Experts-Exchange for help on how to do this. Otherwise this current question could get muddied up really quickly with unrelated details.

Author Closing Comment

ID: 38321700
great answer

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month20 days, 19 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question