schedule cron job with php

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.
phpcrontab.php
work.php
armasmikeAsked:
Who is Participating?
 
gr8gonzoConnect With a Mentor ConsultantCommented:
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.
0
 
gr8gonzoConsultantCommented:
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.
0
 
armasmikeAuthor Commented:
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.
0
 
armasmikeAuthor Commented:
great answer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.