Jump to content
#StayAtHome ×

Archived

This topic is now archived and is closed to further replies.

razta

Running a cron job through PHP/Apache?!

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.

 

 

Share this post


Link to post
Share on other sites

Try shell_exec(), otherwise if it is a permission problem, then you can't do anything until you have permission.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
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! :)

 

 

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
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!

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

×
×
  • 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.