razta Posted November 27, 2009 Share Posted November 27, 2009 Hello all, I have a PHP script which makes a cron file from user input. When i try to run the cron job via system() the job doesn't run, I suspect it has something to do with permissions. Any ideas on how I would go about getting this to work? Here is the system() call: echo $scriptOutput = system('crontab ' . $cronFile, $retval); Thank you in advance for your help. Link to comment https://forums.phpfreaks.com/topic/183152-running-a-cron-job-through-phpapache/ Share on other sites More sharing options...
abazoskib Posted November 27, 2009 Share Posted November 27, 2009 Try shell_exec(), otherwise if it is a permission problem, then you can't do anything until you have permission. Link to comment https://forums.phpfreaks.com/topic/183152-running-a-cron-job-through-phpapache/#findComment-966611 Share on other sites More sharing options...
trq Posted November 27, 2009 Share Posted November 27, 2009 The user your server runs under (eg; www-data) wouldn't normally have permissions to create cron jobs. Do you have shell access and permissions enough to create users on this machine? Link to comment https://forums.phpfreaks.com/topic/183152-running-a-cron-job-through-phpapache/#findComment-966629 Share on other sites More sharing options...
razta Posted November 27, 2009 Author Share Posted November 27, 2009 @abazoskib no joy with shell_exec() @thorpe Yes I do. Its a development machine I have on my network. Any idea what permissions Apache should have? Wouldn't giving it more permissions be a security issue? Thanks for your replys! Link to comment https://forums.phpfreaks.com/topic/183152-running-a-cron-job-through-phpapache/#findComment-966668 Share on other sites More sharing options...
trq Posted November 27, 2009 Share Posted November 27, 2009 @abazoskib no joy with shell_exec() @thorpe Yes I do. Its a development machine I have on my network. Any idea what permissions Apache should have? Wouldn't giving it more permissions be a security issue? Thanks for your replys! You best option would likely be to create a new user that can create cronjobs. You would then need to setup sudo to enable the apache user to su to this user without being promted for a password. Once that is setup you could execute something like..... sudo -u yourcronuser crontab $file You could also look at crontab's -u option (of course apache would need permissions to use this). The other option might simply be to add the apache user to the /etc/cron.allow file but I'm not sure that is the safest idea. Link to comment https://forums.phpfreaks.com/topic/183152-running-a-cron-job-through-phpapache/#findComment-966675 Share on other sites More sharing options...
razta Posted November 28, 2009 Author Share Posted November 28, 2009 Thanks for the reply. I will try a couple of your options and see if I can get something working. I will leave this topic unsolved for now as I may have further questions. Thanks again! Link to comment https://forums.phpfreaks.com/topic/183152-running-a-cron-job-through-phpapache/#findComment-966709 Share on other sites More sharing options...
razta Posted November 29, 2009 Author Share Posted November 29, 2009 Hello all, Still having problems trying to get this to work. I created a user called 'cron' and give it root privs and a blank password. PHP under the user 'nodoby' creates the cron file and then tries to add it to the user cron's crontab. echo $scriptOutput = shell_exec('sudo crontab -u cron ' . $cronFile); The cron file is being made and with the correct formatting however it is not being added to the user cron's crontab. Any ideas? Thanks again! Link to comment https://forums.phpfreaks.com/topic/183152-running-a-cron-job-through-phpapache/#findComment-967598 Share on other sites More sharing options...
trq Posted November 29, 2009 Share Posted November 29, 2009 You issuing a call to sudo which means your trying to execute crontab as root. You need to sudo to the 'cron' user. eg; echo $scriptOutput = shell_exec('sudo -u cron crontab ' . $cronFile); ps: Giving the cron user a blannk password probably isn't the best idea. You need to configure sudo to enable 'nobody' to sudo as 'cron' without the use of a password. Link to comment https://forums.phpfreaks.com/topic/183152-running-a-cron-job-through-phpapache/#findComment-967751 Share on other sites More sharing options...
razta Posted December 2, 2009 Author Share Posted December 2, 2009 Finally got it working! It was a problem with file/folder permissions in the end, just had to chmod 777 the files/folders that the web app needed access to. A tip for anyone doing the same: Output the cron job results to a file for debugging, i.e. "* * * * * yourcommand > output.txt" Thank you all for your help! Link to comment https://forums.phpfreaks.com/topic/183152-running-a-cron-job-through-phpapache/#findComment-969686 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.