Jump to content

Running a cron job through PHP/Apache?!


razta

Recommended Posts

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
Share on other sites

@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
Share on other sites

@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
Share on other sites

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
Share on other sites

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
Share on other sites

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!  :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.