schedule cron job with php

Posted on 2012-08-22
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
    LVL 34

    Expert Comment

    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

    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 34

    Accepted Solution

    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

    great answer

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Article by: kevp75
    Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
    Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
    The viewer will learn how to count occurrences of each item in an array.
    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.

    731 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now