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. Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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! Quote Link to comment 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. Quote Link to comment 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! Quote Link to comment 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! Quote Link to comment 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. Quote Link to comment 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.