0

I just wanted to run all my cronjob manually and immediately, following command works well in command line terminal but not working with shell_exec

crontab -l | grep -v '^#' | cut -f 6- -d ' ' | while read CMD; do eval $CMD; done

Can anyone please help me how can I merge this with php, I tried as following but not working:

shell_exec('crontab -l | grep -v "^#" | cut -f 6- -d " " | while read CMD; do eval $CMD; done');

One more thing, I just wants to trigger without bothering about output and waiting.

I have also tried creating shell script: cron.sh and tried: shell_exec('sh cron.sh'); Still no success

1
  • I have created one shell script and placed that command inside, and running shell_exec('sh xyx.sh') but still no success
    – minc
    Jan 25, 2016 at 11:14

2 Answers 2

1

I just wanted to run all my cronjob manually and immediately ... I just wants to trigger without bothering about output and waiting.

So why not just leave cron to do what cron does, which is to run jobs when they need to be run and (as far as you're concerned as a User) "without bothering about output and waiting"?

The cron utility is a scheduler, which means that the things that it runs want to be run periodically.
Why do you feel the need to run any of them "immediately"?
Is there any danger in running any of these processes more than once at the same time (the risk you run by trying to do cron's job for it)?

The cron utility runs in the security context of the root user.
PHP does not.
So, in order to get PHP to be able to do [all] the things that cron does, you'd have to give it access to root-level privileges (through sudo). This is a Bad Idea(TM) unless you very tightly control what your "elevated PHP" is allowed to do and that's just making a maintenance headache for yourself, because such configuration will break whenever you added a new cron job.

0

Are you using this php script whit apache ? In this case you should check if apache has the rights to exec those crontabs.

Maybe $CMD is not being well parsed, php and bash use the same syntax for the variables... Who is interpreting this?

Also I would recommend you to use the exec function instead, so you can debug a little more with the output and return code.

Good luck!

3
  • Yes I am using apache lampp, and how can i check for that
    – minc
    Jan 25, 2016 at 11:00
  • I have created one shell script and placed that command inside, and running shell_exec('sh xyx.sh') but still no output
    – minc
    Jan 25, 2016 at 11:13
  • Apache runs under the apache user or www-data user. This user is not capable to run commands who need root rights. If you need to run such command, you have to use sudo. check: blogmotion.fr/systeme/… (sorry, is in french)
    – uDalillu
    Jan 25, 2016 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.